문제

type 'a lazy_node = 
  | Empty
  | Node of 'a * 'a lazy_list
and 'a lazy_list = 'a lazy_node lazy_t

let con x zl = lazy (Node (x,zl))

So there should be two types defined here: 'a lazy_node and 'a lazy_list.

I thought that con's type would be 'a -> 'a lazy_list -> 'a lazy_list

However, actually when I tried it in utop, it gave con such a type:

val con : 'a -> 'a lazy_list -> 'a lazy_node lazy_t = <fun>


Why the return type is not 'a lazy_list? How does OCaml infer in this case?

도움이 되었습니까?

해결책

In your code lazy_list is not a new type but just an alias for lazy_node lazy_t. And the Ocaml type inferer only consider real types, not aliases. So it infers the real type and doesn't check if there is an alias for this type.

The alias is used for the argument because Node (x,zl) implies that zl has type 'a lazy_list.

You can add type annotation to enforce the use of the alias:

let con x zl: 'a lazy_list = lazy (Node (x,zl))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top