-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e673bea
commit 015aa5e
Showing
8 changed files
with
182 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,42 @@ | ||
module Calc.Typecheck.Generalise (generalise) where | ||
|
||
import Calc.TypeUtils (bindType) | ||
import Calc.Typecheck.Types | ||
import Calc.Types.Type | ||
import Calc.Types.TypeVar | ||
import Control.Monad.State | ||
import qualified Data.Set as S | ||
import GHC.Natural | ||
|
||
import Calc.Typecheck.Types | ||
import Calc.Types.Type | ||
import Calc.Types.TypeVar | ||
import Calc.TypeUtils (mapType) | ||
import Control.Monad.State | ||
import qualified Data.HashMap.Strict as HM | ||
import qualified Data.Set as S | ||
import GHC.Natural | ||
|
||
-- get a nice new number | ||
freshUnificationVariable :: TypecheckM ann Natural | ||
freshUnificationVariable = do | ||
current <- gets tcsUnique | ||
modify (\tcs -> tcs {tcsUnique = current + 1}) | ||
pure current | ||
modify (\tcs -> tcs {tcsUnique = tcsUnique tcs + 1}) | ||
gets tcsUnique | ||
|
||
allFresh :: S.Set TypeVar -> TypecheckM ann (HM.HashMap TypeVar Natural) | ||
allFresh generics = | ||
let freshOne typeVar = | ||
HM.singleton typeVar <$> freshUnificationVariable | ||
in mconcat <$> traverse freshOne (S.toList generics) | ||
|
||
-- given a type, replace anything that should be generic with unification | ||
-- variables so that we know to replace them with types easily | ||
generalise :: S.Set TypeVar -> Type ann -> TypecheckM ann (Type ann) | ||
generalise generics (TVar ann var) | ||
| S.member var generics = | ||
TUnificationVar ann <$> freshUnificationVariable | ||
generalise generics other = bindType (generalise generics) other | ||
generalise generics ty | ||
= do | ||
fresh <- allFresh generics | ||
pure $ generaliseInternal fresh ty | ||
|
||
-- given a type, replace anything that should be generic with unification | ||
-- variables so that we know to replace them with types easily | ||
generaliseInternal :: HM.HashMap TypeVar Natural -> Type ann -> Type ann | ||
generaliseInternal fresh (TVar ann var) = | ||
case HM.lookup var fresh of | ||
Just nat -> | ||
TUnificationVar ann nat | ||
Nothing -> error "oh no generalise error" | ||
generaliseInternal fresh other = | ||
mapType (generaliseInternal fresh) other |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Calc.Typecheck.Substitute (substitute) where | ||
|
||
import Calc.Types.Type | ||
import Calc.TypeUtils | ||
import qualified Data.HashMap.Strict as HM | ||
import GHC.Natural | ||
|
||
substitute :: HM.HashMap Natural (Type ann) -> | ||
Type ann -> Type ann | ||
substitute subs (TUnificationVar _ nat) = | ||
case HM.lookup nat subs of | ||
Just ty -> ty | ||
Nothing -> error $ "Could not find unification var for " <> show nat | ||
substitute subs other | ||
= mapType (substitute subs) other |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.