caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Type inference + optional parameters
@ 2003-09-02 16:20 Christoph Bauer
  2003-09-02 17:06 ` brogoff
  2003-09-03  2:08 ` Jacques Garrigue
  0 siblings, 2 replies; 7+ messages in thread
From: Christoph Bauer @ 2003-09-02 16:20 UTC (permalink / raw)
  To: OCaml List

Hi,

ocamls type inference uses information of optionl arguments. This
results in a strange behaviour. Here is an example:

        Objective Caml version 3.07+beta 2

# let do_with_conv ~conv a = conv a;;
do_with_conv ~conv:int_of_string "1" ;;
val do_with_conv : conv:('a -> 'b) -> 'a -> 'b = <fun>

This is ok.

# let do_with_opt_conv ?(conv = fun s -> s) a = conv a;;
do_with_opt_conv ~conv:string_of_int "1";;- : int = 1
# val do_with_opt_conv : ?conv:('a -> 'a) -> 'a -> 'a = <fun>
# do_with_opt_conv ~conv:string_of_int "1";;

Is there a solution? It is not possible to 
add the "right" type information:

# let do_with_opt_conv ?(conv : 'a -> 'b = fun s -> s) (a:'a) : 'b = conv a;;
val do_with_opt_conv : ?conv:('a -> 'a) -> 'a -> 'a = <fun>

Thanks,
Christoph Bauer

-- 
proc self {} {foreach c [split [info body self] ""] d {14 -7 0 0 4 -67 4 73 11
69 24 -83 -15 6 -4 -84 78 20 11 -78 -1 -1 79 19 -8 4} { binary scan $c c c
if {[catch {append r [format %c [expr $c+$d]]}]} {return $r};}};puts [self]

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Type inference + optional parameters
  2003-09-02 16:20 [Caml-list] Type inference + optional parameters Christoph Bauer
@ 2003-09-02 17:06 ` brogoff
  2003-09-03  2:08 ` Jacques Garrigue
  1 sibling, 0 replies; 7+ messages in thread
From: brogoff @ 2003-09-02 17:06 UTC (permalink / raw)
  To: Christoph Bauer; +Cc: OCaml List

This behavior doesn't seem strange to me, not compared to that function you want 
to define, but what the heck, here's one solution to get the type you want 

# let do_with_opt_conv ?(conv = Obj.magic) a = conv a;;
val do_with_opt_conv : ?conv:('a -> 'b) -> 'a -> 'b = <fun>

and here's a better one 

# let do_with_opt_conv ?(conv = fun x -> invalid_arg "do_with_opt_conv") a = conv a;;
val do_with_opt_conv : ?conv:('a -> 'b) -> 'a -> 'b = <fun>

-- Brian

On Tue, 2 Sep 2003, Christoph Bauer wrote:

> Hi,
> 
> ocamls type inference uses information of optionl arguments. This
> results in a strange behaviour. Here is an example:
> 
>         Objective Caml version 3.07+beta 2
> 
> # let do_with_conv ~conv a = conv a;;
> do_with_conv ~conv:int_of_string "1" ;;
> val do_with_conv : conv:('a -> 'b) -> 'a -> 'b = <fun>
> 
> This is ok.
> 
> # let do_with_opt_conv ?(conv = fun s -> s) a = conv a;;
> do_with_opt_conv ~conv:string_of_int "1";;- : int = 1
> # val do_with_opt_conv : ?conv:('a -> 'a) -> 'a -> 'a = <fun>
> # do_with_opt_conv ~conv:string_of_int "1";;
> 
> Is there a solution? It is not possible to 
> add the "right" type information:
> 
> # let do_with_opt_conv ?(conv : 'a -> 'b = fun s -> s) (a:'a) : 'b = conv a;;
> val do_with_opt_conv : ?conv:('a -> 'a) -> 'a -> 'a = <fun>
> 
> Thanks,
> Christoph Bauer
> 
> -- 
> proc self {} {foreach c [split [info body self] ""] d {14 -7 0 0 4 -67 4 73 11
> 69 24 -83 -15 6 -4 -84 78 20 11 -78 -1 -1 79 19 -8 4} { binary scan $c c c
> if {[catch {append r [format %c [expr $c+$d]]}]} {return $r};}};puts [self]
> 
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> 

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Type inference + optional parameters
  2003-09-02 16:20 [Caml-list] Type inference + optional parameters Christoph Bauer
  2003-09-02 17:06 ` brogoff
@ 2003-09-03  2:08 ` Jacques Garrigue
  2003-09-03  8:08   ` Alain.Frisch
  2003-09-03  9:56   ` Michal Moskal
  1 sibling, 2 replies; 7+ messages in thread
From: Jacques Garrigue @ 2003-09-03  2:08 UTC (permalink / raw)
  To: c_bauer; +Cc: caml-list

From: Christoph Bauer <c_bauer@informatik.uni-kl.de>

> ocamls type inference uses information of optionl arguments. This
> results in a strange behaviour.

Brian Rogoff already answered this one. Do you seriously expect a
type-safe compiler to simply ignore the type of the default value :-)

> Here is an example:
> 
>         Objective Caml version 3.07+beta 2
> 
> # let do_with_opt_conv ?(conv = fun s -> s) a = conv a;;
> do_with_opt_conv ~conv:string_of_int "1";;- : int = 1
> # val do_with_opt_conv : ?conv:('a -> 'a) -> 'a -> 'a = <fun>
> # do_with_opt_conv ~conv:string_of_int "1";;
> 
> Is there a solution? It is not possible to 
> add the "right" type information:
> 
> # let do_with_opt_conv ?(conv : 'a -> 'b = fun s -> s) (a:'a) : 'b = conv a;;
> val do_with_opt_conv : ?conv:('a -> 'a) -> 'a -> 'a = <fun>

The answer is that the function you want cannot be defined with
optional arguments.

Actually, we need a slightly more expressive type to allow this.

module Default : sig
  type ('a,'b) t
  val default : ('a,'a) t
  val custom : 'a -> ('a,'b) t
  val get : (unit -> 'b) -> ('a,'b) t -> 'a
end = struct
  type ('a,'b) t = Custom of 'a | Default
  let default = Default
  let custom x = Custom x
  let get def = function
      Default -> Obj.magic (def ())
    | Custom x -> x
end

Note that you need to use Obj.magic to define the above module, but
the type of [default] ensures that this is sound.

Then you can define your function:

# let do_with_opt_conv ~conv a =
  let conv = Default.get (fun () x -> x) conv in
  conv a ;;
val do_with_opt_conv : conv:('a -> 'b, 'c -> 'c) Default.t -> 'a -> 'b
# do_with_opt_conv ~conv:Default.default 1;;
- : int = 1
# do_with_opt_conv ~conv:(Default.custom string_of_int) 1;;
- : string = "1"

Jacques Garrigue

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Type inference + optional parameters
  2003-09-03  2:08 ` Jacques Garrigue
@ 2003-09-03  8:08   ` Alain.Frisch
  2003-09-06  0:53     ` Jacques Garrigue
  2003-09-03  9:56   ` Michal Moskal
  1 sibling, 1 reply; 7+ messages in thread
From: Alain.Frisch @ 2003-09-03  8:08 UTC (permalink / raw)
  To: Caml list

On Wed, 3 Sep 2003, Jacques Garrigue wrote:

> From: Christoph Bauer <c_bauer@informatik.uni-kl.de>
>
> > ocamls type inference uses information of optionl arguments. This
> > results in a strange behaviour.
>
> Brian Rogoff already answered this one. Do you seriously expect a
> type-safe compiler to simply ignore the type of the default value :-)

I guess the original question is not that naive. It would be possible to
have a type system with an extension like your multimatch, to make the
type of the result of a function depend on the presence/absence of
optional argument.

With a crazy syntax to express conditional unification constraints, you
could write for instance:

do_with_opt_conv: ?conv:('a -> 'b  orelse 'b = 'a) -> 'a -> 'b

meaning that in the absence of the optional argument, the result type
must be unified with the argument type (there are more constraints
when the optional argument is not provided).

Does it make sense ?

-- Alain

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Type inference + optional parameters
  2003-09-03  2:08 ` Jacques Garrigue
  2003-09-03  8:08   ` Alain.Frisch
@ 2003-09-03  9:56   ` Michal Moskal
  1 sibling, 0 replies; 7+ messages in thread
From: Michal Moskal @ 2003-09-03  9:56 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: c_bauer, caml-list

On Wed, Sep 03, 2003 at 11:08:13AM +0900, Jacques Garrigue wrote:
> From: Christoph Bauer <c_bauer@informatik.uni-kl.de>
> 
> > ocamls type inference uses information of optionl arguments. This
> > results in a strange behaviour.
> 
> Brian Rogoff already answered this one. Do you seriously expect a
> type-safe compiler to simply ignore the type of the default value :-)

In principle you can imagine type system, where function have different
type depending on whether it has default argument supplied. For example:

do_with_conv : ~conv:('a -> 'b) -> 'a -> 'b [default conv: 'a -> 'a]

or something like this.

-- 
: Michal Moskal :: http://www.kernel.pl/~malekith : GCS {C,UL}++++$ a? !tv
: When in doubt, use brute force. -- Ken Thompson : {E-,w}-- {b++,e}>+++ h

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Type inference + optional parameters
  2003-09-03  8:08   ` Alain.Frisch
@ 2003-09-06  0:53     ` Jacques Garrigue
  0 siblings, 0 replies; 7+ messages in thread
From: Jacques Garrigue @ 2003-09-06  0:53 UTC (permalink / raw)
  To: Alain.Frisch; +Cc: caml-list

From: Alain.Frisch@ens.fr
> On Wed, 3 Sep 2003, Jacques Garrigue wrote:
> > From: Christoph Bauer <c_bauer@informatik.uni-kl.de>
> >
> > > ocamls type inference uses information of optionl arguments. This
> > > results in a strange behaviour.
> >
> > Brian Rogoff already answered this one. Do you seriously expect a
> > type-safe compiler to simply ignore the type of the default value :-)
> 
> I guess the original question is not that naive. It would be possible to
> have a type system with an extension like your multimatch, to make the
> type of the result of a function depend on the presence/absence of
> optional argument.
> 
> With a crazy syntax to express conditional unification constraints, you
> could write for instance:
> 
> do_with_opt_conv: ?conv:('a -> 'b  orelse 'b = 'a) -> 'a -> 'b
> 
> meaning that in the absence of the optional argument, the result type
> must be unified with the argument type (there are more constraints
> when the optional argument is not provided).
> 
> Does it make sense ?

It makes sense, and is just a direct consequence of the Default module
I was describing in my mail. I've already considered it 4 years ago,
after a comment for Wolfram Kahl.
However, if you let this behaviour be active with all default
parameters, you end up with overly general types; usually you expect
the concrete argument to be of the same type as the default.
Moreover, there may be hidden pitfalls: type incompatibilities you
only detect later, when several optional parameters are omitted
simultaneously.

All in all, I think that this feature is just on the borderline of
practical typing: possible, but the typing is too complex for casual
use, and may result in confusing type errors.
In some cases being explicit simplifies lots of things...

Jacques Garrigue

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Type inference + optional parameters
       [not found] <m3fzjf825m.fsf@diebuntekuh.de>
@ 2003-09-02 19:01 ` brogoff
  0 siblings, 0 replies; 7+ messages in thread
From: brogoff @ 2003-09-02 19:01 UTC (permalink / raw)
  To: Christoph Bauer; +Cc: caml-list

On Tue, 2 Sep 2003, Christoph Bauer wrote:
> Sorry, but what are the benefits of these functions over the original
> function without the optional parameter? 

None. I was being facetious, sorry. 

> I need a conversion function conv:'a -> 'b and the default value should be 
> the identity.

There is no suitable default suitable conversion function of that type (think 
about it for a minute and you'll understand), so there is no good way to make 
an optional argument of type ('a -> 'b). 

-- Brian


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2003-09-06  0:53 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-02 16:20 [Caml-list] Type inference + optional parameters Christoph Bauer
2003-09-02 17:06 ` brogoff
2003-09-03  2:08 ` Jacques Garrigue
2003-09-03  8:08   ` Alain.Frisch
2003-09-06  0:53     ` Jacques Garrigue
2003-09-03  9:56   ` Michal Moskal
     [not found] <m3fzjf825m.fsf@diebuntekuh.de>
2003-09-02 19:01 ` brogoff

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).