ocamlml

How can min-caml compiler got the type error?


I am learning https://github.com/esumii/min-caml

the example ack.ml under floder test:

let rec ack x y =
 if x <= 0 then y + 1 else
 if y <= 0 then ack (x - 1) 1 else
 ack (x - 1) (ack x (y - 1)) in
print_int (ack 3 10)

if I change the line

print_int (ack 3 10)

to this:

print_int (ack 3 1.23)

then make,got error:

Fatal error:exception Typing.Error(_,2,3)

I search the sorce,can't find info of Typing.Error,or Fatal.so how min-caml catch the type error?The ocaml compiler display in other way:

Error:this expression has type float but an expression was expected of type int

so where is the info

Fatal error:exception Typing.Error(_,2,3)

from?Thanks!


Solution

  • "Fatal error: exception ..." is the output you get for unhandled exceptions (in native executables at least, in the interpreter the message just says "Exception: ..."). So all that means is that a Typing.Error exception was raised and not handled.

    Typing.Error refers to the exception Error defined in the Typing module. You'll find its definition in typing.ml:

    exception Error of t * Type.t * Type.t
    

    It's raised on line 151 of typing.ml by adding the syntax tree to a previously raised Unify exception.

    Unify exceptions are raised at various points in the file when the type checker can't unify the actual and expected types.