module IL.Type
(
TypeVariableWithKind
, Module (..), Decl (..), ConstrDecl (..), NewConstrDecl (..), Type (..)
, Kind (..), Literal (..), ConstrTerm (..), Expression (..), Eval (..)
, Alt (..), Binding (..)
) where
import Curry.Base.Ident
import Base.Expr
data Module = Module ModuleIdent [ModuleIdent] [Decl]
deriving (Eq, Show)
data Decl
= DataDecl QualIdent [Kind] [ConstrDecl]
| NewtypeDecl QualIdent [Kind] NewConstrDecl
| ExternalDataDecl QualIdent [Kind]
| FunctionDecl QualIdent [(Type, Ident)] Type Expression
| ExternalDecl QualIdent Int Type
deriving (Eq, Show)
data NewConstrDecl = NewConstrDecl QualIdent Type
deriving (Eq, Show)
data ConstrDecl = ConstrDecl QualIdent [Type]
deriving (Eq, Show)
type TypeVariableWithKind = (Int, Kind)
data Type
= TypeConstructor QualIdent [Type]
| TypeVariable Int
| TypeArrow Type Type
| TypeForall [TypeVariableWithKind] Type
deriving (Eq, Show)
data Kind
= KindStar
| KindVariable Int
| KindArrow Kind Kind
deriving (Eq, Ord, Show)
data Literal
= Char Char
| Int Integer
| Float Double
deriving (Eq, Show)
data ConstrTerm
= LiteralPattern Type Literal
| ConstructorPattern Type QualIdent [(Type, Ident)]
| VariablePattern Type Ident
deriving (Eq, Show)
data Expression
= Literal Type Literal
| Variable Type Ident
| Function Type QualIdent Int
| Constructor Type QualIdent Int
| Apply Expression Expression
| Case Eval Expression [Alt]
| Or Expression Expression
| Exist Ident Type Expression
| Let Binding Expression
| Letrec [Binding] Expression
| Typed Expression Type
deriving (Eq, Show)
data Eval
= Rigid
| Flex
deriving (Eq, Show)
data Alt = Alt ConstrTerm Expression
deriving (Eq, Show)
data Binding = Binding Ident Expression
deriving (Eq, Show)
instance Expr Expression where
fv (Variable _ v) = [v]
fv (Apply e1 e2) = fv e1 ++ fv e2
fv (Case _ e alts) = fv e ++ fv alts
fv (Or e1 e2) = fv e1 ++ fv e2
fv (Exist v _ e) = filter (/= v) (fv e)
fv (Let (Binding v e1) e2) = fv e1 ++ filter (/= v) (fv e2)
fv (Letrec bds e) = filter (`notElem` vs) (fv es ++ fv e)
where (vs, es) = unzip [(v, e') | Binding v e' <- bds]
fv (Typed e _) = fv e
fv _ = []
instance Expr Alt where
fv (Alt (ConstructorPattern _ _ vs) e) = filter (`notElem` map snd vs) (fv e)
fv (Alt (VariablePattern _ v) e) = filter (v /=) (fv e)
fv (Alt _ e) = fv e