Types of OCaml functions -
Types of OCaml functions -
i started learning functional programming (ocaml), don't understand 1 of import topic functional programming: types.
can explain me solution please?
have test week , can't reach resolution..
let f b c = (a b c) 0;; f: ('a -> int -> 'a) -> 'a -> int -> 'a
let f b c = (a b c) 0;;
your confusion involves types , type inference, i.e., when define function or binding, don't need give explicit types parameters, nor function/binding itself, ocaml figure out if definition of function/binding correct.
so, let's manual inferences ourselves. if human can do, compiler can do.
1.
let x = 1
1
integer, x must integer. don't need int x = 1
in other languages, right?
2.
let f x = 1
if there multiple variable names between let
, =
, must function definition, right? otherwise, won't create sense. in java language, makes no sense int x y = 1
, right?
so f
function , x must parameter. since righthand side of =
integer, know f
homecoming integer. x, don't know, x
thought polymorphic type 'a
.
so f: 'a -> int = <fun>
.
3.
let f b c = (a b c) 0;;
f
function, parameters a
, b
, c
.
a
must function, because on righthand side of =
, function application.
a
takes 2 arguments: (a b c)
, 0. 0
integer, 2nd parameter of a
must integer type.
look within (a b c)
, c
2nd arguement, c
must integer.
we can't infer on b
. b
'a
.
since (a b c)
can 1st argument of a
, , (a b c)
application on function a
, homecoming type of a
have same type of b
'a
.
combine info above together, f: ('a -> int -> 'a) -> 'a -> int -> 'a
.
if want larn formally, https://realworldocaml.org/ friend.
ocaml
Comments
Post a Comment