import ParseTerm(isSimpleId)
import MixfixParser(getTokenList, expandPos)
----------------------------------------------------------------------------
-----------------------------------------------------------------------------
anaBasicSpec :: BasicSpec -> State Env ()
anaBasicSpec (BasicSpec l) = mapM_ anaAnnotedBasicItem l
anaAnnotedBasicItem :: Annoted BasicItem -> State Env ()
anaAnnotedBasicItem i = anaBasicItem $ item i
anaBasicItem :: BasicItem -> State Env ()
anaBasicItem (SigItems i) = anaSigItems i
anaBasicItem (ClassItems inst l _) = mapM_ (anaAnnotedClassItem inst) l
anaBasicItem (GenVarItems l _) = mapM_ anaGenVarDecl l
anaSigItems :: SigItems -> State Env ()
anaSigItems(TypeItems inst l _) = mapM_ (anaAnnotedTypeItem inst) l
----------------------------------------------------------------------------
-----------------------------------------------------------------------------
anaGenVarDecl :: GenVarDecl -> State Env ()
anaGenVarDecl(GenVarDecl v) = optAnaVarDecl v
anaGenVarDecl(GenTypeVarDecl t) = anaTypeVarDecl t
convertTypeToClass :: ClassMap -> Type -> Result Class
convertTypeToClass cMap (TypeToken t) =
if tokStr t == "Type" then Result [] (Just $ universe) else
Result [] (Just $ Intersection [ci] [])
convertTypeToClass cMap (BracketType Parens ts ps) =
let is = map (convertTypeToClass cMap) ts
in if all isJust mis then Result ds
(Just $ Intersection (concatMap (iclass . fromJust) mis) ps)
convertTypeToClass _ _ = Result [] Nothing
optAnaVarDecl, anaVarDecl :: VarDecl -> State Env ()
optAnaVarDecl vd@(VarDecl v t s q) =
let Result _ mc = convertTypeToClass cMap t
Just c -> anaTypeVarDecl(TypeVarDecl v (Kind [] c []) s q)
anaVarDecl(VarDecl v oldT _ p) =
let l = lookupWithDefaultFM as [] v
ts = SimpleTypeScheme t in
else putAssumps $ addToFM as v (ts:l)
anaTypeVarDecl :: TypeVarDecl -> State Env ()
anaTypeVarDecl(TypeVarDecl t k _ _) =
-- ------------------------------------------------------------------------------ ClassItem
-- ----------------------------------------------------------------------------
anaAnnotedClassItem :: Instance -> Annoted ClassItem -> State Env ()
anaAnnotedClassItem _ aci =
let ClassItem d l _ = item aci in
mapM_ anaAnnotedBasicItem l
-- ----------------------------------------------------------------------------
-- ----------------------------------------------------------------------------
anaAnnotedTypeItem :: Instance -> Annoted TypeItem -> State Env ()
anaAnnotedTypeItem inst i = anaTypeItem inst $ item i
anaTypeItem :: Instance -> TypeItem -> State Env ()
anaTypeItem inst (TypeDecl pats kind _) =
mapM_ (anaTypePattern inst k) pats
anaTypePattern :: Instance -> Kind -> TypePattern -> State Env ()
-- type args not yet considered for kind construction
anaTypePattern _ kind t =
let Result ds mi = convertTypePattern t
in if typePatternArgs t == 0 ||
typePatternArgs t == kindArity kind then
Just ti -> addTypeKind ti kind
Nothing -> appendDiags ds
else appendDiags [Diag Error "non-matching kind arity"
convertTypePattern, makeMixTypeId :: TypePattern -> Result Id
convertTypePattern (TypePattern t _ _) = return t
convertTypePattern(TypePatternToken t) =
if isPlace t then fatal_error ("illegal type '__'") (tokPos t)
else return $ (simpleIdToId t)
if hasPlaces t && hasTypeArgs t then
fatal_error ( "illegal mix of '__' and '(...)'" )
typePatternToTokens :: TypePattern -> [Token]
typePatternToTokens (TypePattern ti _ _) = getTokenList True ti
typePatternToTokens (TypePatternToken t) = [t]
typePatternToTokens (MixfixTypePattern ts) = concatMap typePatternToTokens ts
typePatternToTokens (BracketTypePattern pk ts ps) =
let tts = map typePatternToTokens ts
expand = expandPos (:[]) in
Parens -> if length tts == 1 &&
length (head tts) == 1 then head tts
else concat $ expand "(" ")" tts ps
Squares -> concat $ expand "[" "]" tts ps
Braces -> concat $ expand "{" "}" tts ps
typePatternToTokens (TypePatternArgs as) =
map ( \ (TypeArg v _ _ _) -> Token "__" (tokPos v)) as
-- compound Ids not supported yet
getToken :: GenParser Token st Token
getToken = token tokStr (( \ (l, c) -> newPos "" l c) . tokPos) Just
parseTypePatternId :: GenParser Token st Id
case parse parseTypePatternId "" (typePatternToTokens t) of
Left err -> fatal_error (showErrorMessages "or" "unknown parse error"
"expecting" "unexpected" "end of input"
(let p = errorPos err in (sourceLine p, sourceColumn p))
typePatternArgs :: TypePattern -> Int
typePatternArgs (TypePattern _ as _) = length as
typePatternArgs (TypePatternToken t) = if isPlace t then 1 else 0
typePatternArgs (MixfixTypePattern ts) = sum (map typePatternArgs ts)
typePatternArgs (BracketTypePattern _ ts _) = sum (map typePatternArgs ts)
typePatternArgs (TypePatternArgs as) = length as
hasPlaces, hasTypeArgs :: TypePattern -> Bool
hasPlaces (TypePattern _ _ _) = False
hasPlaces (TypePatternToken t) = isPlace t
hasPlaces (MixfixTypePattern ts) = any hasPlaces ts
hasPlaces (BracketTypePattern Parens _ _) = False
hasPlaces (BracketTypePattern _ ts _) = any hasPlaces ts
hasPlaces (TypePatternArgs _) = False
hasTypeArgs (TypePattern _ _ _) = True
hasTypeArgs (TypePatternToken _) = False
hasTypeArgs (MixfixTypePattern ts) = any hasTypeArgs ts
hasTypeArgs (BracketTypePattern Parens _ _) = True
hasTypeArgs (BracketTypePattern _ ts _) = any hasTypeArgs ts
hasTypeArgs (TypePatternArgs _) = True
-- ----------------------------------------------------------------------------
-- ----------------------------------------------------------------------------
addTypeKind :: Id -> Kind -> State Env ()
Just ks -> do appendDiags [Diag Warning
putTypeKinds $ addToFM tk t (k:ks)
_ -> putTypeKinds $ addToFM tk t [k]
-- add instances later on
Nothing -> do appendDiags [Error ("undeclared class '"
Just info -> do put $ e { classEnv =
[] :=> (ci `IsIn` TCon (Tycon t k))