Add a function to substitute all variables for their values

This commit is contained in:
Danila Fedorin 2023-11-25 23:19:36 -08:00
parent 3269e37ef3
commit ed34302627

View File

@ -1,4 +1,4 @@
module Bergamot.Syntax exposing (Term(..), instantiate, emptyInstantiationState, unify, emptyUnificationState)
module Bergamot.Syntax exposing (Term(..), instantiate, emptyInstantiationState, unify, emptyUnificationState, reify)
import Set exposing (Set)
import Dict exposing (Dict)
@ -120,3 +120,17 @@ unify t1 t2 us =
(Var v1, _) -> set v1 t2 us
(_, Var v2) -> set v2 t1 us
_ -> Nothing
reify : Term UnificationVar -> UnificationState -> Term UnificationVar
reify t us =
case t of
IntLit i -> IntLit i
StringLit s -> StringLit s
Call n ts -> Call n (List.map (\tp -> reify tp us) ts)
Var v ->
case Dict.get v us of
Just ui ->
case ui.term of
Just tp -> reify tp us
_ -> Var v
Nothing -> Var v