Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# type inferencing

I'm working with fsharp type inferencing and I'm trying to understand how they work. Why is it that

List.filter List.head

is type bool list list -> bool list list?

like image 485
coder4lyf Avatar asked Sep 29 '15 04:09

coder4lyf


1 Answers

List.filter has the type (just enter List.filter;; in FSI):

> List.filter;;
val it : (('a -> bool) -> 'a list -> 'a list)

so it takes a 'a -> bool and results in a 'a list -> 'a list

Now you feed it with

> List.head;;
val it : ('b list -> 'b)

(it's another 'a really so I renamed it) and now you have:

'a -> bool ~ 'b list -> 'b

you can unify this and see:

  • 'b ~ bool (from the right-hand-sides of ->)
  • 'a ~ 'b list ~ bool list (from the left-hand-side)

but this all together and you get the answer F#`s type-inference gives you:

'a list -> 'a list 
~ ('b list) list -> ('b list) list 
~ (bool list) list -> (bool list) list
~ bool list list -> bool list list
like image 134
Random Dev Avatar answered Oct 17 '22 10:10

Random Dev