Description : printing Isabelle entities
Copyright : (c) University of Cambridge, Cambridge, England
adaption (c) Till Mossakowski, Uni Bremen 2002-2006
License : GPLv2 or higher
Maintainer : Christian.Maeder@dfki.de
Printing functions for Isabelle logic.
printIsaTheory :: String -> Sign -> [Named Sentence] -> Doc
printIsaTheory tn sign sens = let
ld = "$HETS_ISABELLE_LIB/"
use = text usesS <+> doubleQuotes (text $ ld ++ "prelude")
in text theoryS <+> text tn
$+$ text importsS <+> fsep ((if case b of
_ -> True then doubleQuotes $ text $ ld ++ bs else text bs)
: map (doubleQuotes . text) (imports sign))
$++$ printTheoryBody sign sens
printTheoryBody :: Sign -> [Named Sentence] -> Doc
printTheoryBody sig sens =
callSetup "initialize" (brackets $ sepByCommas
$ map (text . show . Quote . senAttr)
$ filter (\ s -> not (isConstDef s || isRecDef s || isInstance s)
&& senAttr s /= "") sens)
$++$ printNamedSentences sens
printNamedSentences :: [Named Sentence] -> Doc
printNamedSentences sens = case sens of
let (axs, rest) = span isAxiom sens in
text axiomsS $+$ vsep (map printNamedSen axs)
$++$ vcat (map ( \ a -> text declareS <+> text (senAttr a)
<+> brackets (text simpS))
$ filter ( \ a -> case sentence a of
b@Sentence{} -> isSimp b && senAttr a /= ""
$++$ printNamedSentences rest
let (defs, rest) = span isConstDef sens in
text defsS $+$ vsep (map printNamedSen defs)
$++$ printNamedSentences rest
printNamedSen s $++$ (case senAttr s of
n | n == "" || isRecDef s -> empty
| True -> callSetup "record" (text $ show $ Quote n))
$++$ printNamedSentences r
callSetup :: String -> Doc -> Doc
text "setup" <+> doubleQuotes (fsep [text ("Header." ++ fun), args])
data QuotedString = Quote String
instance Show QuotedString where
show (Quote s) = init . tail . show $ show s
getAxioms :: [Named Sentence] -> ([Named Sentence], [Named Sentence])
getAxioms = partition isIsaAxiom
isIsaAxiom :: Named Sentence -> Bool
isIsaAxiom s = case sentence s of
isInstance :: Named Sentence -> Bool
isInstance s = case sentence s of
isConstDef :: Named Sentence -> Bool
isConstDef s = case sentence s of
isRecDef :: Named Sentence -> Bool
isRecDef s = case sentence s of
----------------------- Printing functions -----------------------------
showBaseSig :: BaseSig -> String
showBaseSig = takeWhile (/= '_') . show
printClass :: IsaClass -> Doc
printClass (IsaClass x) = text x
printSort = printSortAux False
printSortAux :: Bool -> Sort -> Doc
printSortAux b l = case l of
_ -> (if b then doubleQuotes else id)
. braces . hcat . punctuate comma $ map printClass l
data SynFlag = Quoted | Unquoted | Null
bar = punctuate $ space <> text "|"
printType = printTyp Unquoted
printTyp :: SynFlag -> Typ -> Doc
printTyp a = fst . printTypeAux a
printTypeAux :: SynFlag -> Typ -> (Doc, Int)
printTypeAux a t = case t of
d = text $ if isPrefixOf "\'" v || isPrefixOf "?\'" v
in if null s then d else case a of
Quoted -> d <> doubleColon <> if null
$ tail s then c else doubleQuotes c
Unquoted -> d <> doubleColon <> c
TVar iv s -> printTypeAux a $ TFree ("?\'" ++ unindexed iv) s
Type name _ args -> case args of
[t1, t2] | elem name [prodS, sProdS, funS, cFunS, lFunS, sSumS] ->
[arg] -> let (d, i) = printTypeAux a arg in
if i < 1000 then parens d else d
_ -> parens $ hsep $ punctuate comma $
map (fst . printTypeAux a) args)
printTypeOp :: SynFlag -> TName -> Typ -> Typ -> (Doc, Int)
printTypeOp x name r1 r2 =
let (d1, i1) = printTypeAux x r1
(d2, i2) = printTypeAux x r2
d3 = if i1 < l then parens d1 else d1
d4 = if i2 < r then parens d2 else d2
in (d3 <+> text name <+> d4, r)
and_docs ds = vcat $ prepPunctuate (text andS <> space) ds
-- | printing a named sentence
printNamedSen :: Named Sentence -> Doc
_ -> let dd = doubleQuotes d in
if isRefute s then text lemmaS <+> text lab <+> colon
else if null lab then dd else fsep[ (case s of
ConstDef {} -> text $ lab ++ "_def"
(if b then empty else text theoremS)
<+> text lab <+> (if b then text "[rule_format]" else
if isSimp s then text "[simp]" else empty)
_ -> error "printNamedSen") <+> colon, dd] $+$ case s of
Sentence {} -> if b then empty else case thmProof s of
printSentence :: Sentence -> Doc
printSentence s = case s of
TypeDef nt td pr -> text typedefS
<+> doubleQuotes(printSetDecl td)
RecDef kw xs -> text kw <+>
and_docs (map (vcat . map (doubleQuotes . printTerm)) xs)
Instance { tName = t, arityArgs = args, arityRes = res, definitions = defs,
text instantiationS <+> text t <> doubleColon <> (case args of
_ -> parens $ hsep $ punctuate comma $ map (printSortAux True) args)
<+> printSortAux True res $+$ text beginS $++$ printDefs defs $++$
text instanceS <+> pretty prf $+$ text endS
where printDefs :: [(String, Term)] -> Doc
printDefs defs' = vcat (map printDef defs')
printDef :: (String, Term) -> Doc
printNamedSen (makeNamed name (ConstDef def))
Sentence { isRefuteAux = b, metaTerm = t } -> printPlainMetaTerm (not b) t
ConstDef t -> printTerm t
Lemmas name lemmas -> if null lemmas
then empty -- only have this lemmas if we have some in
else text lemmasS <+> text name <+>
equals <+> sep (map text lemmas)
printSetDecl :: SetDecl -> Doc
SubSet v t f -> braces $ printTerm v <> doubleColon <> printType t <> dot
FixedSet elems -> braces $ sepByCommas $ map printTerm elems
printPlainMetaTerm :: Bool -> MetaTerm -> Doc
printPlainMetaTerm b mt = case mt of
Term t -> printPlainTerm b t
Conditional conds t -> sep
<+> fsep (punctuate semi $ map printTerm conds)
, text metaImplS <+> printTerm t ]
printTerm = printPlainTerm True
printPlainTerm :: Bool -> Term -> Doc
printPlainTerm b = fst . printTrm b
-- | print parens but leave a space if doc starts or ends with a bar
parensForTerm :: Doc -> Doc
in parens $ if null s then d
else (if head s == b then (space <>) else id)
((if last s == b then (<> space) else id) d)
printParenTerm :: Bool -> Int -> Term -> Doc
printParenTerm b i t = case printTrm b t of
(d, j) -> if j < i then parensForTerm d else d
flatTuplex :: [Term] -> Continuity -> [Term]
flatTuplex cs c = case cs of
Tuplex rs@(_ : _ : _) d | d == c -> init cs ++ flatTuplex rs d
printMixfixAppl :: Bool -> Continuity -> Term -> [Term] -> (Doc, Int)
printMixfixAppl b c f args = case f of
Const (VName n (Just (AltSyntax s is i))) (Hide _ _ _) ->
if length is == length args &&
(b || n == cNot || isPrefixOf "op " n) then
(fsep $ replaceUnderlines s
$ zipWith (printParenTerm b) is args, i)
Const vn _ | new vn `elem` [allS, exS, ex1S] -> case args of
[Abs v t _] -> (fsep [text (new vn) <+> printPlainTerm False v
, printPlainTerm b t], lowPrio)
App g a d | c == d -> printMixfixAppl b c g (a : args)
-- | print the term using the alternative syntax (if True)
printTrm :: Bool -> Term -> (Doc, Int)
printTrm b trm = case trm of
Disp w _ _ -> parens $ dvn <+> doubleColon <+> printType w
Nothing -> (nvn, maxPrio)
Just (AltSyntax s is i) -> if b && null is then
(fsep $ replaceUnderlines s [], i) else (nvn, maxPrio)
Free vn -> (text $ new vn, maxPrio)
Abs v t c -> ((text $ case c of
IsCont _ -> "Lam") <+> printPlainTerm False v <> dot
<+> printPlainTerm b t, lowPrio)
If i t e c -> let d = fsep [printPlainTerm b i,
NotCont -> (text "if" <+> d, lowPrio)
IsCont _ -> (text "IF" <+> d <+> text "FI", maxPrio)
Case e ps -> (text "case" <+> printPlainTerm b e <+> text "of"
$+$ vcat (bar $ map (\ (p, t) ->
fsep [ printPlainTerm b p <+> text "=>"
, printParenTerm b (lowPrio + 1) t]) ps), lowPrio)
Let es i -> (fsep [text "let" <+>
map (\ (p, t) -> fsep [ printPlainTerm b p <+> equals
, printPlainTerm b t]) es)
, text "in" <+> printPlainTerm b i], lowPrio)
(fsep [ printParenTerm b (isaEqPrio + 1) t1 <+> isaEquals
, printParenTerm b isaEqPrio t2], isaEqPrio)
NotCont -> (parensForTerm
$ sepByCommas (map (printPlainTerm b)
[] -> error "IsaPrint, printTrm"
a:aa -> printTrm b $ App (App
lpairTerm a $ IsCont False)
(Tuplex aa c) (IsCont False)
App f a c -> printMixfixAppl b c f [a]
Set setdecl -> (printSetDecl setdecl, lowPrio)
printApp :: Bool -> Continuity -> Term -> [Term] -> (Doc, Int)
printApp b c t l = case l of
_ -> printDocApp b c (printParenTerm b (maxPrio - 1) t) l
printDocApp :: Bool -> Continuity -> Doc -> [Term] -> (Doc, Int)
IsCont True -> punctuate $ text " $$"
IsCont False -> punctuate $ text " $")
$ d : map (printParenTerm b maxPrio) l
replaceUnderlines :: String -> [Doc] -> [Doc]
replaceUnderlines str l = case str of
'\'': r@(q : s) -> if q `elem` "_/'()"
then consDocBarSep (text [q]) $ replaceUnderlines s l
else consDocBarSep (text "'") $ replaceUnderlines r l
h : t -> consDocBarSep h $ replaceUnderlines r t
_ -> error "replaceUnderlines"
'/' : ' ' : r -> empty : replaceUnderlines r l
q : r -> if q `elem` "()/" then replaceUnderlines r l
else consDocBarSep (text [q]) $ replaceUnderlines r l
consDocBarSep :: Doc -> [Doc] -> [Doc]
consDocBarSep d r = case r of
in if null hs || null ds then (d <> h) : t else
if hhs == b && lds == '(' || last ds == b && hhs == ')'
printClassrel :: Classrel -> Doc
printClassrel = vcat . map printClassR . (orderCDecs .
Map.toList)
printClassR :: (IsaClass,[IsaClass]) -> Doc
printClassR (y,ys) = case ys of
z : zs -> text axclassS <+> printClass y <+> less <+> printClass z
text instanceS <+> printClass y <+> less <+>
printClass x <+> text dotDot) zs)
orderCDecs :: [(IsaClass, Maybe [IsaClass])] -> [(IsaClass,[IsaClass])]
ws = [(x,ys) | (x,Just ys) <- ls]
crord m n = elem (fst n) (snd m)
printMonArities :: String -> Arities -> Doc
printMonArities tn = vcat . map ( \ (t, cl) ->
printThMorp :: String -> TName -> (IsaClass, [(Typ, Sort)]) -> Doc
printThMorp tn t xs = case xs of
if (isSuffixOf "_mh" tn) || (isSuffixOf "_mhc" tn)
else error "IsaPrint, printInstance: monads not supported"
printMInstance :: String -> TName -> Doc
printMInstance tn t = let nM = text (t ++ "_tm")
in prnThymorph nM "MonadType" tn t [("
MonadType.M","'a")] []
$+$ text "t_instantiate MonadOps mapping" <+> nM
brackMapList (\x -> t ++ "_" ++ x)
$+$ text "without_syntax"
$+$ text (t ++ "_eta_def:") <+> doubleQuotes
(text (t ++ "_eta") <+> isaEquals <+> text ("return_" ++ t))
$+$ text (t ++ "_bind_def:") <+> doubleQuotes
(text (t ++ "_bind") <+> isaEquals <+> text ("mbind_" ++ t))
$++$ prnThymorph nM2 "MonadAxms" tn t [("
MonadType.M","'a")]
$+$ text "t_instantiate Monad mapping" <+> nM2
brackMapList (\x -> t ++ "_" ++ x)
$+$ text "without_syntax"
lunitLemma w = text lemmaS <+> text (w ++ "_lunit:")
<+> doubleQuotes (text (w ++ "_bind")
<+> parens (text (w ++ "_eta x"))
<+> parens (text $ "t::'a => 'b " ++ w)
<+> equals <+> text "t x")
runitLemma w = text lemmaS <+> text (w ++ "_runit:")
<+> doubleQuotes (text (w ++ "_bind")
<+> parens (text $ "t::'a " ++ w) <+> text (w ++ "_eta")
assocLemma w = text lemmaS <+> text (w ++ "_assoc:")
<+> doubleQuotes ((text $ w ++ "_bind")
<+> parens ((text $ w ++ "_bind")
<+> parens (text $ "s::'a " ++ w) <+> text "t") <+> text "u"
<+> equals <+> text (w ++ "_bind s")
<+> parens ((text "%x.") <+>
(text $ w ++ "_bind") <+> text "(t x) u"))
etaInjLemma w = text lemmaS <+> text (w ++ "_eta_inj:")
<+> doubleQuotes (parens (text $ w ++ "_eta::'a => 'a " ++ w)
<+> equals <+> (text $ w ++ "_eta y")
<+> text "==>" <+> text "x = y")
prnThymorph :: Doc -> String -> String -> TName -> [(String, String)]
-> [(String, String)] -> Doc
prnThymorph nm xn tn t ts ws = let qual s = tn ++ "." ++ s in
text "thymorph" <+> nm <+> colon <+>
text xn <+> cfun <+> text tn
$+$ text " maps" <+> brackets
(hcat [ parens $ doubleQuotes (text b <+> text a) <+> mapsto
<+> doubleQuotes (text b <+> text (qual t))
brackMapList :: (String -> String) -> [(String,String)] -> Doc
brackMapList f ws = brackets $ hsep $ punctuate comma
[ parens $ doubleQuotes (text a) <+> mapsto <+> doubleQuotes (text $ f b)
-- filter out types that are given in the domain table
printTypeDecls :: DomainTab -> Arities -> Doc
let dt =
Map.fromList $ map (\ (t, _) -> (typeId t, [])) $ concat odt
printTycon :: (TName, [(IsaClass, [(Typ, Sort)])]) -> Doc
printTycon (t, arity') = case arity' of
if elem t ["lBool","intT","integerT","charT","ratT","lString"
,"unitT","unit","bool","int","char","rat","string"
,"lOrdering","sOrdering","either","*"
,"llist","list","lprod","lEither","lMaybe","option"]
(if null rs then empty else
parens $ hsep $ punctuate comma
$ map (text . ("'a" ++) . show . snd) $ number rs) <+> text t
-- | show alternative syntax (computed by comorphisms)
printAlt (VName _ altV) = case altV of
Just (AltSyntax s is i) -> parens $ doubleQuotes (text s)
<+> if null is then empty else text (show is) <+>
if i == maxPrio then empty else text (show i)
instance Pretty Sign where
-- | a dummy constant table with wrong types
constructors :: DomainTab -> ConstTab
. concatMap (map fst . snd) . concat
printMonSign :: Sign -> Doc
printMonSign sig = let ars = arities $ tsig sig
printMonArities (theoryName sig) ars
cmpDomainEntries :: [(Typ, [(VName, [Typ])])] -> [(Typ, [(VName, [Typ])])]
cmpDomainEntries l1 l2 = let
a1 = concatMap (concatMap snd . snd) l1
a2 = concatMap (concatMap snd . snd) l2
in case (null $ intersect t1 a2, null $ intersect t2 a1) of
(False, False) -> error "cmpDomainEntries"
printSign sig = let dt = sortBy cmpDomainEntries $ domainTab sig
printAbbrs (abbrs $ tsig sig) $++$
printTypeDecls dt ars $++$
printClassrel (classrel $ tsig sig) $++$
then showCaseLemmata (domainTab sig) else empty)
printAbbrs tab = if
Map.null tab then empty else text typesS
printAbbr (n, (vs, t)) = (case vs of
_ -> parens $ hsep $ punctuate comma $
map (\x -> text $ "\'" ++ x) vs)
<+> (text $ n) <+> equals <+> (doubleQuotes $ printType t)
printConstTab tab = if
Map.null tab then empty else text constsS
printConst (vn, t) = text (new vn) <+> doubleColon <+>
doubleQuotes (printType t) <+> printAlt vn
isDomain = case baseSig sig of
printDomainDefs dtDefs = vcat $ map printDomainDef dtDefs
printDomainDef dts = if null dts then empty else
text (if isDomain then domainS else datatypeS)
<+> and_docs (map printDomain dts)
printTyp (if isDomain then Quoted else Null) t <+> equals <+>
fsep (bar $ map printDOp ops)
printDOp (vn, args) = let opname = new vn in
text opname <+> hsep (map (printDOpArg opname) $ number args)
printDOpArg o (a, i) = let
TFree _ _ -> printTyp Null a
_ -> doubleQuotes $ printTyp Null a
text (o ++ "_" ++ show i) <> doubleColon <> d
showCaseLemmata dtDefs = text (concat $ map showCaseLemmata1 dtDefs)
showCaseLemmata1 dts = concat $ map showCaseLemma dts
showCaseLemma (_, []) = ""
showCaseLemma (tyCons, (c : cns)) =
let cs = "case caseVar of" ++ sp
sc b = showCons b c ++ (concat $ map ((" | " ++)
showCons b ((VName {new=cName}), args) =
let pat = cName ++ (concat $ map ((sp ++) . showArg) args)
term = showCaseTerm cName args
if b then pat ++ "Some" ++ sp ++ lb ++ term ++ rb ++ "\n"
showCaseTerm name args = if null name then sa
else [toLower (head name)] ++ sa
where sa = (concat $ map ((sp ++) . showArg) args)
showArg (TFree [] _) = "varName"
showArg (TFree (n:ns) _) = [toLower n] ++ ns
showArg (TVar v s) = showArg (TFree (unindexed v) s)
showArg (Type [] _ _) = "varName"
showArg (Type m@(n:ns) _ s) =
if m == "typeAppl" || m == "fun" || m == "*"
then concat $ map showArg s
showName (TVar v _) = unindexed v
showName (Type n _ _) = n
proof' = "apply (case_tac caseVar)\napply (auto)\ndone\n"
lemmaS ++ sp ++ "case_" ++ showName tyCons ++ "_SomeProm" ++ sp
++ "[simp]:\"" ++ sp ++ lb ++ cs ++ clSome ++ rb ++ sp
++ "=\n" ++ "Some" ++ sp ++ lb ++ cs ++ cl ++ rb ++ "\"\n"
instance Pretty Sentence where
-- Pretty printing of proofs
instance Pretty IsaProof where
printIsaProof :: IsaProof -> Doc
printIsaProof (IsaProof p e) = fsep $ map pretty p ++ [pretty e]
instance Pretty ProofCommand where
pretty = printProofCommand
printProofCommand :: ProofCommand -> Doc
let plusDoc = if plus then (text "+") else empty
in text applyS <+> (parens $
(sepByCommas $ map pretty pms)) <> plusDoc
Using ls -> text usingS <+> fsep (map text ls)
Defer x -> text deferS <+> pretty x
Prefer x -> text preferS <+> pretty x
instance Pretty ProofEnd where
printProofEnd :: ProofEnd -> Doc
By pm -> text byS <+> parens (pretty pm)
instance Pretty Modifier where
printModifier :: Modifier -> Doc
No_asm_simp -> text "no_asm_simp"
No_asm_use -> text "no_asm_use"
instance Pretty ProofMethod where
pretty = printProofMethod
printProofMethod :: ProofMethod -> Doc
AutoSimpAdd m names -> let modDoc = case m of
Just mod' -> parens $ pretty mod'
in fsep $ [text autoS, text simpS, modDoc,
text "add:"] ++ map text names
SimpAdd m names -> let modDoc = case m of
Just mod' -> parens $ pretty mod'
in fsep $ [text simpS, modDoc, text "add:"] ++
Induct var -> (text inductS) <+> doubleQuotes (printTerm var)
CaseTac t -> text caseTacS <+> doubleQuotes (printTerm t)
SubgoalTac t -> text subgoalTacS <+> doubleQuotes (printTerm t)
Insert ts -> fsep $ (text insertS:(map text ts))