caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] mystified by typing of optional arguments
@ 2003-04-03 14:50 Francois Rouaix
  2003-04-03 15:04 ` Francois Thomasset
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Francois Rouaix @ 2003-04-03 14:50 UTC (permalink / raw)
  To: caml-list

Hi all,
The following example mystifies me

let f ?opt x =
   match opt with
   | None -> x
   | Some dx -> x + dx

let g x = x + 1


let h1 = function
   | 0 -> [g]
   | _ -> [f; g]


let h2 = function
   | _ -> [f; g]


h1 is typable but not h2.
Why is that ?

--f
PS: the example code is not meant to be useful in any way other than 
reproducing the behavior of type checking.

-------------------
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] 6+ messages in thread

* Re: [Caml-list] mystified by typing of optional arguments
  2003-04-03 14:50 [Caml-list] mystified by typing of optional arguments Francois Rouaix
@ 2003-04-03 15:04 ` Francois Thomasset
  2003-04-03 15:05 ` Ville-Pertti Keinonen
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Francois Thomasset @ 2003-04-03 15:04 UTC (permalink / raw)
  To: Francois Rouaix; +Cc: caml-list

> Hi all,
> The following example mystifies me
> 
> let f ?opt x =
>    match opt with
>    | None -> x
>    | Some dx -> x + dx
> 
> let g x = x + 1
> 
> 
> let h1 = function
>    | 0 -> [g]
>    | _ -> [f; g]
> 
> 
> let h2 = function
>    | _ -> [f; g]
> 
> 
> h1 is typable but not h2.
> Why is that ?
You cant' type [f;g] either !
# [ f ; g ];;
This expression has type int -> int but is here used with type
  ?opt:int -> int -> int
While [g;g] is a list of funs
FT



-------------------
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] 6+ messages in thread

* Re: [Caml-list] mystified by typing of optional arguments
  2003-04-03 14:50 [Caml-list] mystified by typing of optional arguments Francois Rouaix
  2003-04-03 15:04 ` Francois Thomasset
@ 2003-04-03 15:05 ` Ville-Pertti Keinonen
  2003-04-03 15:08 ` Eric C. Cooper
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Ville-Pertti Keinonen @ 2003-04-03 15:05 UTC (permalink / raw)
  To: Francois Rouaix; +Cc: caml-list

> The following example mystifies me
>
> let f ?opt x =
>   match opt with
>   | None -> x
>   | Some dx -> x + dx
>
> let g x = x + 1
>
>
> let h1 = function
>   | 0 -> [g]
>   | _ -> [f; g]
>
>
> let h2 = function
>   | _ -> [f; g]
>
>
> h1 is typable but not h2.
> Why is that ?

The type of the list is inferred from the first element of the first 
instance.

Similarly:

let x = [ g; f ] (* works *)

let x = [ f; g ] (* doesn't work *)

let x = [ (f : int -> int); g ] (* works *)

-------------------
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] 6+ messages in thread

* Re: [Caml-list] mystified by typing of optional arguments
  2003-04-03 14:50 [Caml-list] mystified by typing of optional arguments Francois Rouaix
  2003-04-03 15:04 ` Francois Thomasset
  2003-04-03 15:05 ` Ville-Pertti Keinonen
@ 2003-04-03 15:08 ` Eric C. Cooper
  2003-04-03 15:13 ` Jacques Carette
  2003-04-04  0:36 ` Jacques Garrigue
  4 siblings, 0 replies; 6+ messages in thread
From: Eric C. Cooper @ 2003-04-03 15:08 UTC (permalink / raw)
  To: caml-list

On Thu, Apr 03, 2003 at 04:50:44PM +0200, Francois Rouaix wrote:
> The following example mystifies me
> 
> let f ?opt x =
>   match opt with
>   | None -> x
>   | Some dx -> x + dx
> 
> let g x = x + 1
> 
> let h1 = function
>   | 0 -> [g]
>   | _ -> [f; g]
> 
> 
> let h2 = function
>   | _ -> [f; g]
> 
> 
> h1 is typable but not h2.
> Why is that ?

Even simpler: [g; f] type-checks, but [f; g] does not.

-- 
Eric C. Cooper          e c c @ c m u . e d u

-------------------
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] 6+ messages in thread

* RE: [Caml-list] mystified by typing of optional arguments
  2003-04-03 14:50 [Caml-list] mystified by typing of optional arguments Francois Rouaix
                   ` (2 preceding siblings ...)
  2003-04-03 15:08 ` Eric C. Cooper
@ 2003-04-03 15:13 ` Jacques Carette
  2003-04-04  0:36 ` Jacques Garrigue
  4 siblings, 0 replies; 6+ messages in thread
From: Jacques Carette @ 2003-04-03 15:13 UTC (permalink / raw)
  To: 'Francois Rouaix', caml-list

This can be simplified further:

let f ?opt x =
     match opt with
     | None -> x
     | Some dx -> x + dx

let g x = x + 1

Then (in ocaml 3.06)

# [f;g];;
Characters 3-4:
  [f;g];;
     ^
This expression has type int -> int but is here used with type
  ?opt:int -> int -> int
# [g;f];;
- : (int -> int) list = [<fun>; <fun>]

In other words it looks like unification of optional types is not
commutative !  This is indeed surprising.  I can understand that this could
simplify implementation, but is there a theoretical reason that underlies
this ?

Jacques

-----Original Message-----
From: owner-caml-list@pauillac.inria.fr
[mailto:owner-caml-list@pauillac.inria.fr] On Behalf Of Francois Rouaix
Sent: April 3, 2003 9:51 AM
To: caml-list@inria.fr
Subject: [Caml-list] mystified by typing of optional arguments 


Hi all,
The following example mystifies me

let f ?opt x =
   match opt with
   | None -> x
   | Some dx -> x + dx

let g x = x + 1


let h1 = function
   | 0 -> [g]
   | _ -> [f; g]


let h2 = function
   | _ -> [f; g]


h1 is typable but not h2.
Why is that ?

--f
PS: the example code is not meant to be useful in any way other than 
reproducing the behavior of type checking.

-------------------
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] 6+ messages in thread

* Re: [Caml-list] mystified by typing of optional arguments
  2003-04-03 14:50 [Caml-list] mystified by typing of optional arguments Francois Rouaix
                   ` (3 preceding siblings ...)
  2003-04-03 15:13 ` Jacques Carette
@ 2003-04-04  0:36 ` Jacques Garrigue
  4 siblings, 0 replies; 6+ messages in thread
From: Jacques Garrigue @ 2003-04-04  0:36 UTC (permalink / raw)
  To: francois; +Cc: caml-list

From: Francois Rouaix <francois@rouaix.org>

> Hi all,
> The following example mystifies me
> 
> let f ?opt x =
>    match opt with
>    | None -> x
>    | Some dx -> x + dx
> 
> let g x = x + 1
> 
> 
> let h1 = function
>    | 0 -> [g]
>    | _ -> [f; g]
> 
> 
> let h2 = function
>    | _ -> [f; g]
> 
> 
> h1 is typable but not h2.
> Why is that ?

As stated in many places in the manual, type inference for optional
arguments is not principal.
But there is a now a -principal option to ocaml and ocamlc, which
attempts to recover principality, and gives more information in such
cases.

% ocaml -principal
        Objective Caml version 3.06+27 (2003-03-31)

[...]
# let h1 = function
     | 0 -> [g]
     | _ -> [f; g]
             ^
  ;;
Warning: Eliminated optional argument without principality
val h1 : int -> (int -> int) list = <fun>


Note that -principal itself is available in ocaml 3.06, but would not
provide the above warning.

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] 6+ messages in thread

end of thread, other threads:[~2003-04-04  0:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-04-03 14:50 [Caml-list] mystified by typing of optional arguments Francois Rouaix
2003-04-03 15:04 ` Francois Thomasset
2003-04-03 15:05 ` Ville-Pertti Keinonen
2003-04-03 15:08 ` Eric C. Cooper
2003-04-03 15:13 ` Jacques Carette
2003-04-04  0:36 ` Jacques Garrigue

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).