Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 01c9630

Browse files
committed
Merge branch 'types-as-syntax' into gen-x
2 parents e533091 + 4ccf31d commit 01c9630

File tree

1 file changed

+8
-35
lines changed

1 file changed

+8
-35
lines changed

semantic-core/src/Analysis/Typecheck.hs

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ import Control.Monad (unless)
1919
import Control.Monad.Module
2020
import qualified Data.Core as Core
2121
import Data.File
22-
import Data.Foldable (foldl', for_)
22+
import Data.Foldable (for_)
2323
import Data.Function (fix)
2424
import Data.Functor (($>))
2525
import qualified Data.IntMap as IntMap
2626
import qualified Data.IntSet as IntSet
2727
import Data.List.NonEmpty (nonEmpty)
2828
import Data.Loc
2929
import qualified Data.Map as Map
30-
import Data.Maybe (fromJust)
30+
import Data.Maybe (fromJust, fromMaybe)
3131
import Data.Name as Name
3232
import Data.Scope
3333
import qualified Data.Set as Set
@@ -101,7 +101,7 @@ runFile file = traverse run file
101101
where run
102102
= (\ m -> do
103103
(subst, t) <- m
104-
modify @(Heap User (Term Monotype Meta)) (substAll subst)
104+
modify @(Heap User (Term Monotype Meta)) (fmap (Set.map (substAll subst)))
105105
pure (substAll subst <$> t))
106106
. runState (mempty :: Substitution)
107107
. runReader (fileLoc file)
@@ -179,42 +179,15 @@ solve cs = for_ cs solve
179179
case sol of
180180
Just (_ := t1) -> solve (t1 :===: t2)
181181
Nothing | m1 `IntSet.member` mvs t2 -> fail ("Occurs check failure: " <> show m1 <> " :===: " <> show t2)
182-
| otherwise -> modify (IntMap.insert m1 t2 . subst (m1 := t2))
182+
| otherwise -> modify (IntMap.insert m1 t2 . fmap (substAll (IntMap.singleton m1 t2)))
183183
t1 :===: Var m2 -> solve (Var m2 :===: t1)
184184
t1 :===: t2 -> unless (t1 == t2) $ fail ("Type mismatch:\nexpected: " <> show t1 <> "\n actual: " <> show t2)
185185

186186
solution m = fmap (m :=) <$> gets (IntMap.lookup m)
187187

188-
substAll :: Substitutable t => Substitution -> t -> t
189-
substAll s a = foldl' (flip subst) a (map (uncurry (:=)) (IntMap.toList s))
190188

189+
mvs :: Foldable t => t Meta -> IntSet.IntSet
190+
mvs = foldMap IntSet.singleton
191191

192-
class FreeVariables t where
193-
mvs :: t -> IntSet.IntSet
194-
195-
instance FreeVariables (Term Monotype Meta) where
196-
mvs = foldMap IntSet.singleton
197-
198-
instance FreeVariables Constraint where
199-
mvs (t1 :===: t2) = mvs t1 <> mvs t2
200-
201-
class Substitutable t where
202-
subst :: Solution -> t -> t
203-
204-
instance Substitutable (Term Monotype Meta) where
205-
subst (i' := t') t = t >>= \ i -> if i == i' then t' else Var i
206-
207-
instance Substitutable Constraint where
208-
subst s (t1 :===: t2) = subst s t1 :===: subst s t2
209-
210-
instance Substitutable Solution where
211-
subst s (m := t) = m := subst s t
212-
213-
instance Substitutable a => Substitutable (IntMap.IntMap a) where
214-
subst s = IntMap.map (subst s)
215-
216-
instance (Ord a, Substitutable a) => Substitutable (Set.Set a) where
217-
subst s = Set.map (subst s)
218-
219-
instance Substitutable v => Substitutable (Map.Map k v) where
220-
subst s = fmap (subst s)
192+
substAll :: Monad t => IntMap.IntMap (t Meta) -> t Meta -> t Meta
193+
substAll s a = a >>= \ i -> fromMaybe (pure i) (IntMap.lookup i s)

0 commit comments

Comments
 (0)