caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Equality between abstract type definitions
@ 2013-10-24 22:57 Peter Frey
  2013-10-24 23:23 ` Jacques Garrigue
  0 siblings, 1 reply; 27+ messages in thread
From: Peter Frey @ 2013-10-24 22:57 UTC (permalink / raw)
  To: caml-list

Please consider the following two definitions of map which I would have
considered equal.  

  exception Empty

  type 'a t = Cons of 'a * (int -> 'a t) * int

  let null = Cons( Obj.magic None, (fun _ -> assert false), max_int ) 

  let map (f: 'a -> 'b) (t: 'a t) : 'b t =
      if t == null then null else match t with (Cons( h, fn, p )) ->
      let rec aux h1 p1 =
      try match fn p1 with (Cons (h2,_, p2)) -> 
         (Cons(f h1, aux h2, p2 ))
      with Empty                             -> (Cons(f h1, fn, p1)) in
      aux h p

  let map : 'a 'b. ( 'a -> 'b ) -> 'a t -> 'b t = fun f t -> 
    if t == null then null else match t with (Cons( h, fn, p )) ->
    let rec aux h1 p1 =
      try match fn p1 with (Cons (h2,_, p2)) -> (Cons(f h1, aux h2, 2 ))
    with Empty                               -> (Cons(f h1, fn, p1)) in
    aux h p

The second one gives the error below; no surprise.

Error: This definition has type 'b. ('b -> 'b) -> 'b t -> 'b t
       which is less general than 'a 'b. ('a -> 'b) -> 'a t -> 'b t

Why is the first definition accepted?  

Peter Frey




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

end of thread, other threads:[~2013-10-28  3:30 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-10-24 22:57 [Caml-list] Equality between abstract type definitions Peter Frey
2013-10-24 23:23 ` Jacques Garrigue
2013-10-25  6:44   ` Andreas Rossberg
2013-10-25  8:29     ` Roberto Di Cosmo
2013-10-25  9:59       ` Ivan Gotovchits
2013-10-25 11:09         ` Gabriel Scherer
2013-10-25 14:24           ` Andreas Rossberg
2013-10-25 20:32             ` Yaron Minsky
2013-10-25 20:44               ` Jacques Le Normand
2013-10-26  1:08                 ` Norman Hardy
2013-10-26  5:28                   ` Jacques Garrigue
2013-10-27 12:16               ` Andreas Rossberg
2013-10-27 12:56                 ` Yaron Minsky
2013-10-27 14:28                   ` Gabriel Scherer
2013-10-27 14:43                     ` Yaron Minsky
2013-10-27 15:25                       ` Gabriel Scherer
2013-10-27 15:41                         ` Yaron Minsky
2013-10-25 12:35         ` Roberto Di Cosmo
2013-10-25 12:45           ` Jonathan Protzenko
2013-10-25 13:20             ` Roberto Di Cosmo
2013-10-25 14:03       ` Andreas Rossberg
2013-10-26  9:07         ` oleg
2013-10-26 14:11           ` Didier Remy
2013-10-26 17:32         ` Didier Remy
2013-10-27 12:07           ` Andreas Rossberg
2013-10-27 14:10             ` Roberto Di Cosmo
2013-10-28  3:30     ` 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).