Safe Haskell | Safe-Inferred |
---|
Semantic
Description
Module providing semantic analysis and type checking for a simple functional language. It includes desugaring of top-level lambdas, semantic error checking, and Hindley-Milner type inference with support for Case, Let, and generic application.
Synopsis
- desugarDecl :: Decl -> Decl
- desugarProgram :: Program -> Program
- data SemanticError
- data TypeError
- data Error
- type Sig = Map Ident Int
- type Env = Set Ident
- data Type
- type TypeEnv = Map Ident Type
- type FuncEnv = Map Ident ([Type], Type)
- data InferState = InferState {
- count :: Int
- type Infer a = ExceptT TypeError (State InferState) a
- buildSig :: [Decl] -> Sig
- buildFuncEnv :: [Decl] -> FuncEnv
- semanticCheck :: Program -> [Error]
- checkDecl :: Sig -> Decl -> [SemanticError]
- checkExpr :: Sig -> Env -> Expr -> [SemanticError]
- checkAlt :: Sig -> Env -> (Pattern, Expr) -> [SemanticError]
- flattenApp :: Expr -> (Expr, [Expr])
- patVars :: Pattern -> [Ident]
- checkProgram :: Program -> [Error]
- unifyReturn :: Type -> Type -> Infer Type
- inferExpr :: FuncEnv -> TypeEnv -> Expr -> Infer Type
- match :: Type -> Type -> Bool
- resolve :: Type -> Type -> Type
- inferPattern :: Pattern -> Infer ([(Ident, Type)], Type)
- literalType :: Literal -> Type
- numBin :: BinOperator -> Type -> Type -> Infer Type
- boolBin :: BinOperator -> Type -> Type -> Infer Type
- compBin :: BinOperator -> Type -> Type -> Infer Type
- ensureBool :: Expr -> Type -> Infer ()
- freshTypeVar :: Infer Type
- isPoly :: Type -> Bool
- checkAll :: Program -> [Error]
Documentation
desugarDecl :: Decl -> Decl Source #
Perform desugaring only when the function has no parameters and its body is a lambda abstraction.
Transform a top-level function declaration with a lambda body into an equivalent declaration with explicit parameters.
desugarProgram :: Program -> Program Source #
Desugar each declaration in the program.
Apply desugaring to all declarations in a program.
data SemanticError Source #
Semantic errors detected during scope and arity checking.
Constructors
UndefinedVar Ident | Variable used without definition |
ArityMismatch Ident Int Int | Function called with wrong number of arguments |
DuplicateFunc Ident | Function name defined more than once |
DuplicateParam Ident | Parameter name appears multiple times in declaration |
DuplicatePatternVar Ident | Pattern variable appears multiple times in the same pattern |
Instances
Show SemanticError Source # | |
Defined in Semantic Methods showsPrec :: Int -> SemanticError -> ShowS show :: SemanticError -> String showList :: [SemanticError] -> ShowS | |
Eq SemanticError Source # | |
Defined in Semantic |
Type errors detected during type inference and checking.
Constructors
Mismatch Expr Type Type | Expression has unexpected type |
CondNotBool Expr Type | Condition expression is not boolean |
BranchesTypeDiffer Expr Expr Type Type | Then/else branches have different types |
BinOpTypeErr BinOperator Type Type | Binary operator applied to incompatible types |
UnOpTypeErr UnOperator Type | Unary operator applied to non-matching type |
UnknownVar Ident | Variable not found in type environment |
Combined error type for semantic or type errors.
Constructors
SemErr SemanticError | A semantic error occurred |
TypErr TypeError | A type error occurred |
Types in the language, including base types, lists, tuples, type variables, and function types.
type TypeEnv = Map Ident Type Source #
Typing environment: map from identifiers to their inferred types.
type FuncEnv = Map Ident ([Type], Type) Source #
Function environment: map from top-level function names to their argument types and return type.
data InferState Source #
State for generating fresh type variables during inference.
Constructors
InferState | |
Fields
|
type Infer a = ExceptT TypeError (State InferState) a Source #
Inference monad combining state for fresh variables and error handling.
buildSig :: [Decl] -> Sig Source #
Build a signature from a list of function declarations. Records arity for each function, ignoring duplicates.
buildFuncEnv :: [Decl] -> FuncEnv Source #
Create an initial function environment using fresh type variables for arguments and return.
semanticCheck :: Program -> [Error] Source #
Perform semantic checks (undefined variables, arity, duplicates) on a program.
checkDecl :: Sig -> Decl -> [SemanticError] Source #
Check a single function declaration for semantic errors.
checkExpr :: Sig -> Env -> Expr -> [SemanticError] Source #
Recursively check an expression for semantic errors given current signature and environment.
checkAlt :: Sig -> Env -> (Pattern, Expr) -> [SemanticError] Source #
Check a case alternative for duplicate pattern variables and nested errors.
flattenApp :: Expr -> (Expr, [Expr]) Source #
Flatten nested applications into function and argument list.
checkProgram :: Program -> [Error] Source #
Perform both semantic and type checking on a program.
unifyReturn :: Type -> Type -> Infer Type Source #
Unify expected and actual return types, allowing type variables.
inferPattern :: Pattern -> Infer ([(Ident, Type)], Type) Source #
Infer types for pattern variables and return pattern type.
literalType :: Literal -> Type Source #
Determine the type of a literal.
boolBin :: BinOperator -> Type -> Type -> Infer Type Source #
Type-check boolean binary operators.
Type-check numeric binary operators.
compBin :: BinOperator -> Type -> Type -> Infer Type Source #
Type-check comparison binary operators.
Type-check numeric binary operators.
freshTypeVar :: Infer Type Source #
Generate a fresh type variable.