TypeCheck.hs revision 0df692ce8b9293499b2e1768458613a63e7b5cd0
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian MaederModule : $Header$
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkeCopyright : (c) Christian Maeder and Uni Bremen 2003
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu ProdescuLicence : All rights reserved.
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkeMaintainer : hets@tzi.de
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkeStability : experimental
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkePortability : portable
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian Maedertype inference
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartkeimport qualified Common.Lib.Map as Map
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian MaederaVar = simpleIdToId $ mkSimpleId "a"
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian MaederaType = TypeName aVar star 1
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkebindA :: Type -> TypeScheme
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkebindA ty = TypeScheme [TypeArg aVar star Other []] ([] :=> ty) []
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkeeqType, logType, defType, notType, ifType :: TypeScheme
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkeeqType = bindA $
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke FunType (ProductType [aType, aType] [])
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke PFunArr logicalType []
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkelogType = simpleTypeScheme $
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke FunType (ProductType [logicalType, logicalType] [])
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke PFunArr logicalType []
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian MaederdefType = bindA $ FunType aType PFunArr logicalType []
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian MaedernotType = simpleTypeScheme $ FunType logicalType PFunArr logicalType []
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian MaederifType = bindA $
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian Maeder FunType (ProductType [logicalType, aType, aType] [])
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian Maeder PFunArr aType []
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkebList :: [(Id, TypeScheme)]
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkebList = (defId, defType) : (notId, notType) : (ifThenElse, ifType) :
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke map ( \ e -> (e, eqType)) [eqId, exEq] ++
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke map ( \ o -> (o, logType)) [andId, orId, eqvId, implId]
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian MaederaddUnit :: TypeMap -> TypeMap
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkeaddUnit = let TypeName i k _ = logicalType in
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke Map.insertWith ( \ _ old -> old) i
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke (TypeInfo k [] [] NoTypeDefn)
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian MaederaddOps :: Assumps -> Assumps
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkeaddOps as = foldr ( \ (i, sc) m ->
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke Map.insertWith ( \ _ old -> old) i
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke (OpInfos [OpInfo sc [] NoOpDefn]) m) as bList
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkeresolveTerm :: GlobalAnnos -> Maybe Type -> Term -> State Env (Maybe Term)
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkeresolveTerm ga mt trm = do
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke mtrm <- resolve ga trm
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke case mtrm of
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke Nothing -> return Nothing
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke Just t -> typeCheck mt t
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkemUnifySc :: Maybe Type -> OpInfo
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian Maeder -> State Env (Maybe (Subst, Type, OpInfo))
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkemUnifySc mt oi = do
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian Maeder tm <- gets typeMap
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian Maeder ty <- toEnvState $ freshInst $ opType oi
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke let Result ds ms = mUnify tm mt ty
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke case ms of Nothing -> return Nothing
a23e572c8f957cc051a1b0831abd6fe9380d45c7Christian Maeder Just s -> return $ Just (s, subst s ty, oi)
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian MaedercheckList :: [Type] -> [Term] -> State Env [(Subst, [Type], [Term])]
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian MaedercheckList [] [] = return [(eps, [], [])]
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian MaedercheckList (ty : rty) (trm : rt) = do
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian Maeder fts <- infer (Just ty) trm
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian Maeder combs <- mapM ( \ (sf, tyf, tf) -> do
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke rts <- checkList (map (subst sf) rty) rt
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke mapM ( \ (sr, tys, tts) ->
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke return (compSubst sf sr,
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke subst sr tyf : tys,
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke tf : tts)) rts) fts
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke return $ concat combs
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus HartkecheckList _ _ = error "checkList"
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian MaedertypeCheck :: Maybe Type -> Term -> State Env (Maybe Term)
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian MaedertypeCheck mt trm =
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian Maeder do alts <- infer mt trm
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian Maeder if null alts then
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian Maeder do addDiags [mkDiag Error "no typing for" trm]
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke return Nothing
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke else if null $ tail alts then
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke let (_,_,t) = head alts in
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke return $ Just t
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian Maeder else do addDiags [Diag Error
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke ("ambiguous typings \n\t" ++
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke showSepList (showString "\n\t")
9f93539f2fa4f5b9d935c7baa22fcd750806def9Klaus Hartke showPretty (take 5 $ map ( \ (_,_,t) -> t) alts) "")
let ois = opInfos $ Map.findWithDefault (OpInfos []) i as