You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

51 lines
1.3 KiB

open Lang
let fail s = Complice.Utils.failwith (Format.sprintf "type check: %s" s)
module type S = sig
val get_type : string -> Types.dddddml_type
end
module Check (M : S) = struct
let literal = function Unit -> Types.Unit | Bool _ -> Types.Bool
let const = function
| Literal l ->
Types.Primitive (literal l)
| Var x ->
M.get_type x
let check_type e t expected_t =
if t <> expected_t then
fail
(Format.asprintf
"expression %a has type %a but an expression of type %a was expected"
Pp.fprintf_expr e Pp.fprintf_type t Pp.fprintf_type expected_t)
let rec expr = function
| Const c ->
const c
| Bind (x, e, e') ->
let t = expr e in
let expected_t = M.get_type x in
check_type e t expected_t ; expr e'
| Abstract (p, e) ->
Types.Arrow (M.get_type p, expr e)
| Apply (e, e') -> (
let t = expr e in
let t' = expr e' in
match t with
| Types.Arrow (_, t_out) ->
check_type e t (Types.Arrow (t', t_out)) ;
t_out
| _ ->
fail
"function expected (should have been reported during type \
inference)" )
end
let file f tbl =
let module M = Check (struct
let get_type x = Hashtbl.find tbl x
end) in
ignore (M.expr f)