0N/ADescription : printing Isabelle entities
0N/ACopyright : (c) University of Cambridge, Cambridge, England
0N/A adaption (c) Till Mossakowski, Uni Bremen 2002-2006
0N/AMaintainer : Christian.Maeder@dfki.de
0N/AStability : provisional
0N/APortability : portable
0N/APrinting functions for Isabelle logic.
0N/AprintIsaTheory :: String -> String -> Sign -> [Named Sentence] -> Doc
0N/AprintIsaTheory tn _ sign sens = let
0N/A use = text usesS <+> doubleQuotes (text $ ld ++ "prelude")
0N/A in text theoryS <+> text tn
0N/A $+$ text importsS <+> (if case b of
0N/A _ -> True then doubleQuotes
0N/A $ text $ ld ++ bs else text bs)
0N/A $++$ printTheoryBody sign sens
0N/AprintTheoryBody :: Sign -> [Named Sentence] -> Doc
0N/AprintTheoryBody sig sens =
0N/A getAxioms $ filter ( \ ns -> sentence ns /= mkSen true) sens
1178N/A (rdefs, ts) = getRecDefs rs
0N/A tNames = map senName $ ts ++ axs
0N/A callML "initialize" (brackets $ sepByCommas
1178N/A $ map (text . show . Quote) tNames) $++$
0N/A (if null axs then empty else text axiomsS $+$
0N/A vsep (map printNamedSen axs)) $++$
1178N/A (if null defs then empty else text defsS $+$
0N/A vsep (map printNamedSen defs)) $++$
0N/A vsep (map printNamedSen rdefs) $++$
1178N/A vcat (map ( \ a -> text declareS <+> text (senName a)
0N/A $ filter ( \ a -> case sentence a of
0N/A b@Sentence{} -> isSimp b
0N/A _ -> False) axs) $++$
0N/A vsep (map ( \ t -> printNamedSen t $+$
0N/A Sentence { thmProof = Just s } -> s
0N/A $++$ callML "record" (text $ show $ Quote $ senName t)) ts)
0N/A $++$ printMonSign sig
0N/AcallML :: String -> Doc -> Doc
0N/A text mlS <+> doubleQuotes (fsep [text ("Header." ++ fun), args])
0N/Adata QuotedString = Quote String
0N/Ainstance Show QuotedString where
0N/A show (Quote s) = init . tail . show $ show s
0N/AgetAxioms, getDefs, getRecDefs :: [Named Sentence] ->
0N/A ([Named Sentence], [Named Sentence])
0N/AgetAxioms = partition ( \ s -> case sentence s of
0N/A Sentence {} -> isAxiom s
0N/AgetDefs = partition ( \ s -> case sentence s of
1178N/AgetRecDefs = partition ( \ s -> case sentence s of
0N/A------------------- Printing functions -------------------
0N/AshowBaseSig :: BaseSig -> String
0N/AshowBaseSig = reverse . drop 4 . reverse . show
1178N/AprintClass :: IsaClass -> Doc
0N/AprintClass (IsaClass x) = text x
0N/A _ -> specBraces . hsep . punctuate comma $ map printClass l
1178N/Adata SynFlag = Quoted | Unquoted | Null
0N/AdoubleColon = text "::"
0N/Abar :: [Doc] -> [Doc]
0N/Abar = punctuate $ space <> text "|"
0N/AprintType = printTyp Unquoted
0N/AprintTyp :: SynFlag -> Typ -> Doc
1178N/AprintTyp a = fst . printTypeAux a
0N/AprintTypeAux :: SynFlag -> Typ -> (Doc, Int)
0N/AprintTypeAux a t = case t of
1178N/A (TFree v s) -> (let d = text $ if isPrefixOf "\'" v || isPrefixOf "?\'" v
0N/A then v else '\'' : v
0N/A in if null s then d else case a of
1178N/A Quoted -> d <> doubleColon <> if null $ tail s then c else doubleQuotes c
0N/A Unquoted -> d <> doubleColon <> c
0N/A (TVar iv s) -> printTypeAux a $ TFree ("?\'" ++ unindexed iv) s
0N/A (Type name _ args) -> case args of
1178N/A [t1, t2] | elem name [prodS, sProdS, funS, cFunS, sSumS] ->
0N/A printTypeOp a name t1 t2
0N/A [arg] -> let (d, i) = printTypeAux a arg in
1178N/A if i < 1000 then parens d else d
0N/A _ -> parens $ hsep $ punctuate comma $
0N/A map (fst . printTypeAux a) args)
0N/A <+> text name, 1000)
1178N/AprintTypeOp :: SynFlag -> TName -> Typ -> Typ -> (Doc, Int)
0N/AprintTypeOp x name r1 r2 =
0N/A let (d1, i1) = printTypeAux x r1
0N/A (d2, i2) = printTypeAux x r2
0N/A d3 = if i1 < l then parens d1 else d1
1178N/A d4 = if i2 < r then parens d2 else d2
1178N/A in (d3 <+> text name <+> d4, r)
1178N/Aand_docs ds = vcat $ prepPunctuate (text andS <> space) ds
0N/A-- | printing a named sentence
0N/AprintNamedSen :: 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 empty)
_ -> error "printNamedSen") <+> colon, dd]
printSentence :: Sentence -> Doc
printSentence s = case s of
RecDef kw xs -> text kw <+>
and_docs (map (vcat . map (doubleQuotes . printTerm)) xs)
_ -> printPlainTerm (not $ isRefute s) $ senTerm s
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))) _ -> let l = length is in
case compare l $ length args of
EQ -> if b || n == cNot || isPrefixOf "op " n then
(fsep $ replaceUnderlines s
$ zipWith (printParenTerm b) is args, i)
LT -> let (fargs, rargs) = splitAt l args
(d, p) = printMixfixAppl b c f fargs
e = if p < maxPrio - 1 then parensForTerm d else d
in printDocApp b c e rargs
GT -> printApp b c f args
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
_ -> parens $ dvn <+> doubleColon <+> printType ty
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 <> text "."
<+> printPlainTerm b t, lowPrio)
If i t e c -> let d = fsep [printPlainTerm b i,
text "then" <+> printPlainTerm b t,
text "else" <+> printPlainTerm b e]
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 <+> text "="
, printPlainTerm b t]) es)
, text "in" <+> printPlainTerm b i], lowPrio)
IsaEq t1 t2 -> (fsep [ printParenTerm b (isaEqPrio + 1) t1 <+> text "=="
, printParenTerm b isaEqPrio t2], isaEqPrio)
Tuplex cs c -> ((case c of
IsCont -> \ d -> text "<" <+> d <+> text ">") $
sepByCommas (map (printPlainTerm b) $ flatTuplex cs c)
App f a c -> printMixfixAppl b c f [a]
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 -> 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 cons (text [q]) $ replaceUnderlines s l
else cons (text "'") $ replaceUnderlines r l
h : t -> cons h $ replaceUnderlines r t
_ -> error "replaceUnderlines"
'/' : ' ' : r -> empty : replaceUnderlines r l
q : r -> if q `elem` "()/" then replaceUnderlines r l
else cons (text [q]) $ replaceUnderlines r l
cons :: Doc -> [Doc] -> [Doc]
in if null hs || null ds then (d <> h) : t else
if hhs == b && lds == '('
|| last ds == b && hhs == ')' then (d <+> h) : t
printClassrel :: Classrel -> Doc
printClassrel = vcat . map ( \ (t, cl) -> case cl of
Just x -> text axclassS <+> printClass t <+> text "<" <+>
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"
printArities :: String -> Arities -> Doc
printArities tn = vcat . map ( \ (t, cl) ->
printInstance :: String -> TName -> (IsaClass, [(Typ, Sort)]) -> Doc
printInstance _ t xs = case xs of
(IsaClass "Monad", _) -> empty
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") <+> text "==" <+> text ("return_" ++ t))
$+$ text (t ++ "_bind_def:") <+> doubleQuotes
(text (t ++ "_bind") <+> text "==" <+> 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 "lemma" <+> text (w ++ "_lunit:")
<+> doubleQuotes (text (w ++ "_bind")
<+> parens (text (w ++ "_eta x"))
<+> parens (text $ "t::'a => 'b " ++ w)
<+> text "=" <+> text "t x")
runitLemma w = text "lemma" <+> text (w ++ "_runit:")
<+> doubleQuotes (text (w ++ "_bind")
<+> parens (text $ "t::'a " ++ w) <+> text (w ++ "_eta")
<+> text "=" <+> text "t")
assocLemma w = text "lemma" <+> text (w ++ "_assoc:")
<+> doubleQuotes ((text $ w ++ "_bind")
<+> parens ((text $ w ++ "_bind")
<+> parens (text $ "s::'a " ++ w) <+> text "t") <+> text "u"
<+> text "=" <+> text (w ++ "_bind s")
<+> parens ((text "%x.") <+>
(text $ w ++ "_bind") <+> text "(t x) u"))
etaInjLemma w = text "lemma" <+> text (w ++ "_eta_inj:")
<+> doubleQuotes (parens (text $ w ++ "_eta::'a => 'a " ++ w)
<+> text "=" <+> (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 tArrow = text ("-" ++ "->")
in (text "thymorph" <+> nm <+> colon <+>
text xn <+> tArrow <+> text tn)
$+$ text " maps" <+> (brackets $
hcat [parens $ (doubleQuotes (text b <+> text a) <+>
text "|->" <+> doubleQuotes (text b <+> (text $ tn ++ "." ++ t))) |
$+$ brackMapList (\j -> tn ++ "." ++ j) ws
brackMapList :: (String -> String) -> [(String,String)] -> Doc
brackMapList f ws = (brackets $
hsep $ punctuate comma [parens $ (doubleQuotes (text a)
<+> text "|->" <+> doubleQuotes (text $ f b)) | (a,b) <- ws])
printNInstance :: TName -> (IsaClass, [(Typ, Sort)]) -> Doc
printNInstance t (IsaClass x, xs) = let
"tr" -> printNInst "lift" [holType]
"dInt" -> printNInst "lift" [holType]
$+$ text (if x == "Eq" then "sorry"
printNInst :: TName -> [Sort] -> Doc
printNInst t xs = text instanceS <+> text t <>
doubleColon <> (case xs of
_ -> parens $ hsep $ punctuate comma $
map (doubleQuotes . printSort) xs)
-- filter out types that are given in the domain table
printTypeDecls :: DomainTab -> Arities -> Doc
printTycon :: (TName, [(IsaClass, [(Typ, Sort)])]) -> Doc
let arity = if null arity' then
else length (snd $ head arity') in
if t == "tr" || t == "dInt" || t == "bool" || t == "int"
then parens $ hsep $ punctuate comma
$ map (text . ("'a"++) . show) [
1..arity]
-- | 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
printSign sig = let dt = domainTab sig
printAbbrs (abbrs $ tsig sig) $++$
printTypeDecls dt ars $++$
printClassrel (classrel $ tsig sig) $++$
(if showLemmas sig then showCaseLemmata (domainTab sig) else empty) $++$
printArities (theoryName sig) ars
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 <+>
hsep (bar $ map printDOp ops)
printDOp (vn, args) = let opname = new vn in
text opname <+> hsep (map (printDOpArg opname)
$ zip args [1 :: Int .. ])
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:cons)) =
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"
"lemma" ++ 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