import TiInstanceDB -- as TiInst
------------------------------ Top level function ------------------------------
transTheory sign sens = do
sign' <- transSignature sign
(sign'',sens'') <- -- trace (show $ arities $ tsig sign') $
transSentences sign' (map sentence sens)
------------------------------ Theory translation -------------------------------
data ExpRole = FunDef | InstDef
transSentences sign ls = do xx <- mapM (transSentence sign FunDef) ls
xs <- return [[s] | [s] <- xx, extAxType s /= noType]
ys <- -- trace ("\n" ++ (show xs)) $
zz <- mapM (transSentence sign InstDef) ls
zs <- return [s | s <- zw, extAxType s /= noType]
q <- mapM (transIMatch sign t)
makeSentence :: String -> ConstTab -> IsaType -> VName -> [PrPat] -> IsaTerm -> Named Sentence
makeSentence d cs y df ps tx =
if tx == xDummy then NamedSen d True False $ ConstDef $ IsaEq xDummy $ xDummy
else NamedSen d True False $ ConstDef $
IsaEq (Const df y) $ termMAbs IsCont (map (transPat cs) ps) tx
--------------------------- translation of sentences --------------------------------
---------------------------- function definitions ---------------------------
-> Result (Named Sentence)
transMatch sign t = case (t, constTab sign) of
let df = mkVName $ showIsaName nm
------------------------------- translates instances ---------------------------------
-> Result (Named Sentence)
= case (s, constTab sign) of
df = mkVName $ showIsaName nm
c = transClass $ getInstClass t -- instantiated class
x = transType [] $ getInstType t -- instatiating type
w = maybe [] id $
Map.lookup (typeId x) (arities $ tsig sign) -- all arities of inst. type
ty = maybe (noType) id $
Map.lookup df ct -- abstract method type
nty = repVarClass ty c (constrVarClass x cs) -- in ty, replaces the variable of class c with type x, constraining variables in x with cs
in return $ makeSentence ndf ct nty df ps tx
------------------------------ Signature translation -----------------------------
------------------------------ translation of types -------------------------------
{- This signature translation takes after the one to Stratego, and relies on
transType :: [HsType] -> HsType -> IsaType
transType c t = maybe noType id $ transMType IsCont c t
transMType :: Continuity -> [HsType] -> HsType -> Maybe IsaType
transMType a c (Typ t) = transT (transIdV a c) transIdC (transMType a c) t
transT :: Show d => (PNT -> Maybe IsaType) -> (PNT -> Maybe IsaType)
transT trIdV trIdC trT t =
case mapTI3 trIdV trIdC trT t of
Just (HsTyFun t1 t2) -> return $ mkContFun t1 t2
Just (HsTyApp t1 t2) -> case t1 of
IsaSign.Type name s args -> -- better resolve nested HsTyApp first
Just (HsTyVar a) -> return a
Just (HsTyCon k) -> return k
-- _ -> not_supported "Type" t
getSort :: Continuity -> [HsType] -> PNT -> IsaSort
getSort r c t = case c of
x:cs -> let a = getInstType x
in if u == v then k:s else s
-- in if u == v && k /= IsaClass "Eq" && k /= IsaClass "Ord" then k:s else s
transIdV :: Continuity -> [HsType] -> PNT -> Maybe IsaType
transIdV a c t = return $ TFree (showIsaName t) (getSort a c t)
transIdC :: PNT -> Maybe IsaType
_ -> return $ let tfs = transFields t
transFields :: PNT -> [IsaType]
transFields t = case t of
mapTI3 :: (i1 -> Maybe i2) ->
TI i1 t1 -> Maybe (TI i2 t2)
mapTI3 vf cf tf ty = case ty of
HsTyFun x y -> do a <- tf x
HsTyApp f x -> do a <- tf f
HsTyVar nm -> do a <- vf nm
HsTyCon nm -> do a <- cf nm
-- HsTyForall xs ps t -> HsTyForall (map vf xs) (map tf ps) (tf t)
-- error :: forall a. [Char] -> a
------------------------------ Class translation ------------------------------
transClass :: HsType -> IsaClass
Typ (HsTyCon c) -> IsaClass (showIsaName c)
-- need check of parameters?
------------------------------- Kind translation ------------------------------
kindTrans :: Kind -> IsaKind
kindExTrans :: Kind -> IsaExKind
kindExTrans x = case x of
------------------------------- SIGN fields translation ---------------------------------
------------------------------- translation for ConstTab --------------------------
getConstTab :: VaMap -> ConstTab
getAConstTab :: VaMap -> AConstTab
(\y -> (transMScheme IsCont $ snd y, getValInfo $ fst y)) nothingFiOut f
getValInfo :: HsId -> IsaVT
transMScheme :: Continuity -> HsScheme -> Maybe IsaType
transMScheme a s = case s of
Forall _ _ (c TiTypes.:=> t) -> transMType a c t
transScheme :: HsScheme -> IsaType
transScheme s = maybe (error "HsHOLCF, transScheme") id $ transMScheme IsCont s
----------------------------- translation for Classrel (from KEnv) -----------------------
transClassInfo :: (Kind, HsTypeInfo) -> Maybe [IsaClass]
transClassInfo p = case snd p of
TiTypes.Class _ _ _ _ -> Just (map transClass (extClassInfo $ snd p))
------------------------- translation of Abbrs (from KEnv) -----------------------------
in
Map.map (\x -> maybe ([],noType) id x) nf
transAbbrsInfo :: (Kind, HsTypeInfo) -> Maybe ([TName], IsaType)
transAbbrsInfo p = case (snd p) of
return $ (map showIsaName (fst x), transType [] (snd x))
---------------------------- translation of arities --------------------------------
getArities db = -- trace (show $ getTypeInsts db) $ trace (show db) $
Map.fromList (groupInst $ getTypeInsts db) -- \$ getIsaInsts db
getTypeInsts :: HsInstances -> [IsaTypeInsts]
getTypeInsts db = [(typeId $ transType [] $ getMainInstType x, [transInst x]) | x <- db]
transInst :: HsInstance -> (IsaClass, [(IsaType, [IsaClass])])
transInst i = let x = prepInst i in
(transClass $ fst x, reverse [(transType [] a, map transClass b) | (a,b) <- snd x])
------------------------------ translation of domaintab ---------------------------
getDepDoms $ remove_duplicates
[getDomainEntry (getAConstTab f) x | x <-
Map.keys f, checkTyCons x]
getDomainEntry ctab t = case t of
[(getDomType ctab (mkVName $ showIsaName t), [(b, getFieldTypes ctab b)
| b <- [ mkVName $ showIsaName c
| PN c _ <- map conName (constructors d)]])]
--------------------------------- Term translation -------------------------------
-------------------------------------------- term and patterns ------------------------
transExp :: ConstTab -> PrExp -> IsaTerm
transExp c t = maybe xDummy id $ transMExp IsCont c t
transPat :: ConstTab -> PrPat -> IsaPattern
transPat c t = maybe xDummy id $ transMPat IsCont c t
transMExp :: Continuity -> ConstTab -> PrExp -> Maybe IsaTerm
transMExp a cs t = case t of
HsLit _ (HsInt n) -> return $
Const (mkVName $ "(Def (" ++ show n ++ "::int))")
HsList xs -> return $ case xs of
_ -> error "Hs2HOLCF, transMExp, unsupported list notation"
w <- mapM (transPatBind a cs) ds
return $ Let w z -- (transE showIsaName (transExp cs) (transPat cs) e)
HsCase e as -> do bs <- mapM (transCases a cs) as
_ -> transE (mkVName . showIsaName) (transMExp a cs) (transMPat a cs) e
transCases r cs k = case k of
HsAlt _ p (HsBody e) _ -> do a <- transMPat r cs p
_ -> error "HsHOLCF, transCases"
transPatBind :: Continuity -> ConstTab -> PrDecl -> Maybe (IsaTerm,IsaTerm)
transPatBind a cs s = case s of
_ -> error "HsHOLCF, transPatBind"
transMPat :: Continuity -> ConstTab -> PrPat -> Maybe IsaPattern
transMPat a cs t = case t of
HsPList _ xs -> return $ case xs of
_ -> error "Hs2HOLCF, transMPat, unsupported list notation"
_ -> transP a (mkVName . showIsaName) (transMPat a cs) p
-- _ -> error "oh so funny" -- return $ Bottom
transE :: (PNT -> VName) -> (e -> Maybe IsaTerm) -> (p -> Maybe IsaPattern) ->
case (mapEI5 trId trE trP e) of
Just (HsId (HsVar _)) -> return $ conDouble "DIC"
Just (HsApp x y) -> return $ termMAppl IsCont x [y]
Just (HsLambda ps x) -> return $ termMAbs IsCont ps x
Just (HsIf x y z) -> return $ If x y z IsCont
Just (HsTuple xs) -> return $ Tuplex xs IsCont
Just (HsParen x) -> return $ Paren x
_ -> Nothing -- Const "dummy" noType
-- HsId (HsCon c) -> Const c noType
-- HsId (HsVar "==") -> Const "Eq" noType
-- HsLet ds e -> Let (map transPatBind ds) (transExp e)
-- HsCase e ds -> Case e ds
transP :: IsaName i => Continuity -> (i -> VName) -> (p -> Maybe IsaPattern) ->
case mapPI3 trId trP p of
Just (HsPId (HsVar _)) -> return $ conDouble "DIC"
Just (HsPTuple _ xs) -> return $ Tuplex xs a
Just (HsPParen x) -> return $ Paren x
Just HsPWildCard -> return $ Wildcard
-- HsPList ps -> plist ps
-- HsPId (HsCon c) -> Const c noType
-- HsPLit _ lit -> litPat (transL lit) -- new
-- HsPApp c ps -> App x y IsCont
-- HsPAsPat x p -> AsPattern (x,p)
-- HsPIrrPat p -> twiddlePat p
transCN :: HsScheme -> PNT -> Maybe IsaTerm
f w = Const (mkVName w) z
if elem pcpo (typeSort z) then case pp x of
transHV :: Continuity -> HsScheme -> PNT -> Maybe IsaTerm
k = maybe (error "Hs2HOLCF, transHV") id $ transMScheme a s
tag = elem pcpo (typeSort k)
mkConst w = Const (mkVName w) k
mkFree w = Free (mkVName w) k
mFree w = Free (mkVName w) noType
flist = [mFree "x", mFree "y"]
in if qq == "error" then Nothing else return $
"==" -> if tag == False then mkConst eq
(termLift $ termMAppl NotCont (mkConst eq)
"&&" -> if tag == False then mkConst conj
"||" -> if tag == False then mkConst disj
"+" -> if tag == False then mkConst "op +"
else funFliftbin $ mkConst "op +"
"-" -> if tag == False then mkConst "op -"
else funFliftbin $ mkConst "op -"
"*" -> if tag == False then mkConst "op *"
else funFliftbin $ mkConst "op *"
"head" -> if tag == False then mkConst "hd"
"tail" -> if tag == False then mkConst "tl"
":" -> if tag == False then mkConst "op #"
_ -> error "Haskell2IsabelleHOLCF, transHV"
---------------------------------- auxiliary ------------------------------------------------
class IsaName i => TransFunction i j k where
Maybe (EI i2 e2 p2 d t c)
HsApp x y -> do a <- ef x
HsLambda ps e -> do xs <- mapM pf ps
HsIf x y z -> do a <- ef x
HsTuple xs -> do zs <- mapM ef xs
HsParen x -> do a <- ef x
HsList xs -> do ys <- mapM ef xs
-- HsLit s l -> HsLit s l
-- HsCase e as -> do f <- ef e
-- bs <- map (mapA ef pf) as
-- mapA f g k = case k of HsAlt _ p (HsBody e) _ ->
-- HsLit s l -> HsLit s l
-- HsInfixApp x op z -> HsInfixApp (ef x) (mapHsIdent2 vf cf op) (ef z)
-- HsNegApp s x -> HsNegApp s (ef x)
-- HsLet ds e -> HsLet (df ds) (ef e)
-- HsCase e alts -> HsCase (ef e) (map (mapAlt ef pf df) alts)
-- HsDo stmts -> HsDo (mStmt stmts)
-- HsLeftSection x op -> HsLeftSection (ef x) (mapHsIdent2 vf cf op)
-- HsRightSection op y -> HsRightSection (mapHsIdent2 vf cf op) (ef y)
-- HsRecConstr s n upds -> HsRecConstr s (cf n) (mapFieldsI vf ef upds)
-- HsRecUpdate s e upds -> HsRecUpdate s (ef e) (mapFieldsI vf ef upds)
-- HsEnumFrom x -> HsEnumFrom (ef x)
-- HsEnumFromTo x y -> HsEnumFromTo (ef x) (ef y)
-- HsEnumFromThen x y -> HsEnumFromThen (ef x) (ef y)
-- HsEnumFromThenTo x y z -> HsEnumFromThenTo (ef x) (ef y) (ef z)
-- HsListComp stmts -> HsListComp (mStmt stmts)
-- HsExpTypeSig s e c t -> HsExpTypeSig s (ef e) (ctxf c) (tf t)
-- HsAsPat n e -> HsAsPat (vf n) (ef e) -- pattern only
-- HsWildCard -> HsWildCard -- ditto
-- HsIrrPat e -> HsIrrPat (ef e) -- ditto
-- mStmt = mapStmt ef pf df
PI i1 p1 -> Maybe (PI i2 p2)
HsPLit s l -> return $ HsPLit s l
HsPTuple s ps -> do bs <- mapM pf ps
HsPParen p -> do h <- pf p
HsPWildCard -> return $ HsPWildCard
HsPList s ps -> do qs <- mapM pf ps
HsPApp nm ps -> do qs <- mapM pf ps
return $ HsPApp (vf nm) qs
HsPSucc s n l -> return $ HsPSucc s (vf n) l
-- HsPNeg s l -> HsPNeg s l
-- HsPNeg s l -> HsPNeg s l
-- HsPSucc s n l -> HsPSucc s (vf n) l
-- HsPInfixApp x op y -> HsPInfixApp (pf x) (cf op) (pf y)
-- HsPApp nm ps -> HsPApp (cf nm) (map pf ps)
-- HsPList s ps -> HsPList s (map pf ps)
-- HsPRec nm fields -> HsPRec (cf nm) (mapFieldsI vf pf fields)
-- HsPAsPat nm p -> HsPAsPat (vf nm) (pf p)
-- HsPIrrPat p -> HsPIrrPat (pf p)