Le Saturday 06 Aug 2011 à 14:58:49 (+0200), Fabrice Le Fessant a écrit :
> The type constraint that you specified does not constraint the
> polymorphism of the type. To declare a polymorphic constraint, you must
> use (with OCaml >= 3.12.0) :
>
> let pipe : 'a 'b 'c. ('a, 'b) parzer -> ('c, 'a) parzer -> ('c, 'b) parzer =
> fun p1 p2 ->
> let p1_rem = ref [] in
> fun bs -> match p1 (!p1_rem @ bs) with
> | Fail -> Fail
> | Wait -> Wait
> | Res (res, rem) ->
> p1_rem := rem ;
> (match p2 [res] with
> | Res (res', rem') ->
> if rem' <> [] then Printf.printf "WRN: second end of
> a pipe did not consume eveything !\n" ;
> Res (res', !p1_rem)
> | x -> x)
>
> In which case you get the following error :
> Error: This definition has type
> 'a 'b. ('b, 'b) parzer -> ('a, 'b) parzer -> ('a, 'b) parzer
> which is less general than
> 'c 'd 'e. ('e, 'd) parzer -> ('c, 'e) parzer -> ('c, 'd) parzer
>
> Fabrice
You can achieve a similar goal with stuff like:
# let f (type a) (type b) (x : a) = (x : b);;
Error: This expression has type a but an expression was expected of type b
The benefit of which is that you do not have f explicitely quantified as
opposed to the "let f : 'a 'b. ..." alternative. Feels more ocaml-ish to
me.
--
Guillaume Yziquel
--
Caml-list mailing list. Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs