1
0
Fork 0

fix issue with variable handling

This commit is contained in:
Mylloon 2024-04-27 12:34:15 +02:00
parent fd68a06c08
commit ae73de8f64
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -1,11 +1,7 @@
(** Infer the type of a given term and, if exists, returns the type of the term *)
let typeof t =
let rec infer env = function
| Term.Var id ->
( (match TypeSubstitution.find id env with
| Some ty -> Some ty
| None -> Some (Type.Var id))
, env )
| Term.Var id -> TypeSubstitution.find id env, env
| Term.IntConst _ -> Some Type.Int, env
| Term.Binop (t1, _, t2) ->
(* Both operands must have type Int *)
@ -28,7 +24,8 @@ let typeof t =
| Term.Second -> Some ty2, env)
| _ -> None, env)
| Term.Fun (id, body) ->
(match infer env body with
let arg = TypeSubstitution.singleton id (Type.Var id) in
(match infer (TypeSubstitution.compose env arg) body with
| Some ty_body, env' ->
(match infer env' (Term.Var id) with
| Some ty, _ -> Some (Type.Arrow (ty, ty_body)), env'