functional programming - Type Inference in OCaml -
i'm trying wrap head around ocaml's type inference notation.
for example:
# let f x = x [];; val f : ('a list -> 'b) -> 'b = <fun>
makes sense me. val f takes in function x takes in list of 'a type , returns of 'b type. f returns 'b type since calls x.
however, once more arguments more confused.
# let g b c = b c;; val g : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c = <fun>
can assume if function has arguments, first parameters of type inference arguments? , if call b c, order ((a b) c) or (a (b c))?
# let rec h m n ls = match ls | [] -> n | x::t -> h m (m n x) t;; val h : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a = <fun>
as 1 i'm confused how ('a -> 'b -> 'a) -> 'a derived. can see 'b list corresponds ls variable , last 'a corresponds type of terminal symbol n.
can assume if function has arguments, first parameters of type inference arguments?
yes, first parameter of function type type of first argument.
and if call b c, order ((a b) c) or (a (b c))?
the order ((a b) c)
(you can think in simpler way)
as 1 i'm confused how ('a -> 'b -> 'a) -> 'a derived. can see 'b list corresponds ls variable , last 'a corresponds type of terminal symbol n.
you right. ls
has type 'b list
, n
has type 'a
.
let's think type of m
:
- you know
n
has type'a
, can derive(m n x)
has type'a
- you know
ls
has type'b list
, can derivex
has type'b
m
takes 2 arguments of type'a
, type'b
, , produces result of type'a
. so,m
has type'a -> 'b -> 'a
therefore, whole function has type ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a
Comments
Post a Comment