0N/ACopyright : (c) Christian Maeder and Uni Bremen 2002-2003
0N/AMaintainer : maeder@tzi.de
0N/AStability : provisional
0N/APortability : portable
0N/AidsToTypePatterns :: [Maybe (Id, [TypeArg])] -> [TypePattern]
0N/AidsToTypePatterns mis = map ( \ (i, as) -> TypePattern i as [] )
0N/AanaFormula :: GlobalAnnos -> Annoted Term -> State Env (Maybe (Annoted Term))
0N/A do mt <- resolveTerm ga (Just logicalType) $ item at
0N/A return $ case mt of Nothing -> Nothing
0N/A Just e -> Just at { item = e }
0N/AanaVars :: Vars -> Type -> Result [VarDecl]
0N/AanaVars (Var v) t = return [VarDecl v t Other []]
0N/AanaVars (VarTuple vs _) t =
if length ts == length vs then
let lrs = zipWith anaVars vs ts
lms = map maybeResult lrs in
return $ concatMap fromJust lms
else Result (concatMap diags lrs) Nothing
else mkError "wrong arity" t
_ -> mkError "product type expected" t
mapAnMaybe :: (Monad m) => (a -> m (Maybe b)) -> [Annoted a] -> m [Annoted b]
return $ map ( \ a -> replaceAnnoted (fromJust $ item a) a) $
filter (isJust . item) il
anaTypeItems :: GlobalAnnos -> GenKind -> Instance -> [Annoted TypeItem]
-> State Env [Annoted TypeItem]
anaTypeItems ga gk inst l = do
ul <- mapAnMaybe ana1TypeItem l
let tys = map ( \ (Datatype d) -> dataPatToType d) $
filter ( \ t -> case t of
_ -> False) $ map item ul
rl <- mapAnMaybe (anaTypeItem ga gk inst tys) ul
addDataSen :: [DataPat] -> State Env ()
let tis = map ( \ (i, _, _) -> i) tys
Just ti -> case typeDefn ti of
DatatypeDefn dd -> dd : dl
sen = NamedSen ("ga_" ++ showSepList (showString "_") showId tis "")
if null tys then return () else appendSentences [sen]
ana1TypeItem :: TypeItem -> State Env (Maybe TypeItem)
ana1TypeItem (Datatype d) =
return $ fmap Datatype md
ana1TypeItem t = return $ Just t
-- | analyse a 'TypeItem'
anaTypeItem :: GlobalAnnos -> GenKind -> Instance -> [DataPat] -> TypeItem
-> State Env (Maybe TypeItem)
anaTypeItem _ _ inst _ (TypeDecl pats kind ps) =
let Result ds (Just is) = convertTypePatterns pats
mis <- mapM (addTypePattern NoTypeDefn inst ak) is
return $ if null mis then Nothing else
Just $ TypeDecl (idsToTypePatterns mis) ak ps
anaTypeItem _ _ inst _ (SubtypeDecl pats t ps) =
do let Result ds (Just is) = convertTypePatterns pats
let Result _ mp = anaType (Nothing, t) tm
mis <- mapM (addTypePattern NoTypeDefn inst star) is
let newPats = idsToTypePatterns mis
Nothing -> return $ Just $ TypeDecl newPats star ps
Just newT -> do mapM_ (addSuperType newT) $ map fst is
return $ Just $ SubtypeDecl newPats newT ps
mis <- mapM (addTypePattern NoTypeDefn inst ak) is
let newPats = idsToTypePatterns mis
mapM_ (addSuperType newT) $ map fst is
return $ Just $ SubtypeDecl newPats newT ps
anaTypeItem _ _ inst _ (IsoDecl pats ps) =
do let Result ds (Just is) = convertTypePatterns pats
mis <- mapM (addTypePattern NoTypeDefn inst star) is
mapM_ ( \ i -> mapM_ (addSuperType (TypeName i star 0)) js) js
return $ if null mis then Nothing else
Just $ IsoDecl (idsToTypePatterns mis) ps
anaTypeItem ga _ inst _ (SubtypeDefn pat v t f ps) =
do let Result ds m = convertTypePattern pat
Nothing -> return Nothing
newAs <- mapM anaTypeVarDecl as
let nAs = catMaybes newAs
newPat = TypePattern i nAs []
Nothing -> return Nothing
newPty <- generalizeS $ TypeScheme nAs ty []
let fullKind = typeArgsListToKind nAs star
Result es mvds = anaVars v $ monoType ty
altDecl = Just $ AliasType newPat (Just fullKind)
altAct = addTypeId True (AliasTypeDefn newPty) inst
fullKind i >> return altDecl
if cyclicType i ty then do
"illegal recursive subtype definition" ty]
addTypeId True (Supertype v newPty
return $ Just $ SubtypeDefn newPat v ty
anaTypeItem _ _ inst _ (AliasType pat mk sc ps) =
do let Result ds m = convertTypePattern pat
Nothing -> return Nothing
newAs <- mapM anaTypeVarDecl as
(ik, mt) <- anaPseudoType mk sc
let nAs = catMaybes newAs
Nothing -> return Nothing
Just (TypeScheme args ty qs) ->
do addDiags [mkDiag Error
"illegal recursive type synonym" ty]
fullKind = typeArgsListToKind nAs ik
allSc = TypeScheme allArgs ty qs
newPty <- generalizeS allSc
addTypeId True (AliasTypeDefn newPty)
return $ Just $ AliasType (TypePattern i [] [])
(Just fullKind) newPty ps
anaTypeItem _ gk inst tys (Datatype d) =
do mD <- anaDatatype gk inst tys d
Nothing -> return Nothing
Just newD -> return $ Just $ Datatype newD
ana1Datatype :: DatatypeDecl -> State Env (Maybe DatatypeDecl)
ana1Datatype (DatatypeDecl pat kind alts derivs ps) =
addDiags $ checkKinds pat star k
let rms = map ( \ c -> anaClassId c cm) derivs
jcs = catMaybes $ map maybeResult rms
newDerivs = foldr( \ ck l -> case ck of
Result ds m = convertTypePattern pat
addDiags (ds ++ concatMap diags rms)
addDiags $ concatMap (checkKinds pat star) jcs
Nothing -> return Nothing
newAs <- mapM anaTypeVarDecl as
let nAs = catMaybes newAs
fullKind = typeArgsListToKind nAs k
addDiags $ checkUniqueTypevars nAs
mi <- addTypeId False PreDatatype Plain fullKind i
Just _ -> Just $ DatatypeDecl (TypePattern i nAs [])
dataPatToType :: DatatypeDecl -> DataPat
dataPatToType (DatatypeDecl (TypePattern i nAs _) k _ _ _) =
dataPatToType _ = error "dataPatToType"
-- | add a supertype to a given type id
addSuperType :: Type -> Id -> State Env ()
Nothing -> return () -- previous error
Just ti@(TypeInfo ok ks sups defn) ->
addDiags[mkDiag Error "illegal supertype for variable" t]
else if any (== t) sups then
addDiags[mkDiag Hint "repeated supertype" t]
(TypeInfo ok ks (t:sups) defn) tm
addDataSubtype :: DataPat -> Type -> State Env ()
TypeName i _ _ -> addSuperType (typeIdToType dt) i
_ -> addDiags [mkDiag Warning "data subtype ignored" st]
-- | analyse a 'DatatypeDecl'
anaDatatype :: GenKind -> Instance -> [DataPat]
-> DatatypeDecl -> State Env (Maybe DatatypeDecl)
anaDatatype genKind inst tys
d@(DatatypeDecl (TypePattern i nAs _) k alts _ _) =
do let dt = dataPatToType d
fullKind = typeArgsListToKind nAs k
mapM_ (addTypeVarDecl False) nAs
mNewAlts <- fromResult (anaAlts tys dt (map item alts) . typeMap)
Nothing -> return Nothing
mapM_ (addDataSubtype dt) $ foldr
( \ (Construct mc ts _ _) l -> case mc of
mapM_ ( \ (Construct mc tc p sels) -> case mc of
let ty = TypeScheme nAs (getConstrType dt p tc) []
addOpId c sc [] (ConstructData i)
mapM_ ( \ (Select ms ts pa) -> case ms of
Just s -> addOpId s (getSelType dt pa ts) []
$ SelectData [ConstrInfo c sc] i
Nothing -> return Nothing) $ concat sels) newAlts
let de = DataEntry
Map.empty i genKind nAs newAlts
addTypeId True (DatatypeDefn de) inst fullKind i
appendSentences $ makeDataSelEqs de k
anaDatatype _ _ _ _ = error "anaDatatype (not preprocessed)"
-- | analyse a pseudo type (represented as a 'TypeScheme')
anaPseudoType :: Maybe Kind -> TypeScheme -> State Env (Kind, Maybe TypeScheme)
anaPseudoType mk (TypeScheme tArgs ty p) =
Nothing -> return Nothing
Just j -> fromResult $ anaKindM j
tm <- gets typeMap -- save global variables
mapM_ anaTypeVarDecl tArgs
mp <- fromResult (anaType (k, ty) . typeMap)
putTypeMap tm -- forget local variables
Nothing -> return (star, Nothing)
let newK = typeArgsListToKind tArgs sk
Just j -> addDiags $ checkKinds ty j newK
return (newK, Just $ TypeScheme tArgs newTy p)
addTypePattern :: TypeDefn -> Instance -> Kind -> (Id, [TypeArg])
-> State Env (Maybe (Id, [TypeArg]))
addTypePattern defn inst kind (i, as) =
newAs <- mapM anaTypeVarDecl as
let nAs = catMaybes newAs
fullKind = typeArgsListToKind nAs kind
addDiags $ checkUniqueTypevars nAs
mId <- addTypeId True defn inst fullKind i
Just newId -> Just (newId, nAs)
-- | convert type patterns
convertTypePatterns :: [TypePattern] -> Result [(Id, [TypeArg])]
convertTypePatterns [] = Result [] $ Just []
convertTypePatterns (s:r) =
let Result d m = convertTypePattern s
Result ds (Just l) = convertTypePatterns r
Nothing -> Result (d++ds) $ Just l
Just i -> Result (d++ds) $ Just (i:l)
illegalTypePattern, illegalTypePatternArg, illegalTypeId
:: TypePattern -> Result a
illegalTypePattern tp = mkError "illegal type pattern" tp
illegalTypePatternArg tp = mkError "illegal type pattern argument" tp
illegalTypeId tp = mkError "illegal type pattern identifier" tp
-- | convert a 'TypePattern'
convertTypePattern :: TypePattern -> Result (Id, [TypeArg])
convertTypePattern (TypePattern t as _) = return (t, as)
convertTypePattern tp@(TypePatternToken t) =
if isPlace t then illegalTypePattern tp
else return (simpleIdToId t, [])
convertTypePattern tp@(MixfixTypePattern
[ra, ri@(TypePatternToken inTok), rb]) =
if head (tokStr inTok) `elem` signChars
then let inId = Id [Token place $ get_pos ra, inTok,
Token place $ get_pos rb] [] [] in
(TypePatternToken (Token "__" _),
TypePatternToken (Token "__" _)) -> return (inId, [])
_ -> do a <- convertToTypeArg ra
TypePatternToken t1 -> do
return (simpleIdToId t1, [a, b])
_ -> illegalTypePattern tp
convertTypePattern tp@(MixfixTypePattern (TypePatternToken t1 : rp)) =
[TypePatternToken inId, TypePatternToken t2] ->
if isPlace t2 && head (tokStr inId) `elem` signChars
then return (Id [t1,inId,t2] [] [], [])
else illegalTypePattern tp
_ -> illegalTypePattern tp
[BracketTypePattern Squares as@(_:_) ps] -> do
is <- mapM convertToId as
return (Id [t1] is ps, [])
_ -> do as <- mapM convertToTypeArg rp
return (simpleIdToId t1, as)
convertTypePattern (BracketTypePattern bk [ap] ps) =
Parens -> convertTypePattern ap
_ -> let (o, c) = getBrackets bk
tid = Id [Token o ps, Token place $ get_pos ap,
TypePatternToken t -> if isPlace t then
else return (tid, [TypeArg (simpleIdToId t) MissingKind Other []])
_ -> do a <- convertToTypeArg ap
convertTypePattern tp = illegalTypePattern tp
convertToTypeArg :: TypePattern -> Result TypeArg
convertToTypeArg tp@(TypePatternToken t) =
if isPlace t then illegalTypePatternArg tp
else return $ TypeArg (simpleIdToId t) MissingKind Other []
convertToTypeArg (TypePatternArg a _) = return a
convertToTypeArg (BracketTypePattern Parens [tp] _) = convertToTypeArg tp
convertToTypeArg tp = illegalTypePatternArg tp
convertToId :: TypePattern -> Result Id
convertToId tp@(TypePatternToken t) =
if isPlace t then illegalTypeId tp
else return $ Id [t] [] []
convertToId (MixfixTypePattern []) = error "convertToId: MixfixTypePattern []"
convertToId (MixfixTypePattern (hd:tps)) =
if null tps then convertToId hd
let (toks, comps) = break ( \ p ->
case p of BracketTypePattern Squares (_:_) _ -> True
ts <- mapM convertToToks (hd:toks)
(is, ps) <- if null comps then return ([], [])
else convertToIds $ head comps
pls <- if null comps then return []
else mapM convertToPlace $ tail comps
return $ Id (concat ts ++ pls) is ps
convertToIds :: TypePattern -> Result ([Id], [Pos])
convertToIds (BracketTypePattern Squares tps@(_:_) ps) = do
is <- mapM convertToId tps
convertToIds tp = illegalTypeId tp
convertToToks :: TypePattern -> Result [Token]
convertToToks (TypePatternToken t) = return [t]
convertToToks (BracketTypePattern bk [tp] ps) = case bk of
Parens -> illegalTypeId tp
_ -> do let [o,c] = mkBracketToken bk ps
convertToToks(MixfixTypePattern tps) = do
ts <- mapM convertToToks tps
convertToToks tp = illegalTypeId tp
convertToPlace :: TypePattern -> Result Token
convertToPlace tp@(TypePatternToken t) =
if isPlace t then return t
convertToPlace tp = illegalTypeId tp