HTC API Documentation
Safe HaskellSafe-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

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

Instances details
Show SemanticError Source # 
Instance details

Defined in Semantic

Methods

showsPrec :: Int -> SemanticError -> ShowS

show :: SemanticError -> String

showList :: [SemanticError] -> ShowS

Eq SemanticError Source # 
Instance details

Defined in Semantic

data TypeError Source #

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

Instances

Instances details
Show TypeError Source # 
Instance details

Defined in Semantic

Methods

showsPrec :: Int -> TypeError -> ShowS

show :: TypeError -> String

showList :: [TypeError] -> ShowS

data Error Source #

Combined error type for semantic or type errors.

Constructors

SemErr SemanticError

A semantic error occurred

TypErr TypeError

A type error occurred

Instances

Instances details
Show Error Source # 
Instance details

Defined in Semantic

Methods

showsPrec :: Int -> Error -> ShowS

show :: Error -> String

showList :: [Error] -> ShowS

type Sig = Map Ident Int Source #

Signature mapping function identifiers to their arity.

type Env = Set Ident Source #

Environment of variables currently in scope.

data Type Source #

Types in the language, including base types, lists, tuples, type variables, and function types.

Constructors

TInt 
TFloat 
TBool 
TChar 
TString

Primitive types

TList Type

Homogeneous list types

TTuple [Type]

Tuple types with fixed arity

TVar String

Type variable for inference

TFun [Type] Type

Function type with argument types and return type

Instances

Instances details
Show Type Source # 
Instance details

Defined in Semantic

Methods

showsPrec :: Int -> Type -> ShowS

show :: Type -> String

showList :: [Type] -> ShowS

Eq Type Source # 
Instance details

Defined in Semantic

Methods

(==) :: Type -> Type -> Bool

(/=) :: Type -> Type -> Bool

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.

patVars :: Pattern -> [Ident] Source #

Extract variables from a pattern.

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.

inferExpr :: FuncEnv -> TypeEnv -> Expr -> Infer Type Source #

Infer the type of an expression.

match :: Type -> Type -> Bool Source #

Match two 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.

numBin :: BinOperator -> Type -> Type -> Infer Type Source #

Type-check numeric binary operators.

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.

ensureBool :: Expr -> Type -> Infer () Source #

Ensure an expression has boolean type.

freshTypeVar :: Infer Type Source #

Generate a fresh type variable.

isPoly :: Type -> Bool Source #

Check if a type is polymorphic (a type variable).

checkAll :: Program -> [Error] Source #

Run full semantic and type checks on a program.