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:

  1. you know n has type 'a, can derive (m n x) has type 'a
  2. you know ls has type 'b list, can derive x has type 'b
  3. 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

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -