-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubst.hs
More file actions
32 lines (27 loc) · 914 Bytes
/
Subst.hs
File metadata and controls
32 lines (27 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{-# LANGUAGE LambdaCase #-}
module Subst
(
TypeSubst,
substExpr,
substType
) where
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Set (Set)
import qualified Data.Set as Set
import Expr
type TypeSubst = Map Var Type
substExpr :: TypeSubst -> Expr -> Expr
substExpr sub = \case
Var x -> Var x
Lam x t e -> Lam x (substType sub t) (substExpr sub e)
TLam a e -> TLam a (substExpr sub e)
App e e' -> App (substExpr sub e) (substExpr sub e')
TApp e t -> TApp (substExpr sub e) (substType sub t)
substType :: TypeSubst -> Type -> Type
substType sub = \case
TyVar a | Just t <- Map.lookup a sub -> t
| otherwise -> TyVar a
TyFun t t' -> TyFun (substType sub t) (substType sub t')
TyPoly a t | Map.notMember a sub -> TyPoly a (substType sub t)
| otherwise -> TyPoly a t