1
0
Fork 0
This repository has been archived on 2024-05-03. You can view files and clone it, but cannot push or open issues or pull requests.
unification-pfa/lib/unification.ml

20 lines
755 B
OCaml
Raw Normal View History

(** Unify 2 types and, if exists, returns the substitution *)
let rec unify ty1 ty2 =
match ty1, ty2 with
2024-04-11 11:18:14 +02:00
(* Same types *)
| Type.Int, Type.Int -> Some TypeSubstitution.empty
| Type.Var id1, Type.Var id2 when id1 = id2 -> Some TypeSubstitution.empty
(* Different types *)
| Type.Var id, ty | ty, Type.Var id -> Some (TypeSubstitution.singleton id ty)
| Type.Product (l1, r1), Type.Product (l2, r2) | Type.Arrow (l1, r1), Type.Arrow (l2, r2)
->
(match unify l1 l2 with
| Some s1 ->
2024-04-11 11:18:14 +02:00
(* Apply the substitution *)
(match unify (TypeSubstitution.apply s1 r1) (TypeSubstitution.apply s1 r2) with
| Some s2 -> Some (TypeSubstitution.compose s2 s1)
| None -> None)
| None -> None)
2024-04-11 11:18:14 +02:00
| _, _ -> None
;;