Safe Haskell | None |
---|
Type checker for the Disciple Core language.
The functions in this module do not check for language fragment compliance. This needs to be done separately via DDC.Core.Fragment.
- data Config n = Config {
- configPrimKinds :: KindEnv n
- configPrimTypes :: TypeEnv n
- configDataDefs :: DataDefs n
- configTrackedEffects :: Bool
- configTrackedClosures :: Bool
- configFunctionalEffects :: Bool
- configFunctionalClosures :: Bool
- configEffectCapabilities :: Bool
- configNameIsHole :: Maybe (n -> Bool)
- configOfProfile :: Profile n -> Config n
- data CheckTrace = CheckTrace {
- checkTraceDoc :: Doc
- checkModule :: (Ord n, Show n, Pretty n) => Config n -> Module a n -> Mode n -> (Either (Error a n) (Module (AnTEC a n) n), CheckTrace)
- data Mode n
- checkExp :: (Ord n, Show n, Pretty n) => Config n -> KindEnv n -> TypeEnv n -> Exp a n -> Mode n -> (Either (Error a n) (Exp (AnTEC a n) n, Type n, Effect n, Closure n), CheckTrace)
- typeOfExp :: (Ord n, Pretty n, Show n) => Config n -> KindEnv n -> TypeEnv n -> Exp a n -> Either (Error a n) (Type n)
- checkWitness :: (Ord n, Show n, Pretty n) => Config n -> KindEnv n -> TypeEnv n -> Witness a n -> Either (Error a n) (Witness (AnT a n) n, Type n)
- typeOfWitness :: (Ord n, Show n, Pretty n) => Config n -> Witness a n -> Either (Error a n) (Type n)
- typeOfWiCon :: WiCon n -> Type n
- data AnTEC a n = AnTEC {
- annotType :: Type n
- annotEffect :: Effect n
- annotClosure :: Closure n
- annotTail :: a
- data Error a n
- = ErrorType {
- errorTypeError :: Error n
- | ErrorData { }
- | ErrorExportUndefined {
- errorName :: n
- | ErrorExportDuplicate {
- errorName :: n
- | ErrorExportMismatch {
- errorName :: n
- errorExportType :: Type n
- errorDefType :: Type n
- | ErrorImportDuplicate {
- errorName :: n
- | ErrorImportValueNotData {
- errorName :: n
- | ErrorMismatch {
- errorAnnot :: a
- errorInferred :: Type n
- errorExpected :: Type n
- errorChecking :: Exp a n
- | ErrorUndefinedVar {
- errorAnnot :: a
- errorBound :: Bound n
- errorUniverse :: Universe
- | ErrorUndefinedCtor {
- errorAnnot :: a
- errorChecking :: Exp a n
- | ErrorAppMismatch {
- errrorAnnot :: a
- errorChecking :: Exp a n
- errorParamType :: Type n
- errorArgType :: Type n
- | ErrorAppNotFun {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorNotFunType :: Type n
- | ErrorAppCannotInferPolymorphic {
- errorAnnot :: a
- errorChecking :: Exp a n
- | ErrorLamShadow {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorBind :: Bind n
- | ErrorLamNotPure {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorUniverse :: Universe
- errorEffect :: Effect n
- | ErrorLamNotEmpty {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorUniverse :: Universe
- errorClosure :: Closure n
- | ErrorLamBindBadKind {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorType :: Type n
- errorKind :: Kind n
- | ErrorLamBodyNotData {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorBind :: Bind n
- errorType :: Type n
- errorKind :: Kind n
- | ErrorLamParamUnannotated {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorBind :: Bind n
- | ErrorLAMParamUnannotated {
- errorAnnot :: a
- errorChecking :: Exp a n
- | ErrorLAMParamBadSort {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorBind :: Bind n
- errorSort :: Sort n
- | ErrorLetMismatch {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorBind :: Bind n
- errorType :: Type n
- | ErrorLetBindingNotData {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorBind :: Bind n
- errorKind :: Kind n
- | ErrorLetBodyNotData {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorType :: Type n
- errorKind :: Kind n
- | ErrorLetrecBindingNotLambda {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorExp :: Exp a n
- | ErrorLetrecMissingAnnot {
- errorAnnot :: a
- errorBind :: Bind n
- errorExp :: Exp a n
- | ErrorLetrecRebound {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorBind :: Bind n
- | ErrorLetRegionsNotRegion {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorBinds :: [Bind n]
- errorKinds :: [Kind n]
- | ErrorLetRegionsRebound {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorBinds :: [Bind n]
- | ErrorLetRegionFree {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorBinds :: [Bind n]
- errorType :: Type n
- | ErrorLetRegionWitnessInvalid {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorBind :: Bind n
- | ErrorLetRegionWitnessConflict {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorBindWitness1 :: Bind n
- errorBindWitness2 :: Bind n
- | ErrorLetRegionsWitnessOther {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorBoundRegions :: [Bound n]
- errorBindWitness :: Bind n
- | ErrorWithRegionNotRegion {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorBound :: Bound n
- errorKind :: Kind n
- | ErrorWithRegionFree {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorBound :: Bound n
- errorType :: Type n
- | ErrorWAppMismatch {
- errorAnnot :: a
- errorWitness :: Witness a n
- errorParamType :: Type n
- errorArgType :: Type n
- | ErrorWAppNotCtor {
- errorAnnot :: a
- errorWitness :: Witness a n
- errorNotFunType :: Type n
- errorArgType :: Type n
- | ErrorCannotJoin {
- errorAnnot :: a
- errorWitness :: Witness a n
- errorWitnessLeft :: Witness a n
- errorTypeLeft :: Type n
- errorWitnessRight :: Witness a n
- errorTypeRight :: Type n
- | ErrorWitnessNotPurity {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorWitness :: Witness a n
- errorType :: Type n
- | ErrorWitnessNotEmpty {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorWitness :: Witness a n
- errorType :: Type n
- | ErrorCaseScrutineeNotAlgebraic {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorTypeScrutinee :: Type n
- | ErrorCaseScrutineeTypeUndeclared {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorTypeScrutinee :: Type n
- | ErrorCaseNoAlternatives {
- errorAnnot :: a
- errorChecking :: Exp a n
- | ErrorCaseNonExhaustive {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorCtorNamesMissing :: [n]
- | ErrorCaseNonExhaustiveLarge {
- errorAnnot :: a
- errorChecking :: Exp a n
- | ErrorCaseOverlapping {
- errorAnnot :: a
- errorChecking :: Exp a n
- | ErrorCaseTooManyBinders {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorCtorDaCon :: DaCon n
- errorCtorFields :: Int
- errorPatternFields :: Int
- | ErrorCaseCannotInstantiate {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorTypeScrutinee :: Type n
- errorTypeCtor :: Type n
- | ErrorCaseScrutineeTypeMismatch {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorTypeScrutinee :: Type n
- errorTypePattern :: Type n
- | ErrorCaseFieldTypeMismatch {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorTypeAnnot :: Type n
- errorTypeField :: Type n
- | ErrorCaseAltResultMismatch {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorAltType1 :: Type n
- errorAltType2 :: Type n
- | ErrorWeakEffNotEff {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorEffect :: Effect n
- errorKind :: Kind n
- | ErrorRunNotSuspension {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorType :: Type n
- | ErrorRunNotSupported {
- errorAnnot :: a
- errorChecking :: Exp a n
- errorEffect :: Effect n
- | ErrorRunCannotInfer {
- errorAnnot :: a
- errorExp :: Exp a n
- | ErrorNakedType {
- errorAnnot :: a
- errorChecking :: Exp a n
- | ErrorNakedWitness {
- errorAnnot :: a
- errorChecking :: Exp a n
- = ErrorType {
Configuration
Static configuration for the type checker. These fields don't change as we decend into the tree.
The starting configuration should be converted from the profile that
defines the language fragment you are checking.
See DDC.Core.Fragment and use configOfProfile
below.
Config | |
|
configOfProfile :: Profile n -> Config nSource
Convert a langage profile to a type checker configuration.
Type checker trace
Checking Modules
:: (Ord n, Show n, Pretty n) | |
=> Config n | Static configuration. |
-> Module a n | Module to check. |
-> Mode n | Type checker mode. |
-> (Either (Error a n) (Module (AnTEC a n) n), CheckTrace) |
Type check a module.
If it's good, you get a new version with types attached to all the bound variables
If it's bad, you get a description of the error.
Checking Expressions
What mode we're performing type checking/inference in.
Recon | Reconstruct the type of the expression, requiring type annotations on parameters as well as type applications to already be present. |
Synth | The ascending smoke of incense. Synthesise the type of the expression, producing unification variables for bidirectional type inference. |
Check (Type n) | The descending tongue of flame. Check the type of an expression against this expected type, and unify expected types into unification variables for bidirecional type inference. |
:: (Ord n, Show n, Pretty n) | |
=> Config n | Static configuration. |
-> KindEnv n | Starting kind environment. |
-> TypeEnv n | Starting type environment. |
-> Exp a n | Expression to check. |
-> Mode n | Check mode. |
-> (Either (Error a n) (Exp (AnTEC a n) n, Type n, Effect n, Closure n), CheckTrace) |
Type check an expression.
If it's good, you get a new version with types attached every AST node, as well as every binding occurrence of a variable.
If it's bad, you get a description of the error.
The kinds and types of primitives are added to the environments automatically, you don't need to supply these as part of the starting kind and type environment.
:: (Ord n, Pretty n, Show n) | |
=> Config n | Static configuration. |
-> KindEnv n | Starting Kind environment |
-> TypeEnv n | Starting Type environment. |
-> Exp a n | Expression to check. |
-> Either (Error a n) (Type n) |
Like checkExp
, but only return the value type of an expression.
Checking Witnesses
:: (Ord n, Show n, Pretty n) | |
=> Config n | Static configuration. |
-> KindEnv n | Starting Kind Environment. |
-> TypeEnv n | Strating Type Environment. |
-> Witness a n | Witness to check. |
-> Either (Error a n) (Witness (AnT a n) n, Type n) |
Check a witness.
If it's good, you get a new version with types attached to all the bound variables, as well as the type of the overall witness.
If it's bad, you get a description of the error.
The returned expression has types attached to all variable occurrences,
so you can call typeOfWitness
on any open subterm.
The kinds and types of primitives are added to the environments automatically, you don't need to supply these as part of the starting environments.
typeOfWitness :: (Ord n, Show n, Pretty n) => Config n -> Witness a n -> Either (Error a n) (Type n)Source
Like checkWitness
, but check in an empty environment.
As this function is not given an environment, the types of free variables
must be attached directly to the bound occurrences.
This attachment is performed by checkWitness
above.
typeOfWiCon :: WiCon n -> Type nSource
Take the type of a witness constructor.
Annotations
The type checker adds this annotation to every node in the AST, giving its type, effect and closure.
AnTEC | |
|
Error messages
All the things that can go wrong when type checking an expression or witness.
ErrorType | Found a kind error when checking a type. |
| |
ErrorData | Found an error in the data type definitions. |
ErrorExportUndefined | Exported value is undefined. |
| |
ErrorExportDuplicate | Exported name is exported multiple times. |
| |
ErrorExportMismatch | Type signature of exported binding does not match the type at the definition site. |
| |
ErrorImportDuplicate | Imported name is imported multiple times. |
| |
ErrorImportValueNotData | An imported value that doesn't have kind Data. |
| |
ErrorMismatch | Generic mismatch between expected and inferred types. |
| |
ErrorUndefinedVar | An undefined type variable. |
| |
ErrorUndefinedCtor | A data constructor that wasn't in the set of data definitions. |
| |
ErrorAppMismatch | A function application where the parameter and argument don't match. |
| |
ErrorAppNotFun | Tried to apply something that is not a function. |
| |
ErrorAppCannotInferPolymorphic | Cannot infer type of polymorphic expression. |
| |
ErrorLamShadow | A type abstraction that tries to shadow a type variable that is already in the environment. |
| |
ErrorLamNotPure | An abstraction where the body has a visible side effect that is not supported by the current language fragment. |
| |
ErrorLamNotEmpty | An abstraction where the body has a visible closure that is not supported by the current language fragment. |
| |
ErrorLamBindBadKind | A value function where the parameter does not have data or witness kind. |
| |
ErrorLamBodyNotData | An abstraction where the body does not have data kind. |
| |
ErrorLamParamUnannotated | A function abstraction without a type annotation on the parameter. |
| |
ErrorLAMParamUnannotated | A type abstraction without a kind annotation on the parameter. |
| |
ErrorLAMParamBadSort | A type abstraction parameter with a bad sort. |
| |
ErrorLetMismatch | A let-expression where the type of the binder does not match the right of the binding. |
| |
ErrorLetBindingNotData | A let-expression where the right of the binding does not have data kind. |
| |
ErrorLetBodyNotData | A let-expression where the body does not have data kind. |
| |
ErrorLetrecBindingNotLambda | A recursive let-expression where the right of the binding is not a lambda abstraction. |
| |
ErrorLetrecMissingAnnot | A recursive let-binding with a missing type annotation. |
| |
ErrorLetrecRebound | A recursive let-expression that has more than one binding with the same name. |
| |
ErrorLetRegionsNotRegion | A letregion-expression where the some of the bound variables do not have region kind. |
| |
ErrorLetRegionsRebound | A letregion-expression that tried to shadow some pre-existing named region variables. |
| |
ErrorLetRegionFree | A letregion-expression where some of the the bound region variables are free in the type of the body. |
| |
ErrorLetRegionWitnessInvalid | A letregion-expression that tried to create a witness with an invalid type. |
| |
ErrorLetRegionWitnessConflict | A letregion-expression that tried to create conflicting witnesses. |
| |
ErrorLetRegionsWitnessOther | A letregion-expression where a bound witnesses was not for the the region variable being introduced. |
| |
ErrorWithRegionNotRegion | A withregion-expression where the handle does not have region kind. |
| |
ErrorWithRegionFree | A letregion-expression where some of the the bound region variables are free in the type of the body. |
| |
ErrorWAppMismatch | A witness application where the argument type does not match the parameter type. |
| |
ErrorWAppNotCtor | Tried to perform a witness application with a non-witness. |
| |
ErrorCannotJoin | An invalid witness join. |
| |
ErrorWitnessNotPurity | A witness provided for a purify cast that does not witness purity. |
| |
ErrorWitnessNotEmpty | A witness provided for a forget cast that does not witness emptiness. |
| |
ErrorCaseScrutineeNotAlgebraic | A case-expression where the scrutinee type is not algebraic. |
| |
ErrorCaseScrutineeTypeUndeclared | A case-expression where the scrutinee type is not in our set of data type declarations. |
| |
ErrorCaseNoAlternatives | A case-expression with no alternatives. |
| |
ErrorCaseNonExhaustive | A case-expression where the alternatives don't cover all the possible data constructors. |
| |
ErrorCaseNonExhaustiveLarge | A case-expression where the alternatives don't cover all the possible constructors, and the type has too many data constructors to list. |
| |
ErrorCaseOverlapping | A case-expression with overlapping alternatives. |
| |
ErrorCaseTooManyBinders | A case-expression where one of the patterns has too many binders. |
| |
ErrorCaseCannotInstantiate | A case-expression where the pattern types could not be instantiated with the arguments of the scrutinee type. |
| |
ErrorCaseScrutineeTypeMismatch | A case-expression where the type of the scrutinee does not match the type of the pattern. |
| |
ErrorCaseFieldTypeMismatch | A case-expression where the annotation on a pattern variable binder does not match the field type of the constructor. |
| |
ErrorCaseAltResultMismatch | A case-expression where the result types of the alternatives are not identical. |
| |
ErrorWeakEffNotEff | A weakeff-cast where the type provided does not have effect kind. |
| |
ErrorRunNotSuspension | A run cast applied to a non-suspension. |
| |
ErrorRunNotSupported | A run cast where the context does not support the suspended effect. |
| |
ErrorRunCannotInfer | A run cast where we cannot infer the type of the suspended computation and thus cannot check if its effects are suppored by the context. |
| |
ErrorNakedType | Found a naked |
| |
ErrorNakedWitness | Found a naked |
|