moved injections back to state
This commit is contained in:
parent
c34041860d
commit
59676605cd
1 changed files with 18 additions and 17 deletions
|
|
@ -74,7 +74,7 @@ preRun (x : xs) = case x of
|
||||||
case M.lookup (coerce n) s of
|
case M.lookup (coerce n) s of
|
||||||
Nothing -> insertSig (coerce n) Nothing >> preRun xs
|
Nothing -> insertSig (coerce n) Nothing >> preRun xs
|
||||||
Just _ -> preRun xs
|
Just _ -> preRun xs
|
||||||
DData d@(Data t _) -> let collected = collect (collectTVars t) in checkData d collected >> preRun xs
|
DData d@(Data t _) -> collect (collectTVars t) >> checkData d >> preRun xs
|
||||||
where
|
where
|
||||||
-- Check if function body / signature has been declared already
|
-- Check if function body / signature has been declared already
|
||||||
duplicateDecl n env msg = when (coerce n `elem` env) (uncatchableErr msg)
|
duplicateDecl n env msg = when (coerce n `elem` env) (uncatchableErr msg)
|
||||||
|
|
@ -108,10 +108,10 @@ checkBind bind@(Bind name args e) = do
|
||||||
insertSig (coerce name) (Just lambda_t)
|
insertSig (coerce name) (Just lambda_t)
|
||||||
return (T.Bind (coerce name, lambda_t) [] (e, lambda_t))
|
return (T.Bind (coerce name, lambda_t) [] (e, lambda_t))
|
||||||
|
|
||||||
checkData :: (MonadReader Ctx m, Monad m, MonadError Error m) => Data -> m () -> m ()
|
checkData :: (MonadState Env m, Monad m, MonadError Error m) => Data -> m ()
|
||||||
checkData err@(Data typ injs) ma = do
|
checkData err@(Data typ injs) = do
|
||||||
(name, tvars) <- go typ
|
(name, tvars) <- go typ
|
||||||
dataErr (mapM_ (\i -> checkInj i name tvars ma) injs) err
|
dataErr (mapM_ (\i -> checkInj i name tvars) injs) err
|
||||||
where
|
where
|
||||||
go = \case
|
go = \case
|
||||||
TData name typs
|
TData name typs
|
||||||
|
|
@ -122,8 +122,8 @@ checkData err@(Data typ injs) ma = do
|
||||||
uncatchableErr $
|
uncatchableErr $
|
||||||
unwords ["Bad data type definition: ", printTree typ]
|
unwords ["Bad data type definition: ", printTree typ]
|
||||||
|
|
||||||
checkInj :: (MonadError Error m, MonadReader Ctx m, Monad m) => Inj -> UIdent -> [TVar] -> m a -> m a
|
checkInj :: (MonadError Error m, MonadState Env m, Monad m) => Inj -> UIdent -> [TVar] -> m ()
|
||||||
checkInj (Inj c inj_typ) name tvars ma
|
checkInj (Inj c inj_typ) name tvars
|
||||||
| Right False <- boundTVars tvars inj_typ =
|
| Right False <- boundTVars tvars inj_typ =
|
||||||
catchableErr "Unbound type variables"
|
catchableErr "Unbound type variables"
|
||||||
| TData name' typs <- returnType inj_typ
|
| TData name' typs <- returnType inj_typ
|
||||||
|
|
@ -138,7 +138,7 @@ checkInj (Inj c inj_typ) name tvars ma
|
||||||
"with type"
|
"with type"
|
||||||
quote $ printTree t
|
quote $ printTree t
|
||||||
"already exist"
|
"already exist"
|
||||||
Nothing -> insertInj (coerce c) inj_typ ma
|
Nothing -> insertInj (coerce c) inj_typ
|
||||||
| otherwise =
|
| otherwise =
|
||||||
uncatchableErr $
|
uncatchableErr $
|
||||||
unwords
|
unwords
|
||||||
|
|
@ -240,7 +240,7 @@ algoW = \case
|
||||||
"Unbound variable: "
|
"Unbound variable: "
|
||||||
<> printTree i
|
<> printTree i
|
||||||
EInj i -> do
|
EInj i -> do
|
||||||
constr <- asks injections
|
constr <- gets injections
|
||||||
case M.lookup (coerce i) constr of
|
case M.lookup (coerce i) constr of
|
||||||
Just t -> return (nullSubst, (T.EVar $ coerce i, t))
|
Just t -> return (nullSubst, (T.EVar $ coerce i, t))
|
||||||
Nothing ->
|
Nothing ->
|
||||||
|
|
@ -358,7 +358,7 @@ inferPattern = \case
|
||||||
let pvar = T.PVar (coerce x, fr)
|
let pvar = T.PVar (coerce x, fr)
|
||||||
return (pvar, fr)
|
return (pvar, fr)
|
||||||
PEnum p -> do
|
PEnum p -> do
|
||||||
t <- asks (M.lookup (coerce p) . injections)
|
t <- gets (M.lookup (coerce p) . injections)
|
||||||
t <-
|
t <-
|
||||||
maybeToRightM
|
maybeToRightM
|
||||||
( Error
|
( Error
|
||||||
|
|
@ -383,7 +383,7 @@ inferPattern = \case
|
||||||
frs <- mapM (const fresh) _ts
|
frs <- mapM (const fresh) _ts
|
||||||
return (T.PEnum $ coerce p, TData _data frs)
|
return (T.PEnum $ coerce p, TData _data frs)
|
||||||
PInj constr patterns -> do
|
PInj constr patterns -> do
|
||||||
t <- asks (M.lookup (coerce constr) . injections)
|
t <- gets (M.lookup (coerce constr) . injections)
|
||||||
t <-
|
t <-
|
||||||
maybeToRightM
|
maybeToRightM
|
||||||
( Error
|
( Error
|
||||||
|
|
@ -678,9 +678,9 @@ insertBind :: T.Ident -> Infer ()
|
||||||
insertBind i = modify (\st -> st{declaredBinds = S.insert i st.declaredBinds})
|
insertBind i = modify (\st -> st{declaredBinds = S.insert i st.declaredBinds})
|
||||||
|
|
||||||
-- | Insert a constructor into the start with its type
|
-- | Insert a constructor into the start with its type
|
||||||
insertInj :: (Monad m, MonadReader Ctx m) => T.Ident -> Type -> m a -> m a
|
insertInj :: (Monad m, MonadState Env m) => T.Ident -> Type -> m ()
|
||||||
insertInj i t =
|
insertInj i t =
|
||||||
local (\st -> st{injections = M.insert i t (injections st)})
|
modify (\st -> st{injections = M.insert i t (injections st)})
|
||||||
|
|
||||||
applySt :: Subst -> Infer a -> Infer a
|
applySt :: Subst -> Infer a -> Infer a
|
||||||
applySt s = local (\st -> st{vars = apply s st.vars})
|
applySt s = local (\st -> st{vars = apply s st.vars})
|
||||||
|
|
@ -688,8 +688,8 @@ applySt s = local (\st -> st{vars = apply s st.vars})
|
||||||
{- | Check if an injection (constructor of data type)
|
{- | Check if an injection (constructor of data type)
|
||||||
with an equivalent name has been declared already
|
with an equivalent name has been declared already
|
||||||
-}
|
-}
|
||||||
existInj :: (Monad m, MonadReader Ctx m) => T.Ident -> m (Maybe Type)
|
existInj :: (Monad m, MonadState Env m) => T.Ident -> m (Maybe Type)
|
||||||
existInj n = asks (M.lookup n . injections)
|
existInj n = gets (M.lookup n . injections)
|
||||||
|
|
||||||
setCurrentBind :: T.Ident -> Infer ()
|
setCurrentBind :: T.Ident -> Infer ()
|
||||||
setCurrentBind i = modify (\st -> st{currentBind = i})
|
setCurrentBind i = modify (\st -> st{currentBind = i})
|
||||||
|
|
@ -802,8 +802,8 @@ unzip4 =
|
||||||
)
|
)
|
||||||
([], [], [], [])
|
([], [], [], [])
|
||||||
|
|
||||||
initCtx = Ctx mempty mempty
|
initCtx = Ctx mempty
|
||||||
initEnv = Env 0 'a' mempty mempty "" mempty mempty mempty
|
initEnv = Env 0 'a' mempty mempty mempty "" mempty mempty mempty
|
||||||
|
|
||||||
run :: Infer a -> Either Error a
|
run :: Infer a -> Either Error a
|
||||||
run = run' initEnv initCtx
|
run = run' initEnv initCtx
|
||||||
|
|
@ -816,7 +816,7 @@ run' e c =
|
||||||
. flip evalStateT e
|
. flip evalStateT e
|
||||||
. runInfer
|
. runInfer
|
||||||
|
|
||||||
data Ctx = Ctx {vars :: Map T.Ident Type, injections :: Map T.Ident Type}
|
data Ctx = Ctx {vars :: Map T.Ident Type}
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
||||||
data Env = Env
|
data Env = Env
|
||||||
|
|
@ -824,6 +824,7 @@ data Env = Env
|
||||||
, nextChar :: Char
|
, nextChar :: Char
|
||||||
, sigs :: Map T.Ident (Maybe Type)
|
, sigs :: Map T.Ident (Maybe Type)
|
||||||
, takenTypeVars :: Set T.Ident
|
, takenTypeVars :: Set T.Ident
|
||||||
|
, injections :: Map T.Ident Type
|
||||||
, currentBind :: T.Ident
|
, currentBind :: T.Ident
|
||||||
, undecidedSigs :: Map T.Ident Type
|
, undecidedSigs :: Map T.Ident Type
|
||||||
, toDecide :: Set T.Ident
|
, toDecide :: Set T.Ident
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue