On Sat, Sep 17, 2011 at 2:08 PM, Goswin von Brederlow <[email protected]> wrote:
> I think you are missing the point.
>
> I totaly get why it must be '_a instead if 'a. My question is why it
> doesn't infer the full type from the call to the print function.
You're correct, I was missing your point. You have the following situation:
let num_states =
List.fold_left
(fun num (sol, board) ->
let d = D.get_all sol in
let state = ([], sol, board)
in
Hashtbl.add cache board ();
states.(d) <- state::states.(d);
print state;
num + 1)
0
G.solutions
with the following inferred types:
states: ('_a list * (char * int * int) array * string) list array
print: (int * int * dir) list * (char * int * int) array * string -> unit
(Remark: the types given above are the result of calling
"caml-types-show-type" in an Emacs buffer, after compiling the fail
with -annot; we get partial type information even when the compilation
fail)
Why doesn't the type of "states" get inferred from the call to `print state`?
The reason why is that `state`, contrarily to `states`, is
polymorphic. It has type (caml-types...) 'b list * (char * int * int)
array * string, because the empty list [] has polymorphic type.
Therefore, it can be used in two different contexts with *different*
types (instantiations) that don't get unified with each other
states.(d) <- state::states.(d);
print state;
on the first line, the type 'b list of [] is instantiated to a fresh
type then unified with the '_a list of states, and on the second line
it is instantiated again, independently, and the resulting fresh type
is unified with (int * int * dir) list.
You can see that this is the problem by forcing the list to have a
monomorphic type:
let mono_list = ref [] in
let state = (!mono_list, sol, board)
With this (indeed unreadable) version, your whole code compiles fine.
Another technique to do that would be:
print (List.hd states.(d));
(I would still rather use a type annotation on the 'states' variable)
--
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