TypeDecl.hs revision df638d53c2d5fe5e80b943a58609c8936848ed82
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder Authors: Christian Maeder
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder analyse type decls
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maederimport qualified Common.Lib.Map as Map
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder-- ---------------------------------------------------------------------------
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder-- analyse types as state
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder-- ---------------------------------------------------------------------------
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian MaederfromReadR :: a -> ReadR (ClassMap, TypeMap) a -> State Env a
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian MaederfromReadR a r = toState a ( \ e -> (classMap e, typeMap e)) r
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian MaederanaTypeS :: (Kind, Type) -> State Env (Kind, Type)
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian MaederanaTypeS kt = fromReadR kt $ anaType kt
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder-- check with merge
bcd232120234d3cbbfd730b64a5a165f5c16e2a9Christian MaedercompatibleTypeDefn :: TypeDefn -> TypeDefn -> Id -> [Diagnosis]
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian MaedercompatibleTypeDefn d1 d2 i =
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder if case (d1, d2) of
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder (TypeVarDefn, TypeVarDefn) -> True
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder (TypeVarDefn, _) -> False
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder (_, TypeVarDefn) -> False
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder then [] else [mkDiag Error "incompatible redeclaration of type" i]
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder-- ---------------------------------------------------------------------------
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder-- storing type ids with their kind and definition
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder-- ---------------------------------------------------------------------------
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder-- | store a complete type map
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian MaederputTypeMap :: TypeMap -> State Env ()
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian MaederputTypeMap tk = do { e <- get; put e { typeMap = tk } }
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder-- | store type id and check the kind
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian MaederaddTypeId :: TypeDefn -> Instance -> Kind -> Id -> State Env ()
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder-- type args not yet considered for kind construction
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian MaederaddTypeId defn _ kind i@(Id ts _ _) =
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder do nk <- toState kind classMap $ expandKind kind
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder let n = length $ filter isPlace ts
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder if n <= kindArity TopLevel nk then
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder addTypeKind defn i kind
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder else addDiag $ mkDiag Error "wrong arity of" i
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder-- | store prefix type ids both with and without following places
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian MaederaddTypeKind :: TypeDefn -> Id -> Kind -> State Env ()
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian MaederaddTypeKind d i k =
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder if isPrefix i then do addSingleTypeKind d i k
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder addSingleTypeKind d (stripFinalPlaces i) k
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder else addSingleTypeKind d i k
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder-- | store type as is
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian MaederaddSingleTypeKind :: TypeDefn -> Id -> Kind -> State Env ()
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian MaederaddSingleTypeKind d i k =
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder do tk <- gets typeMap
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder Nothing -> putTypeMap $ Map.insert i
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder (TypeInfo k [] [] d) tk
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder Just (TypeInfo ok ks sups defn) ->
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder -- check with merge
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder do checkKindsS i k ok
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder if any (==k) (ok:ks)
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder then addDiag $ mkDiag Warning
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder "redeclared type" i
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder else putTypeMap $ Map.insert i
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder (k:ks) sups defn) tk
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder-- | add a supertype to a given type id
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian MaederaddSuperType :: Type -> Id -> State Env ()
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian MaederaddSuperType t i =
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder do tk <- gets typeMap
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder Nothing -> return () -- previous error
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder Just (TypeInfo ok ks sups defn) ->
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder (TypeInfo ok ks (t:sups) defn)
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder-- ---------------------------------------------------------------------------
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder-- analyse type items
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder-- ---------------------------------------------------------------------------
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian MaederanaTypeItem :: GenKind -> Instance -> TypeItem -> State Env ()
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian MaederanaTypeItem _ inst (TypeDecl pats kind _) =
9eaf4ea0944f7c5a1773c5f3c066f0117ece22dbChristian Maeder do anaKindS kind
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder let Result ds (Just is) = convertTypePatterns pats
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder appendDiags ds
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder mapM_ (addTypeId NoTypeDefn inst kind) is
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian MaederanaTypeItem _ inst (SubtypeDecl pats t _) =
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder do anaTypeS (star, t)
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder let Result ds (Just is) = convertTypePatterns pats
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder appendDiags ds
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder mapM_ (addTypeId NoTypeDefn inst star) is
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder mapM_ (addSuperType t) is
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian MaederanaTypeItem _ inst (IsoDecl pats _) =
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder do let Result ds (Just is) = convertTypePatterns pats
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder appendDiags ds
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder mapM_ (addTypeId NoTypeDefn inst star) is
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder mapM_ ( \ i -> mapM_ (addSuperType (TypeName i star 0)) is) is
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian MaederanaTypeItem _ inst (SubtypeDefn pat v t f ps) =
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder do (k, newT) <- anaTypeS (star, t)
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder checkKindsS t star k
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder addDiag $ Diag Warning ("unchecked formula '" ++ showPretty f "'")
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder $ firstPos [v] ps
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder let Result ds m = convertTypePattern pat
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder appendDiags ds
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder Nothing -> return ()
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder Just i -> do addTypeId (Supertype v newT $ item f)
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder addSuperType newT i
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian MaederanaTypeItem _ inst (AliasType pat mk sc _) =
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder do (ik, newPty) <- anaPseudoType mk sc
9eaf4ea0944f7c5a1773c5f3c066f0117ece22dbChristian Maeder let Result ds m = convertTypePattern pat
9eaf4ea0944f7c5a1773c5f3c066f0117ece22dbChristian Maeder appendDiags ds
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder Just i -> addTypeId (AliasTypeDefn newPty) inst ik i
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maeder _ -> return ()
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian MaederanaTypeItem gk inst (Datatype d) = anaDatatype gk inst d
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian MaederanaDatatype :: GenKind -> Instance -> DatatypeDecl -> State Env ()
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian MaederanaDatatype genKind inst (DatatypeDecl pat kind alts derivs _) =
9eaf4ea0944f7c5a1773c5f3c066f0117ece22dbChristian Maeder do k <- anaKindS kind
9eaf4ea0944f7c5a1773c5f3c066f0117ece22dbChristian Maeder checkKindsS pat star k
9eaf4ea0944f7c5a1773c5f3c066f0117ece22dbChristian Maeder case derivs of
let l = Map.findWithDefault [] i as
else putAssumps $ Map.insert i