From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: malc@boblycat.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Type inference problem
Date: Fri, 29 Jun 2001 11:41:37 +0900 [thread overview]
Message-ID: <20010629114137G.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <20010628172348.A24964@boblycat.com>
> Can anyone explain why O'Caml specializes the following:
>
> let simple ?fn list =
> let fn = match fn with
> | Some fn -> fn
> | None -> fun t -> t in
> let rec print = function
> | [] -> ()
> | x :: xs -> print_endline (fn x); print xs
> in
> print list
> val simple : ?fn:(string -> string) -> string list -> unit
The simple answer is type inference:
* fn and (fun t -> t) should have the same type 'a -> 'a
* the result of (fn x) is of type string
* consequently, 'a = string
* and the type of fn is string -> string
Now, what you would want is to distinguish when there is a function
passed and when there is none at the type level.
You cannot do it with optional arguments, but you can use the
following encoding:
module Default : sig
type ('a, 'b) t
val none : ('a, 'a) t
val some : 'a -> ('a, 'b) t
val get : 'a -> ('b, 'a) t -> 'b
val get_lazy : 'a Lazy.t -> ('b, 'a) t -> 'b
end = struct
type ('a,'b) t = Dnone | Dsome of 'a
let none = Dnone
let some x = Dsome x
let get d = function Dnone -> Obj.magic d | Dsome x -> x
let get_lazy d = function Dnone -> Obj.magic (Lazy.force d) | Dsome x -> x
end
let out ~fn x = let fn = Default.get (fun x -> x) fn in print_endline (fn x)
val out : fn:('a -> string, 'b -> 'b) Default.t -> 'a -> unit = <fun>
out ~fn:Default.none "Hello";;
out ~fn:(Default.some string_of_int) 3;;
Would it be possible to include it with optional arguments?
Theoretically, yes, but it would require a new syntax, make typing
more complex, and checking weaker (the type of the default is only
checked the first time the function is called with no argument).
I'm not sure it's worth it.
Jacques Garrigue
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
next prev parent reply other threads:[~2001-06-29 2:41 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-06-28 15:23 Vasilij Karpow
2001-06-28 15:40 ` Remi VANICAT
2001-06-28 19:08 ` Nils Goesche
2001-06-29 2:41 ` Jacques Garrigue [this message]
2005-11-01 7:58 Jonathan Roewen
2005-11-01 8:19 ` Jonathan Roewen
2005-11-01 8:50 ` skaller
2005-11-01 8:34 ` skaller
2005-11-01 16:21 ` Brian Hurt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20010629114137G.garrigue@kurims.kyoto-u.ac.jp \
--to=garrigue@kurims.kyoto-u.ac.jp \
--cc=caml-list@inria.fr \
--cc=malc@boblycat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox