Le.hs revision facf15c975d25ca5d31d8f84bf48f09d1d951ad6
5c933f3c61d2cfa7e76e4eb610a4b0bac988be47Christian MaederModule : $Header$
66267bcb678a9c341272c323b299337bcfdb7cc5Christian MaederDescription : the abstract syntax for analysis and final signature instance
66267bcb678a9c341272c323b299337bcfdb7cc5Christian MaederCopyright : (c) Christian Maeder and Uni Bremen 2003-2005
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian MaederLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
ffd01020a4f35f434b912844ad6e0d6918fadffdChristian MaederMaintainer : Christian.Maeder@dfki.de
66267bcb678a9c341272c323b299337bcfdb7cc5Christian MaederStability : experimental
66267bcb678a9c341272c323b299337bcfdb7cc5Christian MaederPortability : portable
fb69cd512eab767747f109e40322df7cae2f7bdfChristian Maederabstract syntax during static analysis
05e2a3161e4589a717c6fe5c7306820273a473c5Christian Maederimport qualified Data.Map as Map
05e2a3161e4589a717c6fe5c7306820273a473c5Christian Maederimport qualified Data.Set as Set
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederimport qualified Common.Lib.State as State
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- * class info
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | store the raw kind and all superclasses of a class identifier
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederdata ClassInfo = ClassInfo
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder { rawKind :: RawKind
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian Maeder , classKinds :: Set.Set Kind
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian Maeder } deriving (Show, Eq)
05e2a3161e4589a717c6fe5c7306820273a473c5Christian Maeder-- | mapping class identifiers to their definition
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maedertype ClassMap = Map.Map Id ClassInfo
024621f43239cfe9629e35d35a8669fad7acbba2Christian Maeder-- * type info
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | data type generatedness indicator
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maederdata GenKind = Free | Generated | Loose deriving (Show, Eq, Ord)
27912d626bf179b82fcb337077e5cd9653bb71cfChristian Maeder-- | an analysed alternative with a list of (product) types
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederdata AltDefn =
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Construct (Maybe Id) [Type] Partiality [[Selector]]
5c933f3c61d2cfa7e76e4eb610a4b0bac988be47Christian Maeder -- only argument types
5c933f3c61d2cfa7e76e4eb610a4b0bac988be47Christian Maeder deriving (Show, Eq, Ord)
8c81b727b788d90ff3b8cbda7b0900c9009243bbChristian Maeder-- | an analysed component
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederdata Selector =
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Select (Maybe Id) Type Partiality deriving (Show, Eq, Ord)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder -- only result type
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | a mapping of type (and disjoint class) identifiers
07b1bf56f3a486f26d69514d05b73100abb25a0eChristian Maedertype IdMap = Map.Map Id Id
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | for data types the morphism needs to be kept as well
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederdata DataEntry =
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder DataEntry IdMap Id GenKind [TypeArg] RawKind (Set.Set AltDefn)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder deriving (Show, Eq, Ord)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | possible definitions for type identifiers
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederdata TypeDefn =
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder | PreDatatype -- auxiliary entry for DatatypeDefn
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder | DatatypeDefn DataEntry
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder | AliasTypeDefn Type
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder deriving (Show, Eq)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | for type identifiers also store the raw kind, instances and supertypes
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederdata TypeInfo = TypeInfo
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder { typeKind :: RawKind
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , otherTypeKinds :: Set.Set Kind
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , superTypes :: Set.Set Id -- only declared or direct supertypes?
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , typeDefn :: TypeDefn
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder } deriving Show
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederinstance Eq TypeInfo where
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder t1 == t2 = (typeKind t1, otherTypeKinds t1, superTypes t1)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder == (typeKind t2, otherTypeKinds t2, superTypes t2)
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder-- | mapping type identifiers to their definition
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maedertype TypeMap = Map.Map Id TypeInfo
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | the minimal information for a sort
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederstarTypeInfo :: TypeInfo
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederstarTypeInfo = TypeInfo rStar (Set.singleton universe) Set.empty NoTypeDefn
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | rename the type according to identifier map (for comorphisms)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedermapType :: IdMap -> Type -> Type
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedermapType m ty = if Map.null m then ty else
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder rename ( \ i k n ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder let t = TypeName i k n in
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder if n == 0 then
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Just j -> TypeName j k 0
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- * sentences
5c933f3c61d2cfa7e76e4eb610a4b0bac988be47Christian Maeder-- | data types are also special sentences because of their properties
5c933f3c61d2cfa7e76e4eb610a4b0bac988be47Christian Maederdata Sentence =
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder | DatatypeSen [DataEntry]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder | ProgEqSen Id TypeScheme ProgEq
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder deriving (Show, Eq, Ord)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- * variables
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | type variable are kept separately
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederdata TypeVarDefn = TypeVarDefn Variance VarKind RawKind Int deriving Show
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | mapping type variables to their definition
76647324ed70f33b95a881b536d883daccf9568dChristian Maedertype LocalTypeVars = Map.Map Id TypeVarDefn
8aea46773664711e0910accc5cf80ef9ee1bcfbfChristian Maeder-- | the type of a local variable
8aea46773664711e0910accc5cf80ef9ee1bcfbfChristian Maederdata VarDefn = VarDefn Type deriving Show
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder-- * assumptions
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | name and scheme of a constructor
8aea46773664711e0910accc5cf80ef9ee1bcfbfChristian Maederdata ConstrInfo = ConstrInfo
8aea46773664711e0910accc5cf80ef9ee1bcfbfChristian Maeder { constrId :: Id
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , constrType :: TypeScheme
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder } deriving (Show, Eq, Ord)
8aea46773664711e0910accc5cf80ef9ee1bcfbfChristian Maeder-- | possible definitions of functions
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder NoOpDefn OpBrand
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder | ConstructData Id -- ^ target type
61091743da1a9ed6dfd5e077fdcc972553358962Christian Maeder | SelectData (Set.Set ConstrInfo) Id -- ^ constructors of source type
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder | Definition OpBrand Term
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder deriving (Show, Eq)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | scheme, attributes and definition for function identifiers
5d7e4bf173534e7eb3fc84dce7bb0151079d3f8aChristian Maederdata OpInfo = OpInfo
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder { opType :: TypeScheme
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , opAttrs :: Set.Set OpAttr
5d7e4bf173534e7eb3fc84dce7bb0151079d3f8aChristian Maeder , opDefn :: OpDefn
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder } deriving Show
5d7e4bf173534e7eb3fc84dce7bb0151079d3f8aChristian Maederinstance Eq OpInfo where
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder o1 == o2 = compare o1 o2 == EQ
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederinstance Ord OpInfo where
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder compare o1 o2 = compare (opType o1) $ opType o2
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder-- | test for constructor
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederisConstructor :: OpInfo -> Bool
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederisConstructor o = case opDefn o of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder ConstructData _ -> True
405b95208385572f491e1e0207d8d14e31022fa6Christian Maeder-- | mapping operation identifiers to their definition
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- * the local environment and final signature
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | the signature is established by the classes, types and assumptions
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederdata Env = Env
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder { classMap :: ClassMap
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , typeMap :: TypeMap
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , localTypeVars :: LocalTypeVars
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , assumps :: Assumps
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , localVars :: Map.Map Id VarDefn
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , sentences :: [Named Sentence]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , envDiags :: [Diagnosis]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder , preIds :: (PrecMap, Set.Set Id)
05e2a3161e4589a717c6fe5c7306820273a473c5Christian Maeder , globAnnos :: GlobalAnnos
05e2a3161e4589a717c6fe5c7306820273a473c5Christian Maeder , counter :: Int
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder } deriving Show
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maederinstance Eq Env where
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder e1 == e2 = (classMap e1, typeMap e1, assumps e1) ==
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder (classMap e2, typeMap e2, assumps e2)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | the empty environment (fresh variables start with 1)
341d00318de2d0ea9b6f0ab43f7e4d10ee4fb454Christian MaederinitialEnv :: Env
341d00318de2d0ea9b6f0ab43f7e4d10ee4fb454Christian MaederinitialEnv = Env
, localTypeVars = Map.empty
, assumps = Map.empty
, localVars = Map.empty
, preIds = (emptyPrecMap, Set.empty)
{- utils for singleton sets that could also be part of "Data.Set". These
functions rely on 'Data.Set.size' being computable in constant time and
isSingleton :: Set.Set a -> Bool
isSingleton s = Set.size s == 1
hasMany :: Set.Set a -> Bool
hasMany s = Set.size s > 1
addDiags :: [Diagnosis] -> State.State Env ()
e <- State.get
State.put $ e {envDiags = reverse ds ++ envDiags e}
appendSentences :: [Named Sentence] -> State.State Env ()
e <- State.get
State.put $ e {sentences = reverse fs ++ sentences e}
putClassMap :: ClassMap -> State.State Env ()
e <- State.get
State.put e { classMap = ce }
e <- State.get
State.put e { localVars = vs }
fromResult :: (Env -> Result a) -> State.State Env (Maybe a)
e <- State.get
putLocalTypeVars :: LocalTypeVars -> State.State Env ()
e <- State.get
State.put e { localTypeVars = tvs }
putTypeMap :: TypeMap -> State.State Env ()
e <- State.get
State.put e { typeMap = tm }
putAssumps :: Assumps -> State.State Env ()
e <- State.get
State.put e { assumps = ops }
checkUniqueVars :: [VarDecl] -> State.State Env ()
type FunMap = Map.Map (Id, TypeScheme) (Id, TypeScheme)
, typeIdMap = Map.empty
, funMap = Map.empty }
type SymbolMap = Map.Map Symbol Symbol
type SymbolSet = Set.Set Symbol
type RawSymbolMap = Map.Map RawSymbol RawSymbol