{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE NondecreasingIndentation #-}
{-# LANGUAGE OverloadedStrings #-}
module Disco.Typecheck where
import Control.Arrow ((&&&))
import Control.Lens ((^..))
import Control.Monad.Except
import Control.Monad.Trans.Maybe
import Data.Bifunctor (first)
import Data.Coerce
import qualified Data.Foldable as F
import Data.List (group, sort)
import Data.Map (Map)
import qualified Data.Map as M
import Data.Maybe (isJust)
import Data.Set (Set)
import qualified Data.Set as S
import Prelude as P hiding (lookup)
import Unbound.Generics.LocallyNameless (
Alpha,
Bind,
Name,
bind,
embed,
name2String,
string2Name,
substs,
unembed,
)
import Unbound.Generics.LocallyNameless.Unsafe (unsafeUnbind)
import Disco.Effects.Fresh
import Polysemy hiding (embed)
import Polysemy.Error
import Polysemy.Output
import Polysemy.Reader
import Polysemy.Writer
import Disco.AST.Surface
import Disco.AST.Typed
import Disco.Context hiding (filter)
import qualified Disco.Context as Ctx
import Disco.Messages
import Disco.Module
import Disco.Names
import Disco.Subst (applySubst)
import qualified Disco.Subst as Subst
import Disco.Syntax.Operators
import Disco.Syntax.Prims
import Disco.Typecheck.Constraints
import Disco.Typecheck.Util
import Disco.Types
import Disco.Types.Rules
containerTy :: Container -> Type -> Type
containerTy :: Container -> Type -> Type
containerTy Container
c Type
ty = Con -> [Type] -> Type
TyCon (Container -> Con
containerToCon Container
c) [Type
ty]
containerToCon :: Container -> Con
containerToCon :: Container -> Con
containerToCon Container
ListContainer = Con
CList
containerToCon Container
BagContainer = Con
CBag
containerToCon Container
SetContainer = Con
CSet
inferTelescope ::
(Alpha b, Alpha tyb, Member (Reader TyCtx) r) =>
(b -> Sem r (tyb, TyCtx)) ->
Telescope b ->
Sem r (Telescope tyb, TyCtx)
inferTelescope :: forall b tyb (r :: EffectRow).
(Alpha b, Alpha tyb, Member (Reader TyCtx) r) =>
(b -> Sem r (tyb, TyCtx))
-> Telescope b -> Sem r (Telescope tyb, TyCtx)
inferTelescope b -> Sem r (tyb, TyCtx)
inferOne Telescope b
tel = do
([tyb]
tel1, TyCtx
ctx) <- [b] -> Sem r ([tyb], TyCtx)
go (forall b. Alpha b => Telescope b -> [b]
fromTelescope Telescope b
tel)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall b. Alpha b => [b] -> Telescope b
toTelescope [tyb]
tel1, TyCtx
ctx)
where
go :: [b] -> Sem r ([tyb], TyCtx)
go [] = forall (m :: * -> *) a. Monad m => a -> m a
return ([], forall a b. Ctx a b
emptyCtx)
go (b
b : [b]
bs) = do
(tyb
tyb, TyCtx
ctx) <- b -> Sem r (tyb, TyCtx)
inferOne b
b
forall a b (r :: EffectRow) c.
Member (Reader (Ctx a b)) r =>
Ctx a b -> Sem r c -> Sem r c
extends TyCtx
ctx forall a b. (a -> b) -> a -> b
$ do
([tyb]
tybs, TyCtx
ctx') <- [b] -> Sem r ([tyb], TyCtx)
go [b]
bs
forall (m :: * -> *) a. Monad m => a -> m a
return (tyb
tyb forall a. a -> [a] -> [a]
: [tyb]
tybs, TyCtx
ctx forall a. Semigroup a => a -> a -> a
<> TyCtx
ctx')
checkModule ::
Members '[Output (Message ann), Reader TyCtx, Reader TyDefCtx, Error LocTCError, Fresh] r =>
ModuleName ->
Map ModuleName ModuleInfo ->
Module ->
Sem r ModuleInfo
checkModule :: forall ann (r :: EffectRow).
Members
'[Output (Message ann), Reader TyCtx, Reader TyDefCtx,
Error LocTCError, Fresh]
r =>
ModuleName
-> Map ModuleName ModuleInfo -> Module -> Sem r ModuleInfo
checkModule ModuleName
name Map ModuleName ModuleInfo
imports (Module Set Ext
es [String]
_ [Decl]
m [(Name Term, Docs)]
docs [Term]
terms) = do
let ([TypeDecl]
typeDecls, [TermDefn]
defns, [TypeDefn]
tydefs) = [Decl] -> ([TypeDecl], [TermDefn], [TypeDefn])
partitionDecls [Decl]
m
importTyCtx :: TyCtx
importTyCtx = forall a. Monoid a => [a] -> a
mconcat (Map ModuleName ModuleInfo
imports forall s a. s -> Getting (Endo [a]) s a -> [a]
^.. forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ModuleInfo TyCtx
miTys)
importTyDefnCtx :: TyDefCtx
importTyDefnCtx = forall (f :: * -> *) k a.
(Foldable f, Ord k) =>
f (Map k a) -> Map k a
M.unions (Map ModuleName ModuleInfo
imports forall s a. s -> Getting (Endo [a]) s a -> [a]
^.. forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ModuleInfo TyDefCtx
miTydefs)
TyDefCtx
tyDefnCtx <- forall e1 e2 (r :: EffectRow) a.
Member (Error e2) r =>
(e1 -> e2) -> Sem (Error e1 : r) a -> Sem r a
mapError TCError -> LocTCError
noLoc forall a b. (a -> b) -> a -> b
$ forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
[TypeDefn] -> Sem r TyDefCtx
makeTyDefnCtx [TypeDefn]
tydefs
forall (r :: EffectRow) a.
Member (Reader TyDefCtx) r =>
TyDefCtx -> Sem r a -> Sem r a
withTyDefns (TyDefCtx
tyDefnCtx forall k a. Ord k => Map k a -> Map k a -> Map k a
`M.union` TyDefCtx
importTyDefnCtx) forall a b. (a -> b) -> a -> b
$ do
TyCtx
tyCtx <- forall e1 e2 (r :: EffectRow) a.
Member (Error e2) r =>
(e1 -> e2) -> Sem (Error e1 : r) a -> Sem r a
mapError TCError -> LocTCError
noLoc forall a b. (a -> b) -> a -> b
$ forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
ModuleName -> [TypeDecl] -> Sem r TyCtx
makeTyCtx ModuleName
name [TypeDecl]
typeDecls
forall a b (r :: EffectRow) c.
Member (Reader (Ctx a b)) r =>
Ctx a b -> Sem r c -> Sem r c
extends TyCtx
importTyCtx forall a b. (a -> b) -> a -> b
$ forall a b (r :: EffectRow) c.
Member (Reader (Ctx a b)) r =>
Ctx a b -> Sem r c -> Sem r c
extends TyCtx
tyCtx forall a b. (a -> b) -> a -> b
$ do
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error LocTCError] r =>
ModuleName -> TypeDefn -> Sem r ()
checkTyDefn ModuleName
name) [TypeDefn]
tydefs
[Defn]
adefns <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall ann (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Error LocTCError, Fresh,
Output (Message ann)]
r =>
ModuleName -> TermDefn -> Sem r Defn
checkDefn ModuleName
name) [TermDefn]
defns
let defnCtx :: Ctx ATerm Defn
defnCtx = forall a b. ModuleName -> [(Name a, b)] -> Ctx a b
ctxForModule ModuleName
name (forall a b. (a -> b) -> [a] -> [b]
map (Defn -> Name ATerm
getDefnName forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& forall a. a -> a
id) [Defn]
adefns)
docCtx :: Ctx Term Docs
docCtx = forall a b. ModuleName -> [(Name a, b)] -> Ctx a b
ctxForModule ModuleName
name [(Name Term, Docs)]
docs
dups :: [Name ATerm]
dups = forall a. Ord a => [a] -> [a]
filterDups forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Defn -> Name ATerm
getDefnName forall a b. (a -> b) -> a -> b
$ [Defn]
adefns
case [Name ATerm]
dups of
(Name ATerm
x : [Name ATerm]
_) -> forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw forall a b. (a -> b) -> a -> b
$ TCError -> LocTCError
noLoc forall a b. (a -> b) -> a -> b
$ Name Term -> TCError
DuplicateDefns (coerce :: forall a b. Coercible a b => a -> b
coerce Name ATerm
x)
[] -> do
Ctx ATerm [ATerm]
aprops <- forall e1 e2 (r :: EffectRow) a.
Member (Error e2) r =>
(e1 -> e2) -> Sem (Error e1 : r) a -> Sem r a
mapError TCError -> LocTCError
noLoc forall a b. (a -> b) -> a -> b
$ forall ann (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Error TCError, Fresh,
Output (Message ann)]
r =>
Ctx Term Docs -> Sem r (Ctx ATerm [ATerm])
checkProperties Ctx Term Docs
docCtx
[(ATerm, PolyType)]
aterms <- forall e1 e2 (r :: EffectRow) a.
Member (Error e2) r =>
(e1 -> e2) -> Sem (Error e1 : r) a -> Sem r a
mapError TCError -> LocTCError
noLoc forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall ann (r :: EffectRow).
Members
'[Output (Message ann), Reader TyCtx, Reader TyDefCtx,
Error TCError, Fresh]
r =>
Term -> Sem r (ATerm, PolyType)
inferTop [Term]
terms
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ ModuleName
-> Map ModuleName ModuleInfo
-> [QName Term]
-> Ctx Term Docs
-> Ctx ATerm [ATerm]
-> TyCtx
-> TyDefCtx
-> Ctx ATerm Defn
-> [(ATerm, PolyType)]
-> Set Ext
-> ModuleInfo
ModuleInfo ModuleName
name Map ModuleName ModuleInfo
imports (forall a b. (a -> b) -> [a] -> [b]
map ((ModuleName
name forall a. ModuleName -> Name a -> QName a
.-) forall b c a. (b -> c) -> (a -> b) -> a -> c
. TypeDecl -> Name Term
getDeclName) [TypeDecl]
typeDecls) Ctx Term Docs
docCtx Ctx ATerm [ATerm]
aprops TyCtx
tyCtx TyDefCtx
tyDefnCtx Ctx ATerm Defn
defnCtx [(ATerm, PolyType)]
aterms Set Ext
es
where
getDefnName :: Defn -> Name ATerm
getDefnName :: Defn -> Name ATerm
getDefnName (Defn Name ATerm
n [Type]
_ Type
_ [Clause]
_) = Name ATerm
n
getDeclName :: TypeDecl -> Name Term
getDeclName :: TypeDecl -> Name Term
getDeclName (TypeDecl Name Term
n PolyType
_) = Name Term
n
makeTyDefnCtx :: Members '[Reader TyDefCtx, Error TCError] r => [TypeDefn] -> Sem r TyDefCtx
makeTyDefnCtx :: forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
[TypeDefn] -> Sem r TyDefCtx
makeTyDefnCtx [TypeDefn]
tydefs = do
TyDefCtx
oldTyDefs <- forall i (r :: EffectRow). Member (Reader i) r => Sem r i
ask @TyDefCtx
let oldNames :: [String]
oldNames = forall k a. Map k a -> [k]
M.keys TyDefCtx
oldTyDefs
newNames :: [String]
newNames = forall a b. (a -> b) -> [a] -> [b]
map (\(TypeDefn String
x [String]
_ Type
_) -> String
x) [TypeDefn]
tydefs
dups :: [String]
dups = forall a. Ord a => [a] -> [a]
filterDups forall a b. (a -> b) -> a -> b
$ [String]
newNames forall a. [a] -> [a] -> [a]
++ [String]
oldNames
let convert :: TypeDefn -> (String, TyDefBody)
convert (TypeDefn String
x [String]
args Type
body) =
(String
x, [String] -> ([Type] -> Type) -> TyDefBody
TyDefBody [String]
args (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall b a. Subst b a => [(Name b, b)] -> a -> a
substs Type
body forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. [a] -> [b] -> [(a, b)]
zip (forall a b. (a -> b) -> [a] -> [b]
map forall a. String -> Name a
string2Name [String]
args)))
case [String]
dups of
(String
x : [String]
_) -> forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw (String -> TCError
DuplicateTyDefns String
x)
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Ord k => [(k, a)] -> Map k a
M.fromList forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map TypeDefn -> (String, TyDefBody)
convert [TypeDefn]
tydefs
checkTyDefn :: Members '[Reader TyDefCtx, Error LocTCError] r => ModuleName -> TypeDefn -> Sem r ()
checkTyDefn :: forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error LocTCError] r =>
ModuleName -> TypeDefn -> Sem r ()
checkTyDefn ModuleName
name defn :: TypeDefn
defn@(TypeDefn String
x [String]
args Type
body) = forall e1 e2 (r :: EffectRow) a.
Member (Error e2) r =>
(e1 -> e2) -> Sem (Error e1 : r) a -> Sem r a
mapError (Maybe (QName Term) -> TCError -> LocTCError
LocTCError (forall a. a -> Maybe a
Just (ModuleName
name forall a. ModuleName -> Name a -> QName a
.- forall a. String -> Name a
string2Name String
x))) forall a b. (a -> b) -> a -> b
$ do
forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
Type -> Sem r ()
checkTypeValid Type
body
Set String
_ <- forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
Type -> Set String -> Sem r (Set String)
checkCyclicTy (String -> [Type] -> Type
TyUser String
x (forall a b. (a -> b) -> [a] -> [b]
map (Name Type -> Type
TyVar forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. String -> Name a
string2Name) [String]
args)) forall a. Set a
S.empty
forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
TypeDefn -> Sem r ()
checkUnboundVars TypeDefn
defn
forall (r :: EffectRow).
Member (Error TCError) r =>
TypeDefn -> Sem r ()
checkPolyRec TypeDefn
defn
checkCyclicTy :: Members '[Reader TyDefCtx, Error TCError] r => Type -> Set String -> Sem r (Set String)
checkCyclicTy :: forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
Type -> Set String -> Sem r (Set String)
checkCyclicTy (TyUser String
name [Type]
args) Set String
set = do
case forall a. Ord a => a -> Set a -> Bool
S.member String
name Set String
set of
Bool
True -> forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw forall a b. (a -> b) -> a -> b
$ String -> TCError
CyclicTyDef String
name
Bool
False -> do
Type
ty <- forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
String -> [Type] -> Sem r Type
lookupTyDefn String
name [Type]
args
forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
Type -> Set String -> Sem r (Set String)
checkCyclicTy Type
ty (forall a. Ord a => a -> Set a -> Set a
S.insert String
name Set String
set)
checkCyclicTy Type
_ Set String
set = forall (m :: * -> *) a. Monad m => a -> m a
return Set String
set
checkUnboundVars :: Members '[Reader TyDefCtx, Error TCError] r => TypeDefn -> Sem r ()
checkUnboundVars :: forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
TypeDefn -> Sem r ()
checkUnboundVars (TypeDefn String
_ [String]
args Type
body) = Type -> Sem r ()
go Type
body
where
go :: Type -> Sem r ()
go (TyAtom (AVar (U Name Type
x)))
| forall a. Name a -> String
name2String Name Type
x forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
args = forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw forall a b. (a -> b) -> a -> b
$ Name Type -> TCError
UnboundTyVar Name Type
x
go (TyAtom Atom
_) = forall (m :: * -> *) a. Monad m => a -> m a
return ()
go (TyUser String
name [Type]
tys) = forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
String -> [Type] -> Sem r Type
lookupTyDefn String
name [Type]
tys forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Type -> Sem r ()
go [Type]
tys
go (TyCon Con
_ [Type]
tys) = forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Type -> Sem r ()
go [Type]
tys
checkPolyRec :: Member (Error TCError) r => TypeDefn -> Sem r ()
checkPolyRec :: forall (r :: EffectRow).
Member (Error TCError) r =>
TypeDefn -> Sem r ()
checkPolyRec (TypeDefn String
name [String]
args Type
body) = Type -> Sem r ()
go Type
body
where
go :: Type -> Sem r ()
go (TyCon (CUser String
x) [Type]
tys)
| String
x forall a. Eq a => a -> a -> Bool
== String
name Bool -> Bool -> Bool
&& Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Type -> Bool
isTyVar [Type]
tys) =
forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw forall a b. (a -> b) -> a -> b
$ String -> [String] -> [Type] -> TCError
NoPolyRec String
name [String]
args [Type]
tys
| Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return ()
go (TyCon Con
_ [Type]
tys) = forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Type -> Sem r ()
go [Type]
tys
go Type
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()
filterDups :: Ord a => [a] -> [a]
filterDups :: forall a. Ord a => [a] -> [a]
filterDups = forall a b. (a -> b) -> [a] -> [b]
map forall a. [a] -> a
head forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter ((forall a. Ord a => a -> a -> Bool
> Int
1) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Int
length) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => [a] -> [[a]]
group forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => [a] -> [a]
sort
makeTyCtx :: Members '[Reader TyDefCtx, Error TCError] r => ModuleName -> [TypeDecl] -> Sem r TyCtx
makeTyCtx :: forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
ModuleName -> [TypeDecl] -> Sem r TyCtx
makeTyCtx ModuleName
name [TypeDecl]
decls = do
let dups :: [Name Term]
dups = forall a. Ord a => [a] -> [a]
filterDups forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (\(TypeDecl Name Term
x PolyType
_) -> Name Term
x) forall a b. (a -> b) -> a -> b
$ [TypeDecl]
decls
case [Name Term]
dups of
(Name Term
x : [Name Term]
_) -> forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw (Name Term -> TCError
DuplicateDecls Name Term
x)
[] -> do
forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
TyCtx -> Sem r ()
checkCtx TyCtx
declCtx
forall (m :: * -> *) a. Monad m => a -> m a
return TyCtx
declCtx
where
declCtx :: TyCtx
declCtx = forall a b. ModuleName -> [(Name a, b)] -> Ctx a b
ctxForModule ModuleName
name forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\(TypeDecl Name Term
x PolyType
ty) -> (Name Term
x, PolyType
ty)) [TypeDecl]
decls
checkCtx :: Members '[Reader TyDefCtx, Error TCError] r => TyCtx -> Sem r ()
checkCtx :: forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
TyCtx -> Sem r ()
checkCtx = forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
PolyType -> Sem r ()
checkPolyTyValid forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Ctx a b -> [b]
Ctx.elems
checkDefn ::
Members '[Reader TyCtx, Reader TyDefCtx, Error LocTCError, Fresh, Output (Message ann)] r =>
ModuleName ->
TermDefn ->
Sem r Defn
checkDefn :: forall ann (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Error LocTCError, Fresh,
Output (Message ann)]
r =>
ModuleName -> TermDefn -> Sem r Defn
checkDefn ModuleName
name (TermDefn Name Term
x [Bind [Pattern] Term]
clauses) = forall e1 e2 (r :: EffectRow) a.
Member (Error e2) r =>
(e1 -> e2) -> Sem (Error e1 : r) a -> Sem r a
mapError (Maybe (QName Term) -> TCError -> LocTCError
LocTCError (forall a. a -> Maybe a
Just (ModuleName
name forall a. ModuleName -> Name a -> QName a
.- Name Term
x))) forall a b. (a -> b) -> a -> b
$ do
[Bind [Pattern] Term] -> Sem (Error TCError : r) ()
checkNumPats [Bind [Pattern] Term]
clauses
Forall Bind [Name Type] Type
sig <- forall a b (r :: EffectRow).
Member (Reader (Ctx a b)) r =>
QName a -> Sem r (Maybe b)
lookup (ModuleName
name forall a. ModuleName -> Name a -> QName a
.- Name Term
x) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw forall a b. (a -> b) -> a -> b
$ Name Term -> TCError
NoType Name Term
x) forall (m :: * -> *) a. Monad m => a -> m a
return
([Name Type]
nms, Type
ty) <- forall (r :: EffectRow) p t.
(Member Fresh r, Alpha p, Alpha t) =>
Bind p t -> Sem r (p, t)
unbind Bind [Name Type] Type
sig
([Type]
patTys, Type
bodyTy) <- forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
Int -> Type -> Sem r ([Type], Type)
decomposeDefnTy (Bind [Pattern] Term -> Int
numPats (forall a. [a] -> a
head [Bind [Pattern] Term]
clauses)) Type
ty
(([Clause]
acs, Type
_), S
theta) <- forall ann (r :: EffectRow) a.
Members
'[Reader TyDefCtx, Error TCError, Output (Message ann)] r =>
Sem (Writer Constraint : r) a -> Sem r (a, S)
solve forall a b. (a -> b) -> a -> b
$ do
[Clause]
aclauses <- forall (r :: EffectRow) a.
Member (Writer Constraint) r =>
[Name Type] -> Sem r a -> Sem r a
forAll [Name Type]
nms forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
[Type] -> Type -> Bind [Pattern] Term -> Sem r Clause
checkClause [Type]
patTys Type
bodyTy) [Bind [Pattern] Term]
clauses
forall (m :: * -> *) a. Monad m => a -> m a
return ([Clause]
aclauses, Type
ty)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall b a. Subst b a => Substitution b -> a -> a
applySubst S
theta (Name ATerm -> [Type] -> Type -> [Clause] -> Defn
Defn (coerce :: forall a b. Coercible a b => a -> b
coerce Name Term
x) [Type]
patTys Type
bodyTy [Clause]
acs)
where
numPats :: Bind [Pattern] Term -> Int
numPats = forall (t :: * -> *) a. Foldable t => t a -> Int
length forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall p t. (Alpha p, Alpha t) => Bind p t -> (p, t)
unsafeUnbind
checkNumPats :: [Bind [Pattern] Term] -> Sem (Error TCError : r) ()
checkNumPats [] = forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkNumPats [Bind [Pattern] Term
_] = forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkNumPats (Bind [Pattern] Term
c : [Bind [Pattern] Term]
cs)
| forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((forall a. Eq a => a -> a -> Bool
== Int
0) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bind [Pattern] Term -> Int
numPats) (Bind [Pattern] Term
c forall a. a -> [a] -> [a]
: [Bind [Pattern] Term]
cs) = forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw (Name Term -> TCError
DuplicateDefns Name Term
x)
| Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((forall a. Eq a => a -> a -> Bool
== Bind [Pattern] Term -> Int
numPats Bind [Pattern] Term
c) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bind [Pattern] Term -> Int
numPats) [Bind [Pattern] Term]
cs) = forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw TCError
NumPatterns
| Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkClause ::
Members '[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
[Type] ->
Type ->
Bind [Pattern] Term ->
Sem r Clause
checkClause :: forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
[Type] -> Type -> Bind [Pattern] Term -> Sem r Clause
checkClause [Type]
patTys Type
bodyTy Bind [Pattern] Term
clause = do
([Pattern]
pats, Term
body) <- forall (r :: EffectRow) p t.
(Member Fresh r, Alpha p, Alpha t) =>
Bind p t -> Sem r (p, t)
unbind Bind [Pattern] Term
clause
([TyCtx]
ctxs, [APattern]
aps) <- forall a b. [(a, b)] -> ([a], [b])
unzip forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern [Pattern]
pats [Type]
patTys
ATerm
at <- forall a b (r :: EffectRow) c.
Member (Reader (Ctx a b)) r =>
Ctx a b -> Sem r c -> Sem r c
extends (forall a. Monoid a => [a] -> a
mconcat [TyCtx]
ctxs) forall a b. (a -> b) -> a -> b
$ forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Type -> Sem r ATerm
check Term
body Type
bodyTy
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall p t. (Alpha p, Alpha t) => p -> t -> Bind p t
bind [APattern]
aps ATerm
at
decomposeDefnTy :: Members '[Reader TyDefCtx, Error TCError] r => Int -> Type -> Sem r ([Type], Type)
decomposeDefnTy :: forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
Int -> Type -> Sem r ([Type], Type)
decomposeDefnTy Int
0 Type
ty = forall (m :: * -> *) a. Monad m => a -> m a
return ([], Type
ty)
decomposeDefnTy Int
n (TyUser String
tyName [Type]
args) = forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
String -> [Type] -> Sem r Type
lookupTyDefn String
tyName [Type]
args forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
Int -> Type -> Sem r ([Type], Type)
decomposeDefnTy Int
n
decomposeDefnTy Int
n (Type
ty1 :->: Type
ty2) = forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (Type
ty1 forall a. a -> [a] -> [a]
:) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
Int -> Type -> Sem r ([Type], Type)
decomposeDefnTy (Int
n forall a. Num a => a -> a -> a
- Int
1) Type
ty2
decomposeDefnTy Int
_n Type
_ty = forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw TCError
NumPatterns
checkProperties ::
Members '[Reader TyCtx, Reader TyDefCtx, Error TCError, Fresh, Output (Message ann)] r =>
Ctx Term Docs ->
Sem r (Ctx ATerm [AProperty])
checkProperties :: forall ann (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Error TCError, Fresh,
Output (Message ann)]
r =>
Ctx Term Docs -> Sem r (Ctx ATerm [ATerm])
checkProperties Ctx Term Docs
docs =
forall a1 b a2. Ctx a1 b -> Ctx a2 b
Ctx.coerceKeys forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. (b -> Bool) -> Ctx a b -> Ctx a b
Ctx.filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Bool
P.null)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse) forall ann (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Error TCError, Fresh,
Output (Message ann)]
r =>
Term -> Sem r ATerm
checkProperty Ctx Term [Term]
properties
where
properties :: Ctx Term [Property]
properties :: Ctx Term [Term]
properties = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Docs
ds -> [Term
p | DocProperty Term
p <- Docs
ds]) Ctx Term Docs
docs
checkProperty ::
Members '[Reader TyCtx, Reader TyDefCtx, Error TCError, Fresh, Output (Message ann)] r =>
Property ->
Sem r AProperty
checkProperty :: forall ann (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Error TCError, Fresh,
Output (Message ann)]
r =>
Term -> Sem r ATerm
checkProperty Term
prop = do
(ATerm
at, S
theta) <- forall ann (r :: EffectRow) a.
Members
'[Reader TyDefCtx, Error TCError, Output (Message ann)] r =>
Sem (Writer Constraint : r) a -> Sem r (a, S)
solve forall a b. (a -> b) -> a -> b
$ forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Type -> Sem r ATerm
check Term
prop Type
TyProp
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall b a. Subst b a => Substitution b -> a -> a
applySubst S
theta ATerm
at
checkPolyTyValid :: Members '[Reader TyDefCtx, Error TCError] r => PolyType -> Sem r ()
checkPolyTyValid :: forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
PolyType -> Sem r ()
checkPolyTyValid (Forall Bind [Name Type] Type
b) = do
let ([Name Type]
_, Type
ty) = forall p t. (Alpha p, Alpha t) => Bind p t -> (p, t)
unsafeUnbind Bind [Name Type] Type
b
forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
Type -> Sem r ()
checkTypeValid Type
ty
checkTypeValid :: Members '[Reader TyDefCtx, Error TCError] r => Type -> Sem r ()
checkTypeValid :: forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
Type -> Sem r ()
checkTypeValid (TyAtom Atom
_) = forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkTypeValid (TyCon Con
c [Type]
tys) = do
Int
k <- forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
Con -> Sem r Int
conArity Con
c
if
| Int
n forall a. Ord a => a -> a -> Bool
< Int
k -> forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw (Con -> TCError
NotEnoughArgs Con
c)
| Int
n forall a. Ord a => a -> a -> Bool
> Int
k -> forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw (Con -> TCError
TooManyArgs Con
c)
| Bool
otherwise -> forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
Type -> Sem r ()
checkTypeValid [Type]
tys
where
n :: Int
n = forall (t :: * -> *) a. Foldable t => t a -> Int
length [Type]
tys
conArity :: Members '[Reader TyDefCtx, Error TCError] r => Con -> Sem r Int
conArity :: forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
Con -> Sem r Int
conArity (CContainer Atom
_) = forall (m :: * -> *) a. Monad m => a -> m a
return Int
1
conArity Con
CGraph = forall (m :: * -> *) a. Monad m => a -> m a
return Int
1
conArity (CUser String
name) = do
TyDefCtx
d <- forall i (r :: EffectRow). Member (Reader i) r => Sem r i
ask @TyDefCtx
case forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup String
name TyDefCtx
d of
Maybe TyDefBody
Nothing -> forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw (String -> TCError
NotTyDef String
name)
Just (TyDefBody [String]
as [Type] -> Type
_) -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall (t :: * -> *) a. Foldable t => t a -> Int
length [String]
as)
conArity Con
_ = forall (m :: * -> *) a. Monad m => a -> m a
return Int
2
data Mode = Infer | Check Type
deriving (Int -> Mode -> ShowS
[Mode] -> ShowS
Mode -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Mode] -> ShowS
$cshowList :: [Mode] -> ShowS
show :: Mode -> String
$cshow :: Mode -> String
showsPrec :: Int -> Mode -> ShowS
$cshowsPrec :: Int -> Mode -> ShowS
Show)
check ::
Members '[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Term ->
Type ->
Sem r ATerm
check :: forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Type -> Sem r ATerm
check Term
t Type
ty = forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Mode -> Term -> Sem r ATerm
typecheck (Type -> Mode
Check Type
ty) Term
t
checkPolyTy ::
Members '[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Term ->
PolyType ->
Sem r ATerm
checkPolyTy :: forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> PolyType -> Sem r ATerm
checkPolyTy Term
t (Forall Bind [Name Type] Type
sig) = do
([Name Type]
as, Type
tau) <- forall (r :: EffectRow) p t.
(Member Fresh r, Alpha p, Alpha t) =>
Bind p t -> Sem r (p, t)
unbind Bind [Name Type] Type
sig
(ATerm
at, Constraint
cst) <- forall (r :: EffectRow) a.
Sem (Writer Constraint : r) a -> Sem r (a, Constraint)
withConstraint forall a b. (a -> b) -> a -> b
$ forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Type -> Sem r ATerm
check Term
t Type
tau
case [Name Type]
as of
[] -> forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint Constraint
cst
[Name Type]
_ -> forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Bind [Name Type] Constraint -> Constraint
CAll (forall p t. (Alpha p, Alpha t) => p -> t -> Bind p t
bind [Name Type]
as Constraint
cst)
forall (m :: * -> *) a. Monad m => a -> m a
return ATerm
at
infer ::
Members '[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Term ->
Sem r ATerm
infer :: forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Sem r ATerm
infer = forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Mode -> Term -> Sem r ATerm
typecheck Mode
Infer
inferTop ::
Members '[Output (Message ann), Reader TyCtx, Reader TyDefCtx, Error TCError, Fresh] r =>
Term ->
Sem r (ATerm, PolyType)
inferTop :: forall ann (r :: EffectRow).
Members
'[Output (Message ann), Reader TyCtx, Reader TyDefCtx,
Error TCError, Fresh]
r =>
Term -> Sem r (ATerm, PolyType)
inferTop Term
t = do
(ATerm
at, S
theta) <- forall ann (r :: EffectRow) a.
Members
'[Reader TyDefCtx, Error TCError, Output (Message ann)] r =>
Sem (Writer Constraint : r) a -> Sem r (a, S)
solve forall a b. (a -> b) -> a -> b
$ forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Sem r ATerm
infer Term
t
forall ann (r :: EffectRow).
Member (Output (Message ann)) r =>
Sem r (Doc ann) -> Sem r ()
debug Sem r (Doc ann)
"Final annotated term (before substitution and container monomorphizing):"
forall ann (r :: EffectRow) t.
(Member (Output (Message ann)) r, Pretty t) =>
t -> Sem r ()
debugPretty ATerm
at
let at' :: ATerm
at' = forall b a. Subst b a => Substitution b -> a -> a
applySubst S
theta ATerm
at
cvs :: Set (Name Type)
cvs = Type -> Set (Name Type)
containerVars (forall t. HasType t => t -> Type
getType ATerm
at')
at'' :: ATerm
at'' = forall b a. Subst b a => Substitution b -> a -> a
applySubst (forall a. [(Name a, a)] -> Substitution a
Subst.fromList forall a b. (a -> b) -> a -> b
$ forall a b. [a] -> [b] -> [(a, b)]
zip (forall a. Set a -> [a]
S.toList Set (Name Type)
cvs) (forall a. a -> [a]
repeat (Atom -> Type
TyAtom (BaseTy -> Atom
ABase BaseTy
CtrList)))) ATerm
at'
forall (m :: * -> *) a. Monad m => a -> m a
return (ATerm
at'', Type -> PolyType
closeType (forall t. HasType t => t -> Type
getType ATerm
at''))
checkTop ::
Members '[Output (Message ann), Reader TyCtx, Reader TyDefCtx, Error TCError, Fresh] r =>
Term ->
PolyType ->
Sem r ATerm
checkTop :: forall ann (r :: EffectRow).
Members
'[Output (Message ann), Reader TyCtx, Reader TyDefCtx,
Error TCError, Fresh]
r =>
Term -> PolyType -> Sem r ATerm
checkTop Term
t PolyType
ty = do
(ATerm
at, S
theta) <- forall ann (r :: EffectRow) a.
Members
'[Reader TyDefCtx, Error TCError, Output (Message ann)] r =>
Sem (Writer Constraint : r) a -> Sem r (a, S)
solve forall a b. (a -> b) -> a -> b
$ forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> PolyType -> Sem r ATerm
checkPolyTy Term
t PolyType
ty
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall b a. Subst b a => Substitution b -> a -> a
applySubst S
theta ATerm
at
typecheck ::
Members '[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Mode ->
Term ->
Sem r ATerm
typecheck :: forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Mode -> Term -> Sem r ATerm
typecheck (Check (TyUser String
name [Type]
args)) Term
t = forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
String -> [Type] -> Sem r Type
lookupTyDefn String
name [Type]
args forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Type -> Sem r ATerm
check Term
t
typecheck Mode
mode (TParens Term
t) = forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Mode -> Term -> Sem r ATerm
typecheck Mode
mode Term
t
typecheck Mode
Infer (TVar Name Term
x) = do
Maybe ATerm
mt <- forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
F.asum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT forall a b. (a -> b) -> a -> b
$ [Sem r (Maybe ATerm)
tryLocal, Sem r (Maybe ATerm)
tryModule, Sem r (Maybe ATerm)
tryPrim]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw (Name Term -> TCError
Unbound Name Term
x)) forall (m :: * -> *) a. Monad m => a -> m a
return Maybe ATerm
mt
where
tryLocal :: Sem r (Maybe ATerm)
tryLocal = do
Maybe PolyType
mty <- forall a b (r :: EffectRow).
Member (Reader (Ctx a b)) r =>
QName a -> Sem r (Maybe b)
Ctx.lookup (forall a. Name a -> QName a
localName Name Term
x)
case Maybe PolyType
mty of
Just (Forall Bind [Name Type] Type
sig) -> do
([Name Type]
_, Type
ty) <- forall (r :: EffectRow) p t.
(Member Fresh r, Alpha p, Alpha t) =>
Bind p t -> Sem r (p, t)
unbind Bind [Name Type] Type
sig
forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Type -> QName ATerm -> ATerm
ATVar Type
ty (forall a. Name a -> QName a
localName (coerce :: forall a b. Coercible a b => a -> b
coerce Name Term
x))
Maybe PolyType
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
tryModule :: Sem r (Maybe ATerm)
tryModule = do
[(ModuleName, PolyType)]
bs <- forall a b (r :: EffectRow).
Member (Reader (Ctx a b)) r =>
Name a -> Sem r [(ModuleName, b)]
Ctx.lookupNonLocal Name Term
x
case [(ModuleName, PolyType)]
bs of
[(ModuleName
m, Forall Bind [Name Type] Type
sig)] -> do
([Name Type]
_, Type
ty) <- forall (r :: EffectRow) p t.
(Member Fresh r, Alpha p, Alpha t) =>
Bind p t -> Sem r (p, t)
unbind Bind [Name Type] Type
sig
forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Type -> QName ATerm -> ATerm
ATVar Type
ty (ModuleName
m forall a. ModuleName -> Name a -> QName a
.- coerce :: forall a b. Coercible a b => a -> b
coerce Name Term
x)
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
[(ModuleName, PolyType)]
_ -> forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw forall a b. (a -> b) -> a -> b
$ Name Term -> [ModuleName] -> TCError
Ambiguous Name Term
x (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(ModuleName, PolyType)]
bs)
tryPrim :: Sem r (Maybe ATerm)
tryPrim =
case String -> [Prim]
toPrim (forall a. Name a -> String
name2String Name Term
x) of
(Prim
prim : [Prim]
_) -> forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Mode -> Term -> Sem r ATerm
typecheck Mode
Infer (Prim -> Term
TPrim Prim
prim)
[Prim]
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
typecheck Mode
Infer (TPrim Prim
prim) = do
Type
ty <- forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Prim -> Sem r Type
inferPrim Prim
prim
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type -> Prim -> ATerm
ATPrim Type
ty Prim
prim
where
inferPrim :: Members '[Writer Constraint, Fresh] r => Prim -> Sem r Type
inferPrim :: forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Prim -> Sem r Type
inferPrim Prim
PrimLeft = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Type
b <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
a Type -> Type -> Type
:->: (Type
a Type -> Type -> Type
:+: Type
b)
inferPrim Prim
PrimRight = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Type
b <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
b Type -> Type -> Type
:->: (Type
a Type -> Type -> Type
:+: Type
b)
inferPrim (PrimBOp BOp
op) | BOp
op forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [BOp
And, BOp
Or, BOp
Impl, BOp
Iff] = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual (BOp -> Qualifier
bopQual BOp
op) Type
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
a Type -> Type -> Type
:*: Type
a Type -> Type -> Type
:->: Type
a
inferPrim (PrimBOp BOp
And) = forall a. HasCallStack => String -> a
error String
"inferPrim And should be unreachable"
inferPrim (PrimBOp BOp
Or) = forall a. HasCallStack => String -> a
error String
"inferPrim Or should be unreachable"
inferPrim (PrimBOp BOp
Impl) = forall a. HasCallStack => String -> a
error String
"inferPrim Impl should be unreachable"
inferPrim (PrimBOp BOp
Iff) = forall a. HasCallStack => String -> a
error String
"inferPrim Iff should be unreachable"
inferPrim (PrimUOp UOp
Not) = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QBool Type
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
a Type -> Type -> Type
:->: Type
a
inferPrim Prim
conv | Prim
conv forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Prim
PrimList, Prim
PrimBag, Prim
PrimSet] = do
Atom
c <- forall (r :: EffectRow). Member Fresh r => Sem r Atom
freshAtom
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Prim
conv forall a. Eq a => a -> a -> Bool
/= Prim
PrimList) forall a b. (a -> b) -> a -> b
$ forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QCmp Type
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Atom -> Type -> Type
TyContainer Atom
c Type
a Type -> Type -> Type
:->: Prim -> Type -> Type
primCtrCon Prim
conv Type
a
where
primCtrCon :: Prim -> Type -> Type
primCtrCon Prim
PrimList = Type -> Type
TyList
primCtrCon Prim
PrimBag = Type -> Type
TyBag
primCtrCon Prim
_ = Type -> Type
TySet
inferPrim Prim
PrimList = forall a. HasCallStack => String -> a
error String
"inferPrim PrimList should be unreachable"
inferPrim Prim
PrimBag = forall a. HasCallStack => String -> a
error String
"inferPrim PrimBag should be unreachable"
inferPrim Prim
PrimSet = forall a. HasCallStack => String -> a
error String
"inferPrim PrimSet should be unreachable"
inferPrim Prim
PrimB2C = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type -> Type
TyBag Type
a Type -> Type -> Type
:->: Type -> Type
TySet (Type
a Type -> Type -> Type
:*: Type
TyN)
inferPrim Prim
PrimC2B = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Atom
c <- forall (r :: EffectRow). Member Fresh r => Sem r Atom
freshAtom
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QCmp Type
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Atom -> Type -> Type
TyContainer Atom
c (Type
a Type -> Type -> Type
:*: Type
TyN) Type -> Type -> Type
:->: Type -> Type
TyBag Type
a
inferPrim Prim
PrimUC2B = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Atom
c <- forall (r :: EffectRow). Member Fresh r => Sem r Atom
freshAtom
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Atom -> Type -> Type
TyContainer Atom
c (Type
a Type -> Type -> Type
:*: Type
TyN) Type -> Type -> Type
:->: Type -> Type
TyBag Type
a
inferPrim Prim
PrimMapToSet = do
Type
k <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Type
v <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QSimple Type
k
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type -> Type -> Type
TyMap Type
k Type
v Type -> Type -> Type
:->: Type -> Type
TySet (Type
k Type -> Type -> Type
:*: Type
v)
inferPrim Prim
PrimSetToMap = do
Type
k <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Type
v <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QSimple Type
k
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type -> Type
TySet (Type
k Type -> Type -> Type
:*: Type
v) Type -> Type -> Type
:->: Type -> Type -> Type
TyMap Type
k Type
v
inferPrim Prim
PrimSummary = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QSimple Type
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type -> Type
TyGraph Type
a Type -> Type -> Type
:->: Type -> Type -> Type
TyMap Type
a (Type -> Type
TySet Type
a)
inferPrim Prim
PrimVertex = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QSimple Type
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
a Type -> Type -> Type
:->: Type -> Type
TyGraph Type
a
inferPrim Prim
PrimEmptyGraph = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QSimple Type
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type -> Type
TyGraph Type
a
inferPrim Prim
PrimOverlay = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QSimple Type
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type -> Type
TyGraph Type
a Type -> Type -> Type
:*: Type -> Type
TyGraph Type
a Type -> Type -> Type
:->: Type -> Type
TyGraph Type
a
inferPrim Prim
PrimConnect = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QSimple Type
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type -> Type
TyGraph Type
a Type -> Type -> Type
:*: Type -> Type
TyGraph Type
a Type -> Type -> Type
:->: Type -> Type
TyGraph Type
a
inferPrim Prim
PrimInsert = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Type
b <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QSimple Type
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
a Type -> Type -> Type
:*: Type
b Type -> Type -> Type
:*: Type -> Type -> Type
TyMap Type
a Type
b Type -> Type -> Type
:->: Type -> Type -> Type
TyMap Type
a Type
b
inferPrim Prim
PrimLookup = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Type
b <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QSimple Type
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
a Type -> Type -> Type
:*: Type -> Type -> Type
TyMap Type
a Type
b Type -> Type -> Type
:->: (Type
TyUnit Type -> Type -> Type
:+: Type
b)
inferPrim (PrimBOp BOp
Cons) = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
a Type -> Type -> Type
:*: Type -> Type
TyList Type
a Type -> Type -> Type
:->: Type -> Type
TyList Type
a
inferPrim Prim
PrimEach = do
Atom
c <- forall (r :: EffectRow). Member Fresh r => Sem r Atom
freshAtom
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Type
b <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ (Type
a Type -> Type -> Type
:->: Type
b) Type -> Type -> Type
:*: Atom -> Type -> Type
TyContainer Atom
c Type
a Type -> Type -> Type
:->: Atom -> Type -> Type
TyContainer Atom
c Type
b
inferPrim Prim
PrimReduce = do
Atom
c <- forall (r :: EffectRow). Member Fresh r => Sem r Atom
freshAtom
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ (Type
a Type -> Type -> Type
:*: Type
a Type -> Type -> Type
:->: Type
a) Type -> Type -> Type
:*: Type
a Type -> Type -> Type
:*: Atom -> Type -> Type
TyContainer Atom
c Type
a Type -> Type -> Type
:->: Type
a
inferPrim Prim
PrimFilter = do
Atom
c <- forall (r :: EffectRow). Member Fresh r => Sem r Atom
freshAtom
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ (Type
a Type -> Type -> Type
:->: Type
TyBool) Type -> Type -> Type
:*: Atom -> Type -> Type
TyContainer Atom
c Type
a Type -> Type -> Type
:->: Atom -> Type -> Type
TyContainer Atom
c Type
a
inferPrim Prim
PrimJoin = do
Atom
c <- forall (r :: EffectRow). Member Fresh r => Sem r Atom
freshAtom
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Atom -> Type -> Type
TyContainer Atom
c (Atom -> Type -> Type
TyContainer Atom
c Type
a) Type -> Type -> Type
:->: Atom -> Type -> Type
TyContainer Atom
c Type
a
inferPrim Prim
PrimMerge = do
Atom
c <- forall (r :: EffectRow). Member Fresh r => Sem r Atom
freshAtom
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$
[Constraint] -> Constraint
COr
[ Type -> Type -> Constraint
CEq (Atom -> Type
TyAtom (BaseTy -> Atom
ABase BaseTy
CtrBag)) (Atom -> Type
TyAtom Atom
c)
, Type -> Type -> Constraint
CEq (Atom -> Type
TyAtom (BaseTy -> Atom
ABase BaseTy
CtrSet)) (Atom -> Type
TyAtom Atom
c)
]
let ca :: Type
ca = Atom -> Type -> Type
TyContainer Atom
c Type
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ (Type
TyN Type -> Type -> Type
:*: Type
TyN Type -> Type -> Type
:->: Type
TyN) Type -> Type -> Type
:*: Type
ca Type -> Type -> Type
:*: Type
ca Type -> Type -> Type
:->: Type
ca
inferPrim (PrimBOp BOp
CartProd) = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Type
b <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Atom
c <- forall (r :: EffectRow). Member Fresh r => Sem r Atom
freshAtom
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Atom -> Type -> Type
TyContainer Atom
c Type
a Type -> Type -> Type
:*: Atom -> Type -> Type
TyContainer Atom
c Type
b Type -> Type -> Type
:->: Atom -> Type -> Type
TyContainer Atom
c (Type
a Type -> Type -> Type
:*: Type
b)
inferPrim (PrimBOp BOp
setOp) | BOp
setOp forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [BOp
Union, BOp
Inter, BOp
Diff, BOp
Subset] = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Atom
c <- forall (r :: EffectRow). Member Fresh r => Sem r Atom
freshAtom
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$
[Constraint] -> Constraint
COr
[ Type -> Type -> Constraint
CEq (Atom -> Type
TyAtom (BaseTy -> Atom
ABase BaseTy
CtrBag)) (Atom -> Type
TyAtom Atom
c)
, Type -> Type -> Constraint
CEq (Atom -> Type
TyAtom (BaseTy -> Atom
ABase BaseTy
CtrSet)) (Atom -> Type
TyAtom Atom
c)
]
let ca :: Type
ca = Atom -> Type -> Type
TyContainer Atom
c Type
a
let resTy :: Type
resTy = case BOp
setOp of BOp
Subset -> Type
TyBool; BOp
_ -> Type
ca
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
ca Type -> Type -> Type
:*: Type
ca Type -> Type -> Type
:->: Type
resTy
inferPrim (PrimBOp BOp
Union) = forall a. HasCallStack => String -> a
error String
"inferPrim Union should be unreachable"
inferPrim (PrimBOp BOp
Inter) = forall a. HasCallStack => String -> a
error String
"inferPrim Inter should be unreachable"
inferPrim (PrimBOp BOp
Diff) = forall a. HasCallStack => String -> a
error String
"inferPrim Diff should be unreachable"
inferPrim (PrimBOp BOp
Subset) = forall a. HasCallStack => String -> a
error String
"inferPrim Subset should be unreachable"
inferPrim (PrimBOp BOp
Elem) = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Atom
c <- forall (r :: EffectRow). Member Fresh r => Sem r Atom
freshAtom
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QCmp Type
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
a Type -> Type -> Type
:*: Atom -> Type -> Type
TyContainer Atom
c Type
a Type -> Type -> Type
:->: Type
TyBool
inferPrim (PrimBOp BOp
IDiv) = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Type
resTy <- forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Type -> Sem r Type
cInt Type
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
a Type -> Type -> Type
:*: Type
a Type -> Type -> Type
:->: Type
resTy
inferPrim (PrimBOp BOp
Mod) = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Type -> Type -> Constraint
CSub Type
a Type
TyZ
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
a Type -> Type -> Type
:*: Type
a Type -> Type -> Type
:->: Type
a
inferPrim (PrimBOp BOp
op) | BOp
op forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [BOp
Add, BOp
Mul, BOp
Sub, BOp
Div, BOp
SSub] = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual (BOp -> Qualifier
bopQual BOp
op) Type
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
a Type -> Type -> Type
:*: Type
a Type -> Type -> Type
:->: Type
a
inferPrim (PrimBOp BOp
Add) = forall a. HasCallStack => String -> a
error String
"inferPrim Add should be unreachable"
inferPrim (PrimBOp BOp
Mul) = forall a. HasCallStack => String -> a
error String
"inferPrim Mul should be unreachable"
inferPrim (PrimBOp BOp
Sub) = forall a. HasCallStack => String -> a
error String
"inferPrim Sub should be unreachable"
inferPrim (PrimBOp BOp
Div) = forall a. HasCallStack => String -> a
error String
"inferPrim Div should be unreachable"
inferPrim (PrimBOp BOp
SSub) = forall a. HasCallStack => String -> a
error String
"inferPrim SSub should be unreachable"
inferPrim (PrimUOp UOp
Neg) = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QSub Type
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
a Type -> Type -> Type
:->: Type
a
inferPrim (PrimBOp BOp
Exp) = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Type
b <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Type
resTy <- forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Type -> Type -> Sem r Type
cExp Type
a Type
b
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
a Type -> Type -> Type
:*: Type
b Type -> Type -> Type
:->: Type
resTy
inferPrim Prim
PrimIsPrime = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
TyN Type -> Type -> Type
:->: Type
TyBool
inferPrim Prim
PrimFactor = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
TyN Type -> Type -> Type
:->: Type -> Type
TyBag Type
TyN
inferPrim Prim
PrimFrac = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
TyQ Type -> Type -> Type
:->: (Type
TyZ Type -> Type -> Type
:*: Type
TyN)
inferPrim (PrimBOp BOp
Divides) = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QNum Type
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
a Type -> Type -> Type
:*: Type
a Type -> Type -> Type
:->: Type
TyBool
inferPrim (PrimBOp BOp
Choose) = do
Type
b <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ [Constraint] -> Constraint
COr [Type -> Type -> Constraint
CEq Type
b Type
TyN, Type -> Type -> Constraint
CEq Type
b (Type -> Type
TyList Type
TyN)]
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
TyN Type -> Type -> Type
:*: Type
b Type -> Type -> Type
:->: Type
TyN
inferPrim Prim
PrimUntil = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
TyN Type -> Type -> Type
:*: Type -> Type
TyList Type
TyN Type -> Type -> Type
:->: Type -> Type
TyList Type
TyN
inferPrim Prim
PrimCrash = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
TyString Type -> Type -> Type
:->: Type
a
inferPrim Prim
PrimHolds = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
TyProp Type -> Type -> Type
:->: Type
TyBool
inferPrim (PrimBOp BOp
ShouldEq) = do
Type
ty <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QCmp Type
ty
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
ty Type -> Type -> Type
:*: Type
ty Type -> Type -> Type
:->: Type
TyProp
inferPrim (PrimBOp BOp
ShouldLt) = do
Type
ty <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QCmp Type
ty
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
ty Type -> Type -> Type
:*: Type
ty Type -> Type -> Type
:->: Type
TyProp
inferPrim (PrimBOp BOp
op) | BOp
op forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [BOp
Eq, BOp
Neq, BOp
Lt, BOp
Gt, BOp
Leq, BOp
Geq] = do
Type
ty <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QCmp Type
ty
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
ty Type -> Type -> Type
:*: Type
ty Type -> Type -> Type
:->: Type
TyBool
inferPrim (PrimBOp BOp
Eq) = forall a. HasCallStack => String -> a
error String
"inferPrim Eq should be unreachable"
inferPrim (PrimBOp BOp
Neq) = forall a. HasCallStack => String -> a
error String
"inferPrim Neq should be unreachable"
inferPrim (PrimBOp BOp
Lt) = forall a. HasCallStack => String -> a
error String
"inferPrim Lt should be unreachable"
inferPrim (PrimBOp BOp
Gt) = forall a. HasCallStack => String -> a
error String
"inferPrim Gt should be unreachable"
inferPrim (PrimBOp BOp
Leq) = forall a. HasCallStack => String -> a
error String
"inferPrim Leq should be unreachable"
inferPrim (PrimBOp BOp
Geq) = forall a. HasCallStack => String -> a
error String
"inferPrim Geq should be unreachable"
inferPrim (PrimBOp BOp
op) | BOp
op forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [BOp
Min, BOp
Max] = do
Type
ty <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QCmp Type
ty
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
ty Type -> Type -> Type
:*: Type
ty Type -> Type -> Type
:->: Type
ty
inferPrim (PrimBOp BOp
Min) = forall a. HasCallStack => String -> a
error String
"inferPrim Min should be unreachable"
inferPrim (PrimBOp BOp
Max) = forall a. HasCallStack => String -> a
error String
"inferPrim Max should be unreachable"
inferPrim (PrimUOp UOp
Fact) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
TyN Type -> Type -> Type
:->: Type
TyN
inferPrim Prim
PrimSqrt = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
TyN Type -> Type -> Type
:->: Type
TyN
inferPrim Prim
p | Prim
p forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Prim
PrimFloor, Prim
PrimCeil] = do
Type
argTy <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Type
resTy <- forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Type -> Sem r Type
cInt Type
argTy
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
argTy Type -> Type -> Type
:->: Type
resTy
inferPrim Prim
PrimFloor = forall a. HasCallStack => String -> a
error String
"inferPrim Floor should be unreachable"
inferPrim Prim
PrimCeil = forall a. HasCallStack => String -> a
error String
"inferPrim Ceil should be unreachable"
inferPrim Prim
PrimAbs = do
Type
argTy <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Type
resTy <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Type -> Type -> Sem r ()
cAbs Type
argTy Type
resTy forall (r :: EffectRow).
Members '[Writer Constraint] r =>
Sem r () -> Sem r () -> Sem r ()
`cOr` forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Type -> Type -> Sem r ()
cSize Type
argTy Type
resTy
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
argTy Type -> Type -> Type
:->: Type
resTy
inferPrim Prim
PrimPower = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Atom
c <- forall (r :: EffectRow). Member Fresh r => Sem r Atom
freshAtom
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QCmp Type
a
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$
[Constraint] -> Constraint
COr
[ Type -> Type -> Constraint
CEq (Atom -> Type
TyAtom (BaseTy -> Atom
ABase BaseTy
CtrSet)) (Atom -> Type
TyAtom Atom
c)
, Type -> Type -> Constraint
CEq (Atom -> Type
TyAtom (BaseTy -> Atom
ABase BaseTy
CtrBag)) (Atom -> Type
TyAtom Atom
c)
]
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Atom -> Type -> Type
TyContainer Atom
c Type
a Type -> Type -> Type
:->: Atom -> Type -> Type
TyContainer Atom
c (Atom -> Type -> Type
TyContainer Atom
c Type
a)
inferPrim Prim
PrimLookupSeq = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type -> Type
TyList Type
TyN Type -> Type -> Type
:->: (Type
TyUnit Type -> Type -> Type
:+: Type
TyString)
inferPrim Prim
PrimExtendSeq = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type -> Type
TyList Type
TyN Type -> Type -> Type
:->: Type -> Type
TyList Type
TyN
typecheck Mode
Infer Term
TUnit = forall (m :: * -> *) a. Monad m => a -> m a
return ATerm
ATUnit
typecheck Mode
Infer (TBool Bool
b) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type -> Bool -> ATerm
ATBool Type
TyBool Bool
b
typecheck Mode
Infer (TChar Char
c) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Char -> ATerm
ATChar Char
c
typecheck Mode
Infer (TString String
cs) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ String -> ATerm
ATString String
cs
typecheck Mode
Infer (TNat Integer
n) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type -> Integer -> ATerm
ATNat Type
TyN Integer
n
typecheck Mode
Infer (TRat Rational
r) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Rational -> ATerm
ATRat Rational
r
typecheck Mode
_ Term
TWild = forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw TCError
NoTWild
typecheck (Check Type
checkTy) tm :: Term
tm@(TAbs Quantifier
Lam Bind [Pattern] Term
body) = do
([Pattern]
args, Term
t) <- forall (r :: EffectRow) p t.
(Member Fresh r, Alpha p, Alpha t) =>
Bind p t -> Sem r (p, t)
unbind Bind [Pattern] Term
body
(TyCtx
ctx, [APattern]
typedArgs, Type
resTy) <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
[Pattern] -> Type -> Term -> Sem r (TyCtx, [APattern], Type)
checkArgs [Pattern]
args Type
checkTy Term
tm
forall a b (r :: EffectRow) c.
Member (Reader (Ctx a b)) r =>
Ctx a b -> Sem r c -> Sem r c
extends TyCtx
ctx forall a b. (a -> b) -> a -> b
$
Quantifier -> Type -> Clause -> ATerm
ATAbs Quantifier
Lam Type
checkTy forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall p t. (Alpha p, Alpha t) => p -> t -> Bind p t
bind (coerce :: forall a b. Coercible a b => a -> b
coerce [APattern]
typedArgs) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Type -> Sem r ATerm
check Term
t Type
resTy)
where
checkArgs ::
Members '[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
[Pattern] ->
Type ->
Term ->
Sem r (TyCtx, [APattern], Type)
checkArgs :: forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
[Pattern] -> Type -> Term -> Sem r (TyCtx, [APattern], Type)
checkArgs [] Type
ty Term
_ = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. Ctx a b
emptyCtx, [], Type
ty)
checkArgs (Pattern
p : [Pattern]
args) Type
ty Term
term = do
(Type
ty1, Type
ty2) <- forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Type -> Either Term Pattern -> Sem r (Type, Type)
ensureConstr2 Con
CArr Type
ty (forall a b. a -> Either a b
Left Term
term)
(TyCtx
pCtx, APattern
pTyped) <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern Pattern
p Type
ty1
(TyCtx
ctx, [APattern]
typedArgs, Type
resTy) <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
[Pattern] -> Type -> Term -> Sem r (TyCtx, [APattern], Type)
checkArgs [Pattern]
args Type
ty2 Term
term
forall (m :: * -> *) a. Monad m => a -> m a
return (TyCtx
pCtx forall a. Semigroup a => a -> a -> a
<> TyCtx
ctx, APattern
pTyped forall a. a -> [a] -> [a]
: [APattern]
typedArgs, Type
resTy)
typecheck Mode
Infer (TAbs Quantifier
q Bind [Pattern] Term
lam) = do
([Pattern]
args, Term
t) <- forall (r :: EffectRow) p t.
(Member Fresh r, Alpha p, Alpha t) =>
Bind p t -> Sem r (p, t)
unbind Bind [Pattern] Term
lam
[Type]
tys <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError, Fresh] r =>
Pattern -> Sem r Type
getAscrOrFresh [Pattern]
args
([TyCtx]
pCtxs, [APattern]
typedPats) <- forall a b. [(a, b)] -> ([a], [b])
unzip forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern [Pattern]
args [Type]
tys
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Quantifier
q forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Quantifier
All, Quantifier
Ex]) forall a b. (a -> b) -> a -> b
$
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (forall a b. (a -> b) -> [a] -> [b]
map forall t. HasType t => t -> Type
getType [APattern]
typedPats) forall a b. (a -> b) -> a -> b
$ \Type
ty ->
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Type -> Bool
isSearchable Type
ty) forall a b. (a -> b) -> a -> b
$
forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw forall a b. (a -> b) -> a -> b
$
Type -> TCError
NoSearch Type
ty
forall a b (r :: EffectRow) c.
Member (Reader (Ctx a b)) r =>
Ctx a b -> Sem r c -> Sem r c
extends (forall a. Monoid a => [a] -> a
mconcat [TyCtx]
pCtxs) forall a b. (a -> b) -> a -> b
$ do
case Quantifier
q of
Quantifier
Lam -> do
ATerm
at <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Sem r ATerm
infer Term
t
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Quantifier -> Type -> Clause -> ATerm
ATAbs Quantifier
Lam ([Type] -> Type -> Type
mkFunTy [Type]
tys (forall t. HasType t => t -> Type
getType ATerm
at)) (forall p t. (Alpha p, Alpha t) => p -> t -> Bind p t
bind [APattern]
typedPats ATerm
at)
Quantifier
_ -> do
ATerm
at <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Type -> Sem r ATerm
check Term
t Type
TyProp
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Quantifier -> Type -> Clause -> ATerm
ATAbs Quantifier
q Type
TyProp (forall p t. (Alpha p, Alpha t) => p -> t -> Bind p t
bind [APattern]
typedPats ATerm
at)
where
getAscrOrFresh ::
Members '[Reader TyDefCtx, Error TCError, Fresh] r =>
Pattern ->
Sem r Type
getAscrOrFresh :: forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError, Fresh] r =>
Pattern -> Sem r Type
getAscrOrFresh (PAscr Pattern
_ Type
ty) = forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
Type -> Sem r ()
checkTypeValid Type
ty forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
ty
getAscrOrFresh Pattern
_ = forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
mkFunTy :: [Type] -> Type -> Type
mkFunTy :: [Type] -> Type -> Type
mkFunTy [Type]
tys Type
out = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Type -> Type -> Type
(:->:) Type
out [Type]
tys
typecheck Mode
Infer (TApp Term
t Term
t') = do
ATerm
at <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Sem r ATerm
infer Term
t
let ty :: Type
ty = forall t. HasType t => t -> Type
getType ATerm
at
(Type
ty1, Type
ty2) <- forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Type -> Either Term Pattern -> Sem r (Type, Type)
ensureConstr2 Con
CArr Type
ty (forall a b. a -> Either a b
Left Term
t)
Type -> ATerm -> ATerm -> ATerm
ATApp Type
ty2 ATerm
at forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Type -> Sem r ATerm
check Term
t' Type
ty1
typecheck Mode
mode1 (TTup [Term]
tup) = forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Type -> [ATerm] -> ATerm
ATTup forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Mode -> [Term] -> Sem r (Type, [ATerm])
typecheckTuple Mode
mode1 [Term]
tup
where
typecheckTuple ::
Members '[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Mode ->
[Term] ->
Sem r (Type, [ATerm])
typecheckTuple :: forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Mode -> [Term] -> Sem r (Type, [ATerm])
typecheckTuple Mode
_ [] = forall a. HasCallStack => String -> a
error String
"Impossible! typecheckTuple []"
typecheckTuple Mode
mode [Term
t] = (forall t. HasType t => t -> Type
getType forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& (forall a. a -> [a] -> [a]
: [])) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Mode -> Term -> Sem r ATerm
typecheck Mode
mode Term
t
typecheckTuple Mode
mode (Term
t : [Term]
ts) = do
(Mode
m, Mode
ms) <- forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Mode -> Either Term Pattern -> Sem r (Mode, Mode)
ensureConstrMode2 Con
CProd Mode
mode (forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ [Term] -> Term
TTup (Term
t forall a. a -> [a] -> [a]
: [Term]
ts))
ATerm
at <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Mode -> Term -> Sem r ATerm
typecheck Mode
m Term
t
(Type
ty, [ATerm]
ats) <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Mode -> [Term] -> Sem r (Type, [ATerm])
typecheckTuple Mode
ms [Term]
ts
forall (m :: * -> *) a. Monad m => a -> m a
return (forall t. HasType t => t -> Type
getType ATerm
at Type -> Type -> Type
:*: Type
ty, ATerm
at forall a. a -> [a] -> [a]
: [ATerm]
ats)
typecheck Mode
Infer (TChain Term
t [Link_ UD]
ls) =
Type -> ATerm -> [Link_ TY] -> ATerm
ATChain Type
TyBool forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Sem r ATerm
infer Term
t forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> [Link_ UD] -> Sem r [Link_ TY]
inferChain Term
t [Link_ UD]
ls
where
inferChain ::
Members '[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Term ->
[Link] ->
Sem r [ALink]
inferChain :: forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> [Link_ UD] -> Sem r [Link_ TY]
inferChain Term
_ [] = forall (m :: * -> *) a. Monad m => a -> m a
return []
inferChain Term
t1 (TLink BOp
op Term
t2 : [Link_ UD]
links) = do
ATerm
at2 <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Sem r ATerm
infer Term
t2
ATerm
_ <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Type -> Sem r ATerm
check (BOp -> Term -> Term -> Term
TBin BOp
op Term
t1 Term
t2) Type
TyBool
[Link_ TY]
atl <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> [Link_ UD] -> Sem r [Link_ TY]
inferChain Term
t2 [Link_ UD]
links
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ BOp -> ATerm -> Link_ TY
ATLink BOp
op ATerm
at2 forall a. a -> [a] -> [a]
: [Link_ TY]
atl
typecheck Mode
Infer (TTyOp TyOp
Enumerate Type
t) = do
forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
Type -> Sem r ()
checkTypeValid Type
t
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type -> TyOp -> Type -> ATerm
ATTyOp (Type -> Type
TyList Type
t) TyOp
Enumerate Type
t
typecheck Mode
Infer (TTyOp TyOp
Count Type
t) = do
forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
Type -> Sem r ()
checkTypeValid Type
t
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type -> TyOp -> Type -> ATerm
ATTyOp (Type
TyUnit Type -> Type -> Type
:+: Type
TyN) TyOp
Count Type
t
typecheck Mode
mode t :: Term
t@(TContainer Container
c [(Term, Maybe Term)]
xs Maybe (Ellipsis Term)
ell) = do
Mode
eltMode <- forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Mode -> Either Term Pattern -> Sem r Mode
ensureConstrMode1 (Container -> Con
containerToCon Container
c) Mode
mode (forall a b. a -> Either a b
Left Term
t)
[(ATerm, Maybe ATerm)]
axns <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (\(Term
x, Maybe Term
n) -> (,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Mode -> Term -> Sem r ATerm
typecheck Mode
eltMode Term
x forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Type -> Sem r ATerm
`check` Type
TyN) Maybe Term
n) [(Term, Maybe Term)]
xs
Maybe (Ellipsis ATerm)
aell <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Mode -> Maybe (Ellipsis Term) -> Sem r (Maybe (Ellipsis ATerm))
typecheckEllipsis Mode
eltMode Maybe (Ellipsis Term)
ell
Type
resTy <- case Mode
mode of
Mode
Infer -> do
let tys :: [Type]
tys = [forall t. HasType t => t -> Type
getType ATerm
at | Just (Until ATerm
at) <- [Maybe (Ellipsis ATerm)
aell]] forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (forall t. HasType t => t -> Type
getType forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) [(ATerm, Maybe ATerm)]
axns
Type
tyv <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
[Constraint] -> Sem r ()
constraints forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (Type -> Type -> Constraint
`CSub` Type
tyv) [Type]
tys
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Container -> Type -> Type
containerTy Container
c Type
tyv
Check Type
ty -> forall (m :: * -> *) a. Monad m => a -> m a
return Type
ty
Type
eltTy <- forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Container -> Type -> Sem r Type
getEltTy Container
c Type
resTy
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Container
c forall a. Eq a => a -> a -> Bool
/= Container
ListContainer Bool -> Bool -> Bool
&& Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
P.null [(Term, Maybe Term)]
xs)) forall a b. (a -> b) -> a -> b
$ forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QCmp Type
eltTy
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall a. Maybe a -> Bool
isJust Maybe (Ellipsis Term)
ell) forall a b. (a -> b) -> a -> b
$ forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QEnum Type
eltTy
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type
-> Container
-> [(ATerm, Maybe ATerm)]
-> Maybe (Ellipsis ATerm)
-> ATerm
ATContainer Type
resTy Container
c [(ATerm, Maybe ATerm)]
axns Maybe (Ellipsis ATerm)
aell
where
typecheckEllipsis ::
Members '[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Mode ->
Maybe (Ellipsis Term) ->
Sem r (Maybe (Ellipsis ATerm))
typecheckEllipsis :: forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Mode -> Maybe (Ellipsis Term) -> Sem r (Maybe (Ellipsis ATerm))
typecheckEllipsis Mode
_ Maybe (Ellipsis Term)
Nothing = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
typecheckEllipsis Mode
m (Just (Until Term
tm)) = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. t -> Ellipsis t
Until forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Mode -> Term -> Sem r ATerm
typecheck Mode
m Term
tm
typecheck Mode
mode tcc :: Term
tcc@(TContainerComp Container
c Bind (Telescope (Qual_ UD)) Term
bqt) = do
Mode
eltMode <- forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Mode -> Either Term Pattern -> Sem r Mode
ensureConstrMode1 (Container -> Con
containerToCon Container
c) Mode
mode (forall a b. a -> Either a b
Left Term
tcc)
(Telescope (Qual_ UD)
qs, Term
t) <- forall (r :: EffectRow) p t.
(Member Fresh r, Alpha p, Alpha t) =>
Bind p t -> Sem r (p, t)
unbind Bind (Telescope (Qual_ UD)) Term
bqt
(Telescope (Qual_ TY)
aqs, TyCtx
cx) <- forall b tyb (r :: EffectRow).
(Alpha b, Alpha tyb, Member (Reader TyCtx) r) =>
(b -> Sem r (tyb, TyCtx))
-> Telescope b -> Sem r (Telescope tyb, TyCtx)
inferTelescope forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Qual_ UD -> Sem r (Qual_ TY, TyCtx)
inferQual Telescope (Qual_ UD)
qs
forall a b (r :: EffectRow) c.
Member (Reader (Ctx a b)) r =>
Ctx a b -> Sem r c -> Sem r c
extends TyCtx
cx forall a b. (a -> b) -> a -> b
$ do
ATerm
at <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Mode -> Term -> Sem r ATerm
typecheck Mode
eltMode Term
t
let resTy :: Type
resTy = case Mode
mode of
Mode
Infer -> Container -> Type -> Type
containerTy Container
c (forall t. HasType t => t -> Type
getType ATerm
at)
Check Type
ty -> Type
ty
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type -> Container -> Bind (Telescope (Qual_ TY)) ATerm -> ATerm
ATContainerComp Type
resTy Container
c (forall p t. (Alpha p, Alpha t) => p -> t -> Bind p t
bind Telescope (Qual_ TY)
aqs ATerm
at)
where
inferQual ::
Members '[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Qual ->
Sem r (AQual, TyCtx)
inferQual :: forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Qual_ UD -> Sem r (Qual_ TY, TyCtx)
inferQual (QBind Name Term
x (forall e. IsEmbed e => e -> Embedded e
unembed -> Embedded (Embed Term)
t)) = do
ATerm
at <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Sem r ATerm
infer Embedded (Embed Term)
t
Type
ty <- forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Type -> Either Term Pattern -> Sem r Type
ensureConstr1 (Container -> Con
containerToCon Container
c) (forall t. HasType t => t -> Type
getType ATerm
at) (forall a b. a -> Either a b
Left Embedded (Embed Term)
t)
forall (m :: * -> *) a. Monad m => a -> m a
return (Name ATerm -> Embed ATerm -> Qual_ TY
AQBind (coerce :: forall a b. Coercible a b => a -> b
coerce Name Term
x) (forall e. IsEmbed e => Embedded e -> e
embed ATerm
at), forall a b. QName a -> b -> Ctx a b
singleCtx (forall a. Name a -> QName a
localName Name Term
x) (Type -> PolyType
toPolyType Type
ty))
inferQual (QGuard (forall e. IsEmbed e => e -> Embedded e
unembed -> Embedded (Embed Term)
t)) = do
ATerm
at <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Type -> Sem r ATerm
check Embedded (Embed Term)
t Type
TyBool
forall (m :: * -> *) a. Monad m => a -> m a
return (Embed ATerm -> Qual_ TY
AQGuard (forall e. IsEmbed e => Embedded e -> e
embed ATerm
at), forall a b. Ctx a b
emptyCtx)
typecheck Mode
mode (TLet Bind (Telescope (Binding_ UD)) Term
l) = do
(Telescope (Binding_ UD)
bs, Term
t2) <- forall (r :: EffectRow) p t.
(Member Fresh r, Alpha p, Alpha t) =>
Bind p t -> Sem r (p, t)
unbind Bind (Telescope (Binding_ UD)) Term
l
(Telescope (Binding_ TY)
as, TyCtx
ctx) <- forall b tyb (r :: EffectRow).
(Alpha b, Alpha tyb, Member (Reader TyCtx) r) =>
(b -> Sem r (tyb, TyCtx))
-> Telescope b -> Sem r (Telescope tyb, TyCtx)
inferTelescope forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Binding_ UD -> Sem r (Binding_ TY, TyCtx)
inferBinding Telescope (Binding_ UD)
bs
forall a b (r :: EffectRow) c.
Member (Reader (Ctx a b)) r =>
Ctx a b -> Sem r c -> Sem r c
extends TyCtx
ctx forall a b. (a -> b) -> a -> b
$ do
ATerm
at2 <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Mode -> Term -> Sem r ATerm
typecheck Mode
mode Term
t2
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type -> Bind (Telescope (Binding_ TY)) ATerm -> ATerm
ATLet (forall t. HasType t => t -> Type
getType ATerm
at2) (forall p t. (Alpha p, Alpha t) => p -> t -> Bind p t
bind Telescope (Binding_ TY)
as ATerm
at2)
where
inferBinding ::
Members '[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Binding ->
Sem r (ABinding, TyCtx)
inferBinding :: forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Binding_ UD -> Sem r (Binding_ TY, TyCtx)
inferBinding (Binding Maybe (Embed PolyType)
mty Name Term
x (forall e. IsEmbed e => e -> Embedded e
unembed -> Embedded (Embed Term)
t)) = do
ATerm
at <- case Maybe (Embed PolyType)
mty of
Just (forall e. IsEmbed e => e -> Embedded e
unembed -> Embedded (Embed PolyType)
ty) -> forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> PolyType -> Sem r ATerm
checkPolyTy Embedded (Embed Term)
t Embedded (Embed PolyType)
ty
Maybe (Embed PolyType)
Nothing -> forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Sem r ATerm
infer Embedded (Embed Term)
t
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Embed PolyType) -> Name ATerm -> Embed ATerm -> Binding_ TY
ABinding Maybe (Embed PolyType)
mty (coerce :: forall a b. Coercible a b => a -> b
coerce Name Term
x) (forall e. IsEmbed e => Embedded e -> e
embed ATerm
at), forall a b. QName a -> b -> Ctx a b
singleCtx (forall a. Name a -> QName a
localName Name Term
x) (Type -> PolyType
toPolyType forall a b. (a -> b) -> a -> b
$ forall t. HasType t => t -> Type
getType ATerm
at))
typecheck Mode
_ (TCase []) = forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw TCError
EmptyCase
typecheck Mode
mode (TCase [Branch]
bs) = do
[ABranch]
bs' <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Branch -> Sem r ABranch
typecheckBranch [Branch]
bs
Type
resTy <- case Mode
mode of
Check Type
ty -> forall (m :: * -> *) a. Monad m => a -> m a
return Type
ty
Mode
Infer -> do
Type
x <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
[Constraint] -> Sem r ()
constraints forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map ((Type -> Type -> Constraint
`CSub` Type
x) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. HasType t => t -> Type
getType) [ABranch]
bs'
forall (m :: * -> *) a. Monad m => a -> m a
return Type
x
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Type -> [ABranch] -> ATerm
ATCase Type
resTy [ABranch]
bs'
where
typecheckBranch ::
Members '[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Branch ->
Sem r ABranch
typecheckBranch :: forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Branch -> Sem r ABranch
typecheckBranch Branch
b = do
(Telescope (Guard_ UD)
gs, Term
t) <- forall (r :: EffectRow) p t.
(Member Fresh r, Alpha p, Alpha t) =>
Bind p t -> Sem r (p, t)
unbind Branch
b
(Telescope (Guard_ TY)
ags, TyCtx
ctx) <- forall b tyb (r :: EffectRow).
(Alpha b, Alpha tyb, Member (Reader TyCtx) r) =>
(b -> Sem r (tyb, TyCtx))
-> Telescope b -> Sem r (Telescope tyb, TyCtx)
inferTelescope forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Guard_ UD -> Sem r (Guard_ TY, TyCtx)
inferGuard Telescope (Guard_ UD)
gs
forall a b (r :: EffectRow) c.
Member (Reader (Ctx a b)) r =>
Ctx a b -> Sem r c -> Sem r c
extends TyCtx
ctx forall a b. (a -> b) -> a -> b
$
forall p t. (Alpha p, Alpha t) => p -> t -> Bind p t
bind Telescope (Guard_ TY)
ags forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Mode -> Term -> Sem r ATerm
typecheck Mode
mode Term
t
inferGuard ::
Members '[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Guard ->
Sem r (AGuard, TyCtx)
inferGuard :: forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Guard_ UD -> Sem r (Guard_ TY, TyCtx)
inferGuard (GBool (forall e. IsEmbed e => e -> Embedded e
unembed -> Embedded (Embed Term)
t)) = do
ATerm
at <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Type -> Sem r ATerm
check Embedded (Embed Term)
t Type
TyBool
forall (m :: * -> *) a. Monad m => a -> m a
return (Embed ATerm -> Guard_ TY
AGBool (forall e. IsEmbed e => Embedded e -> e
embed ATerm
at), forall a b. Ctx a b
emptyCtx)
inferGuard (GPat (forall e. IsEmbed e => e -> Embedded e
unembed -> Embedded (Embed Term)
t) Pattern
p) = do
ATerm
at <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Sem r ATerm
infer Embedded (Embed Term)
t
(TyCtx
ctx, APattern
apt) <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern Pattern
p (forall t. HasType t => t -> Type
getType ATerm
at)
forall (m :: * -> *) a. Monad m => a -> m a
return (Embed ATerm -> APattern -> Guard_ TY
AGPat (forall e. IsEmbed e => Embedded e -> e
embed ATerm
at) APattern
apt, TyCtx
ctx)
inferGuard (GLet (Binding Maybe (Embed PolyType)
mty Name Term
x (forall e. IsEmbed e => e -> Embedded e
unembed -> Embedded (Embed Term)
t))) = do
ATerm
at <- case Maybe (Embed PolyType)
mty of
Just (forall e. IsEmbed e => e -> Embedded e
unembed -> Embedded (Embed PolyType)
ty) -> forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> PolyType -> Sem r ATerm
checkPolyTy Embedded (Embed Term)
t Embedded (Embed PolyType)
ty
Maybe (Embed PolyType)
Nothing -> forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Sem r ATerm
infer Embedded (Embed Term)
t
forall (m :: * -> *) a. Monad m => a -> m a
return
( Binding_ TY -> Guard_ TY
AGLet (Maybe (Embed PolyType) -> Name ATerm -> Embed ATerm -> Binding_ TY
ABinding Maybe (Embed PolyType)
mty (coerce :: forall a b. Coercible a b => a -> b
coerce Name Term
x) (forall e. IsEmbed e => Embedded e -> e
embed ATerm
at))
, forall a b. QName a -> b -> Ctx a b
singleCtx (forall a. Name a -> QName a
localName Name Term
x) (Type -> PolyType
toPolyType (forall t. HasType t => t -> Type
getType ATerm
at))
)
typecheck Mode
Infer (TAscr Term
t PolyType
ty) = forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
PolyType -> Sem r ()
checkPolyTyValid PolyType
ty forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> PolyType -> Sem r ATerm
checkPolyTy Term
t PolyType
ty
typecheck (Check Type
ty) Term
t = do
ATerm
at <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Sem r ATerm
infer Term
t
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Type -> Type -> Constraint
CSub (forall t. HasType t => t -> Type
getType ATerm
at) Type
ty
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall t. HasType t => Type -> t -> t
setType Type
ty ATerm
at
checkPattern ::
Members '[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Pattern ->
Type ->
Sem r (TyCtx, APattern)
checkPattern :: forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern (PNonlinear Pattern
p Name Term
x) Type
_ = forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw forall a b. (a -> b) -> a -> b
$ Pattern -> Name Term -> TCError
NonlinearPattern Pattern
p Name Term
x
checkPattern Pattern
p (TyUser String
name [Type]
args) = forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
String -> [Type] -> Sem r Type
lookupTyDefn String
name [Type]
args forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern Pattern
p
checkPattern (PVar Name Term
x) Type
ty = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. QName a -> b -> Ctx a b
singleCtx (forall a. Name a -> QName a
localName Name Term
x) (Type -> PolyType
toPolyType Type
ty), Type -> Name ATerm -> APattern
APVar Type
ty (coerce :: forall a b. Coercible a b => a -> b
coerce Name Term
x))
checkPattern Pattern
PWild Type
ty = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. Ctx a b
emptyCtx, Type -> APattern
APWild Type
ty)
checkPattern (PAscr Pattern
p Type
ty1) Type
ty2 = do
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Type -> Type -> Constraint
CSub Type
ty2 Type
ty1
forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern Pattern
p Type
ty1
checkPattern Pattern
PUnit Type
ty = do
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Type -> Type -> Sem r ()
ensureEq Type
ty Type
TyUnit
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. Ctx a b
emptyCtx, APattern
APUnit)
checkPattern (PBool Bool
b) Type
ty = do
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Type -> Type -> Sem r ()
ensureEq Type
ty Type
TyBool
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. Ctx a b
emptyCtx, Bool -> APattern
APBool Bool
b)
checkPattern (PChar Char
c) Type
ty = do
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Type -> Type -> Sem r ()
ensureEq Type
ty Type
TyC
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. Ctx a b
emptyCtx, Char -> APattern
APChar Char
c)
checkPattern (PString String
s) Type
ty = do
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Type -> Type -> Sem r ()
ensureEq Type
ty Type
TyString
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. Ctx a b
emptyCtx, String -> APattern
APString String
s)
checkPattern (PTup [Pattern]
tup) Type
tupTy = do
[(TyCtx, APattern)]
listCtxtAps <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
[Pattern] -> Type -> Sem r [(TyCtx, APattern)]
checkTuplePat [Pattern]
tup Type
tupTy
let ([TyCtx]
ctxs, [APattern]
aps) = forall a b. [(a, b)] -> ([a], [b])
unzip [(TyCtx, APattern)]
listCtxtAps
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Monoid a => [a] -> a
mconcat [TyCtx]
ctxs, Type -> [APattern] -> APattern
APTup (forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 Type -> Type -> Type
(:*:) (forall a b. (a -> b) -> [a] -> [b]
map forall t. HasType t => t -> Type
getType [APattern]
aps)) [APattern]
aps)
where
checkTuplePat ::
Members '[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
[Pattern] ->
Type ->
Sem r [(TyCtx, APattern)]
checkTuplePat :: forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
[Pattern] -> Type -> Sem r [(TyCtx, APattern)]
checkTuplePat [] Type
_ = forall a. HasCallStack => String -> a
error String
"Impossible! checkTuplePat []"
checkTuplePat [Pattern
p] Type
ty = do
(TyCtx
ctx, APattern
apt) <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern Pattern
p Type
ty
forall (m :: * -> *) a. Monad m => a -> m a
return [(TyCtx
ctx, APattern
apt)]
checkTuplePat (Pattern
p : [Pattern]
ps) Type
ty = do
(Type
ty1, Type
ty2) <- forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Type -> Either Term Pattern -> Sem r (Type, Type)
ensureConstr2 Con
CProd Type
ty (forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ [Pattern] -> Pattern
PTup (Pattern
p forall a. a -> [a] -> [a]
: [Pattern]
ps))
(TyCtx
ctx, APattern
apt) <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern Pattern
p Type
ty1
[(TyCtx, APattern)]
rest <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
[Pattern] -> Type -> Sem r [(TyCtx, APattern)]
checkTuplePat [Pattern]
ps Type
ty2
forall (m :: * -> *) a. Monad m => a -> m a
return ((TyCtx
ctx, APattern
apt) forall a. a -> [a] -> [a]
: [(TyCtx, APattern)]
rest)
checkPattern p :: Pattern
p@(PInj Side
L Pattern
pat) Type
ty = do
(Type
ty1, Type
ty2) <- forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Type -> Either Term Pattern -> Sem r (Type, Type)
ensureConstr2 Con
CSum Type
ty (forall a b. b -> Either a b
Right Pattern
p)
(TyCtx
ctx, APattern
apt) <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern Pattern
pat Type
ty1
forall (m :: * -> *) a. Monad m => a -> m a
return (TyCtx
ctx, Type -> Side -> APattern -> APattern
APInj (Type
ty1 Type -> Type -> Type
:+: Type
ty2) Side
L APattern
apt)
checkPattern p :: Pattern
p@(PInj Side
R Pattern
pat) Type
ty = do
(Type
ty1, Type
ty2) <- forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Type -> Either Term Pattern -> Sem r (Type, Type)
ensureConstr2 Con
CSum Type
ty (forall a b. b -> Either a b
Right Pattern
p)
(TyCtx
ctx, APattern
apt) <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern Pattern
pat Type
ty2
forall (m :: * -> *) a. Monad m => a -> m a
return (TyCtx
ctx, Type -> Side -> APattern -> APattern
APInj (Type
ty1 Type -> Type -> Type
:+: Type
ty2) Side
R APattern
apt)
checkPattern (PNat Integer
n) Type
ty = do
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Type -> Type -> Constraint
CSub Type
TyN Type
ty
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. Ctx a b
emptyCtx, Type -> Integer -> APattern
APNat Type
ty Integer
n)
checkPattern p :: Pattern
p@(PCons Pattern
p1 Pattern
p2) Type
ty = do
Type
tyl <- forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Type -> Either Term Pattern -> Sem r Type
ensureConstr1 Con
CList Type
ty (forall a b. b -> Either a b
Right Pattern
p)
(TyCtx
ctx1, APattern
ap1) <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern Pattern
p1 Type
tyl
(TyCtx
ctx2, APattern
ap2) <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern Pattern
p2 (Type -> Type
TyList Type
tyl)
forall (m :: * -> *) a. Monad m => a -> m a
return (TyCtx
ctx1 forall a. Semigroup a => a -> a -> a
<> TyCtx
ctx2, Type -> APattern -> APattern -> APattern
APCons (Type -> Type
TyList Type
tyl) APattern
ap1 APattern
ap2)
checkPattern p :: Pattern
p@(PList [Pattern]
ps) Type
ty = do
Type
tyl <- forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Type -> Either Term Pattern -> Sem r Type
ensureConstr1 Con
CList Type
ty (forall a b. b -> Either a b
Right Pattern
p)
[(TyCtx, APattern)]
listCtxtAps <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
`checkPattern` Type
tyl) [Pattern]
ps
let ([TyCtx]
ctxs, [APattern]
aps) = forall a b. [(a, b)] -> ([a], [b])
unzip [(TyCtx, APattern)]
listCtxtAps
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Monoid a => [a] -> a
mconcat [TyCtx]
ctxs, Type -> [APattern] -> APattern
APList (Type -> Type
TyList Type
tyl) [APattern]
aps)
checkPattern (PAdd Side
s Pattern
p Term
t) Type
ty = do
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QNum Type
ty
(TyCtx
ctx, APattern
apt) <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern Pattern
p Type
ty
ATerm
at <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Type -> Sem r ATerm
check Term
t Type
ty
forall (m :: * -> *) a. Monad m => a -> m a
return (TyCtx
ctx, Type -> Side -> APattern -> ATerm -> APattern
APAdd Type
ty Side
s APattern
apt ATerm
at)
checkPattern (PMul Side
s Pattern
p Term
t) Type
ty = do
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QNum Type
ty
(TyCtx
ctx, APattern
apt) <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern Pattern
p Type
ty
ATerm
at <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Type -> Sem r ATerm
check Term
t Type
ty
forall (m :: * -> *) a. Monad m => a -> m a
return (TyCtx
ctx, Type -> Side -> APattern -> ATerm -> APattern
APMul Type
ty Side
s APattern
apt ATerm
at)
checkPattern (PSub Pattern
p Term
t) Type
ty = do
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QNum Type
ty
(TyCtx
ctx, APattern
apt) <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern Pattern
p Type
ty
ATerm
at <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Term -> Type -> Sem r ATerm
check Term
t Type
ty
forall (m :: * -> *) a. Monad m => a -> m a
return (TyCtx
ctx, Type -> APattern -> ATerm -> APattern
APSub Type
ty APattern
apt ATerm
at)
checkPattern (PNeg Pattern
p) Type
ty = do
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QSub Type
ty
Type
tyInner <- forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Type -> Sem r Type
cPos Type
ty
(TyCtx
ctx, APattern
apt) <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern Pattern
p Type
tyInner
forall (m :: * -> *) a. Monad m => a -> m a
return (TyCtx
ctx, Type -> APattern -> APattern
APNeg Type
ty APattern
apt)
checkPattern (PFrac Pattern
p Pattern
q) Type
ty = do
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QDiv Type
ty
Type
tyP <- forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Type -> Sem r Type
cInt Type
ty
Type
tyQ <- forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Type -> Sem r Type
cPos Type
tyP
(TyCtx
ctx1, APattern
ap1) <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern Pattern
p Type
tyP
(TyCtx
ctx2, APattern
ap2) <- forall (r :: EffectRow).
Members
'[Reader TyCtx, Reader TyDefCtx, Writer Constraint, Error TCError,
Fresh]
r =>
Pattern -> Type -> Sem r (TyCtx, APattern)
checkPattern Pattern
q Type
tyQ
forall (m :: * -> *) a. Monad m => a -> m a
return (TyCtx
ctx1 forall a. Semigroup a => a -> a -> a
<> TyCtx
ctx2, Type -> APattern -> APattern -> APattern
APFrac Type
ty APattern
ap1 APattern
ap2)
cAbs :: Members '[Writer Constraint, Fresh] r => Type -> Type -> Sem r ()
cAbs :: forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Type -> Type -> Sem r ()
cAbs Type
argTy Type
resTy = do
Type
resTy' <- forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Type -> Sem r Type
cPos Type
argTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Type -> Type -> Constraint
CEq Type
resTy Type
resTy'
cSize :: Members '[Writer Constraint, Fresh] r => Type -> Type -> Sem r ()
cSize :: forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Type -> Type -> Sem r ()
cSize Type
argTy Type
resTy = do
Type
a <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
Atom
c <- forall (r :: EffectRow). Member Fresh r => Sem r Atom
freshAtom
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Type -> Type -> Constraint
CEq (Atom -> Type -> Type
TyContainer Atom
c Type
a) Type
argTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Type -> Type -> Constraint
CEq Type
TyN Type
resTy
cPos :: Members '[Writer Constraint, Fresh] r => Type -> Sem r Type
cPos :: forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Type -> Sem r Type
cPos Type
ty = do
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QNum Type
ty
case Type
ty of
TyAtom (ABase BaseTy
b) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Atom -> Type
TyAtom (BaseTy -> Atom
ABase (BaseTy -> BaseTy
pos BaseTy
b))
Type
_ -> do
Type
res <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$
[Constraint] -> Constraint
COr
[ [Constraint] -> Constraint
cAnd [Type -> Type -> Constraint
CSub Type
ty Type
TyZ, Type -> Type -> Constraint
CSub Type
TyN Type
res]
, [Constraint] -> Constraint
cAnd [Type -> Type -> Constraint
CSub Type
ty Type
TyQ, Type -> Type -> Constraint
CSub Type
TyF Type
res]
, Type -> Type -> Constraint
CEq Type
ty Type
res
]
forall (m :: * -> *) a. Monad m => a -> m a
return Type
res
where
pos :: BaseTy -> BaseTy
pos BaseTy
Z = BaseTy
N
pos BaseTy
Q = BaseTy
F
pos BaseTy
t = BaseTy
t
cInt :: Members '[Writer Constraint, Fresh] r => Type -> Sem r Type
cInt :: forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Type -> Sem r Type
cInt Type
ty = do
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QNum Type
ty
case Type
ty of
TyAtom (ABase BaseTy
b) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Atom -> Type
TyAtom (BaseTy -> Atom
ABase (BaseTy -> BaseTy
int BaseTy
b))
Type
_ -> do
Type
res <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$
[Constraint] -> Constraint
COr
[ [Constraint] -> Constraint
cAnd [Type -> Type -> Constraint
CSub Type
ty Type
TyF, Type -> Type -> Constraint
CSub Type
TyN Type
res]
, [Constraint] -> Constraint
cAnd [Type -> Type -> Constraint
CSub Type
ty Type
TyQ, Type -> Type -> Constraint
CSub Type
TyZ Type
res]
, Type -> Type -> Constraint
CEq Type
ty Type
res
]
forall (m :: * -> *) a. Monad m => a -> m a
return Type
res
where
int :: BaseTy -> BaseTy
int BaseTy
F = BaseTy
N
int BaseTy
Q = BaseTy
Z
int BaseTy
t = BaseTy
t
cExp :: Members '[Writer Constraint, Fresh] r => Type -> Type -> Sem r Type
cExp :: forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Type -> Type -> Sem r Type
cExp Type
ty1 Type
TyN = do
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Qualifier -> Type -> Constraint
CQual Qualifier
QNum Type
ty1
forall (m :: * -> *) a. Monad m => a -> m a
return Type
ty1
cExp Type
ty1 Type
ty2 = do
Type
resTy <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Type -> Type -> Constraint
CSub Type
ty1 Type
resTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$
[Constraint] -> Constraint
COr
[ [Constraint] -> Constraint
cAnd [Qualifier -> Type -> Constraint
CQual Qualifier
QNum Type
resTy, Type -> Type -> Constraint
CEq Type
ty2 Type
TyN]
, [Constraint] -> Constraint
cAnd [Qualifier -> Type -> Constraint
CQual Qualifier
QDiv Type
resTy, Type -> Type -> Constraint
CEq Type
ty2 Type
TyZ]
]
forall (m :: * -> *) a. Monad m => a -> m a
return Type
resTy
getEltTy :: Members '[Writer Constraint, Fresh] r => Container -> Type -> Sem r Type
getEltTy :: forall (r :: EffectRow).
Members '[Writer Constraint, Fresh] r =>
Container -> Type -> Sem r Type
getEltTy Container
_ (TyContainer Atom
_ Type
e) = forall (m :: * -> *) a. Monad m => a -> m a
return Type
e
getEltTy Container
c Type
ty = do
Type
eltTy <- forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Type -> Type -> Constraint
CEq (Container -> Type -> Type
containerTy Container
c Type
eltTy) Type
ty
forall (m :: * -> *) a. Monad m => a -> m a
return Type
eltTy
ensureConstr ::
forall r.
Members '[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con ->
Type ->
Either Term Pattern ->
Sem r [Type]
ensureConstr :: forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Type -> Either Term Pattern -> Sem r [Type]
ensureConstr Con
c Type
ty Either Term Pattern
targ = Con -> Type -> Sem r [Type]
matchConTy Con
c Type
ty
where
matchConTy :: Con -> Type -> Sem r [Type]
matchConTy :: Con -> Type -> Sem r [Type]
matchConTy Con
c1 (TyUser String
name [Type]
args) = forall (r :: EffectRow).
Members '[Reader TyDefCtx, Error TCError] r =>
String -> [Type] -> Sem r Type
lookupTyDefn String
name [Type]
args forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Con -> Type -> Sem r [Type]
matchConTy Con
c1
matchConTy Con
c1 (TyCon Con
c2 [Type]
tys) = do
Con -> Con -> Sem r ()
matchCon Con
c1 Con
c2
forall (m :: * -> *) a. Monad m => a -> m a
return [Type]
tys
matchConTy Con
c1 tyv :: Type
tyv@(TyAtom (AVar (U Name Type
_))) = do
[Type]
tyvs <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall a b. a -> b -> a
const forall (r :: EffectRow). Member Fresh r => Sem r Type
freshTy) (Con -> [Variance]
arity Con
c1)
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Type -> Type -> Constraint
CEq Type
tyv (Con -> [Type] -> Type
TyCon Con
c1 [Type]
tyvs)
forall (m :: * -> *) a. Monad m => a -> m a
return [Type]
tyvs
matchConTy Con
_ Type
_ = forall a. Sem r a
matchError
matchCon :: Con -> Con -> Sem r ()
matchCon :: Con -> Con -> Sem r ()
matchCon Con
c1 Con
c2 | Con
c1 forall a. Eq a => a -> a -> Bool
== Con
c2 = forall (m :: * -> *) a. Monad m => a -> m a
return ()
matchCon (CContainer v :: Atom
v@(AVar (U Name Type
_))) (CContainer Atom
ctr2) =
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Type -> Type -> Constraint
CEq (Atom -> Type
TyAtom Atom
v) (Atom -> Type
TyAtom Atom
ctr2)
matchCon (CContainer Atom
ctr1) (CContainer v :: Atom
v@(AVar (U Name Type
_))) =
forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Type -> Type -> Constraint
CEq (Atom -> Type
TyAtom Atom
ctr1) (Atom -> Type
TyAtom Atom
v)
matchCon Con
_ Con
_ = forall a. Sem r a
matchError
matchError :: Sem r a
matchError :: forall a. Sem r a
matchError = case Either Term Pattern
targ of
Left Term
term -> forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw (Con -> Term -> Type -> TCError
NotCon Con
c Term
term Type
ty)
Right Pattern
pat -> forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw (Con -> Pattern -> Type -> TCError
PatternType Con
c Pattern
pat Type
ty)
ensureConstr1 ::
Members '[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con ->
Type ->
Either Term Pattern ->
Sem r Type
ensureConstr1 :: forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Type -> Either Term Pattern -> Sem r Type
ensureConstr1 Con
c Type
ty Either Term Pattern
targ = do
[Type]
tys <- forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Type -> Either Term Pattern -> Sem r [Type]
ensureConstr Con
c Type
ty Either Term Pattern
targ
case [Type]
tys of
[Type
ty1] -> forall (m :: * -> *) a. Monad m => a -> m a
return Type
ty1
[Type]
_ ->
forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$
String
"Impossible! Wrong number of arg types in ensureConstr1 "
forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Con
c
forall a. [a] -> [a] -> [a]
++ String
" "
forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Type
ty
forall a. [a] -> [a] -> [a]
++ String
": "
forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show [Type]
tys
ensureConstr2 ::
Members '[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con ->
Type ->
Either Term Pattern ->
Sem r (Type, Type)
ensureConstr2 :: forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Type -> Either Term Pattern -> Sem r (Type, Type)
ensureConstr2 Con
c Type
ty Either Term Pattern
targ = do
[Type]
tys <- forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Type -> Either Term Pattern -> Sem r [Type]
ensureConstr Con
c Type
ty Either Term Pattern
targ
case [Type]
tys of
[Type
ty1, Type
ty2] -> forall (m :: * -> *) a. Monad m => a -> m a
return (Type
ty1, Type
ty2)
[Type]
_ ->
forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$
String
"Impossible! Wrong number of arg types in ensureConstr2 "
forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Con
c
forall a. [a] -> [a] -> [a]
++ String
" "
forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Type
ty
forall a. [a] -> [a] -> [a]
++ String
": "
forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show [Type]
tys
ensureConstrMode ::
Members '[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con ->
Mode ->
Either Term Pattern ->
Sem r [Mode]
ensureConstrMode :: forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Mode -> Either Term Pattern -> Sem r [Mode]
ensureConstrMode Con
c Mode
Infer Either Term Pattern
_ = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall a b. a -> b -> a
const Mode
Infer) (Con -> [Variance]
arity Con
c)
ensureConstrMode Con
c (Check Type
ty) Either Term Pattern
tp = forall a b. (a -> b) -> [a] -> [b]
map Type -> Mode
Check forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Type -> Either Term Pattern -> Sem r [Type]
ensureConstr Con
c Type
ty Either Term Pattern
tp
ensureConstrMode1 ::
Members '[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con ->
Mode ->
Either Term Pattern ->
Sem r Mode
ensureConstrMode1 :: forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Mode -> Either Term Pattern -> Sem r Mode
ensureConstrMode1 Con
c Mode
m Either Term Pattern
targ = do
[Mode]
ms <- forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Mode -> Either Term Pattern -> Sem r [Mode]
ensureConstrMode Con
c Mode
m Either Term Pattern
targ
case [Mode]
ms of
[Mode
m1] -> forall (m :: * -> *) a. Monad m => a -> m a
return Mode
m1
[Mode]
_ ->
forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$
String
"Impossible! Wrong number of arg types in ensureConstrMode1 "
forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Con
c
forall a. [a] -> [a] -> [a]
++ String
" "
forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Mode
m
forall a. [a] -> [a] -> [a]
++ String
": "
forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show [Mode]
ms
ensureConstrMode2 ::
Members '[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con ->
Mode ->
Either Term Pattern ->
Sem r (Mode, Mode)
ensureConstrMode2 :: forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Mode -> Either Term Pattern -> Sem r (Mode, Mode)
ensureConstrMode2 Con
c Mode
m Either Term Pattern
targ = do
[Mode]
ms <- forall (r :: EffectRow).
Members
'[Reader TyDefCtx, Writer Constraint, Error TCError, Fresh] r =>
Con -> Mode -> Either Term Pattern -> Sem r [Mode]
ensureConstrMode Con
c Mode
m Either Term Pattern
targ
case [Mode]
ms of
[Mode
m1, Mode
m2] -> forall (m :: * -> *) a. Monad m => a -> m a
return (Mode
m1, Mode
m2)
[Mode]
_ ->
forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$
String
"Impossible! Wrong number of arg types in ensureConstrMode2 "
forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Con
c
forall a. [a] -> [a] -> [a]
++ String
" "
forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Mode
m
forall a. [a] -> [a] -> [a]
++ String
": "
forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show [Mode]
ms
ensureEq :: Member (Writer Constraint) r => Type -> Type -> Sem r ()
ensureEq :: forall (r :: EffectRow).
Member (Writer Constraint) r =>
Type -> Type -> Sem r ()
ensureEq Type
ty1 Type
ty2
| Type
ty1 forall a. Eq a => a -> a -> Bool
== Type
ty2 = forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = forall (r :: EffectRow).
Member (Writer Constraint) r =>
Constraint -> Sem r ()
constraint forall a b. (a -> b) -> a -> b
$ Type -> Type -> Constraint
CEq Type
ty1 Type
ty2