author | wenzelm |
Thu, 23 Jan 2025 22:19:30 +0100 | |
changeset 81960 | 326ecfc079a6 |
parent 80815 | cd10c7c9f25c |
permissions | -rw-r--r-- |
62665 | 1 |
(* Title: Pure/ML/ml_pp.ML |
38327 | 2 |
Author: Makarius |
30633
cc18ae3c1c7f
extra toplevel pretty-printing for Poly/ML; experimental version for Poly/ML 5.3;
wenzelm
parents:
diff
changeset
|
3 |
|
62665 | 4 |
ML toplevel pretty-printing for logical entities. |
30633
cc18ae3c1c7f
extra toplevel pretty-printing for Poly/ML; experimental version for Poly/ML 5.3;
wenzelm
parents:
diff
changeset
|
5 |
*) |
cc18ae3c1c7f
extra toplevel pretty-printing for Poly/ML; experimental version for Poly/ML 5.3;
wenzelm
parents:
diff
changeset
|
6 |
|
80812 | 7 |
signature ML_PP = |
8 |
sig |
|
9 |
val toplevel_context: unit -> Proof.context |
|
10 |
val pp_context: Proof.context -> Pretty.T |
|
11 |
val pp_typ: Proof.context -> typ -> Pretty.T |
|
12 |
val pp_term: Proof.context -> term -> Pretty.T |
|
13 |
val pp_thm: Proof.context -> thm -> Pretty.T |
|
14 |
end; |
|
15 |
||
80815 | 16 |
structure ML_PP: ML_PP = |
62665 | 17 |
struct |
62356 | 18 |
|
80812 | 19 |
(* logical context *) |
80803
7e39c785ca5d
clarified modules: enable pretty.ML to use XML/YXML more directly;
wenzelm
parents:
79127
diff
changeset
|
20 |
|
80812 | 21 |
(*ML compiler toplevel context: fallback on theory Pure for regular runtime*) |
22 |
fun toplevel_context () = |
|
23 |
let |
|
24 |
fun global_context () = |
|
25 |
Theory.get_pure () |
|
26 |
|> Config.put_global Name_Space.names_long true |
|
27 |
|> Syntax.init_pretty_global; |
|
28 |
in |
|
29 |
(case Context.get_generic_context () of |
|
30 |
SOME (Context.Proof ctxt) => ctxt |
|
31 |
| SOME (Context.Theory thy) => |
|
32 |
(case try Syntax.init_pretty_global thy of |
|
33 |
SOME ctxt => ctxt |
|
34 |
| NONE => global_context ()) |
|
35 |
| NONE => global_context ()) |
|
36 |
end; |
|
80803
7e39c785ca5d
clarified modules: enable pretty.ML to use XML/YXML more directly;
wenzelm
parents:
79127
diff
changeset
|
37 |
|
80812 | 38 |
fun pp_context ctxt = |
39 |
(if Config.get ctxt Proof_Context.debug then |
|
40 |
Pretty.quote (Pretty.big_list "proof context:" (Proof_Context.pretty_context ctxt)) |
|
41 |
else Pretty.str "<context>"); |
|
42 |
||
43 |
||
44 |
(* logical entities (with syntax) *) |
|
45 |
||
46 |
fun pp_typ ctxt T = Pretty.quote (Syntax.pretty_typ ctxt T); |
|
47 |
fun pp_term ctxt t = Pretty.quote (Syntax.pretty_term ctxt t); |
|
48 |
fun pp_thm ctxt th = Thm.pretty_thm_raw ctxt {quote = true, show_hyps = false} th; |
|
78649 | 49 |
|
50 |
val _ = |
|
80812 | 51 |
ML_system_pp (fn _ => fn _ => Pretty.to_ML o pp_context); |
62665 | 52 |
|
53 |
val _ = |
|
80812 | 54 |
ML_system_pp (fn depth => fn _ => fn th => |
55 |
ML_Pretty.prune depth (Pretty.to_ML (pp_thm (toplevel_context ()) th))); |
|
62356 | 56 |
|
62665 | 57 |
val _ = |
80812 | 58 |
ML_system_pp (fn depth => fn _ => fn ct => |
59 |
ML_Pretty.prune depth (Pretty.to_ML (pp_term (toplevel_context ()) (Thm.term_of ct)))); |
|
33767 | 60 |
|
62665 | 61 |
val _ = |
80812 | 62 |
ML_system_pp (fn depth => fn _ => fn cT => |
63 |
ML_Pretty.prune depth (Pretty.to_ML (pp_typ (toplevel_context ()) (Thm.typ_of cT)))); |
|
30633
cc18ae3c1c7f
extra toplevel pretty-printing for Poly/ML; experimental version for Poly/ML 5.3;
wenzelm
parents:
diff
changeset
|
64 |
|
79127 | 65 |
val _ = |
80812 | 66 |
ML_system_pp (fn depth => fn _ => fn T => |
67 |
ML_Pretty.prune depth (Pretty.to_ML (pp_typ (toplevel_context ()) T))); |
|
80809
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
68 |
|
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
69 |
val _ = |
80812 | 70 |
ML_system_pp (fn depth => fn _ => fn T => |
71 |
ML_Pretty.prune depth (Pretty.to_ML (pp_typ (toplevel_context ()) (ZTerm.typ_of T)))); |
|
79127 | 72 |
|
52426
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
73 |
|
80812 | 74 |
(* ML term and proofterm *) |
75 |
||
52426
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
76 |
local |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
77 |
|
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
78 |
fun prt_app name prt = Pretty.block [Pretty.str (name ^ " "), prt]; |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
79 |
fun prt_apps name = Pretty.enum "," (name ^ " (") ")"; |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
80 |
|
62387
ad3eb2889f9a
support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents:
62356
diff
changeset
|
81 |
fun prt_term parens (dp: FixedInt.int) t = |
80813
9dd4dcb08d37
clarified signature, following 1f718be3608b: Pretty.str is now value-oriented;
wenzelm
parents:
80812
diff
changeset
|
82 |
if dp <= 0 then Pretty.dots |
52426
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
83 |
else |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
84 |
(case t of |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
85 |
_ $ _ => |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
86 |
op :: (strip_comb t) |
62387
ad3eb2889f9a
support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents:
62356
diff
changeset
|
87 |
|> map_index (fn (i, u) => prt_term true (dp - FixedInt.fromInt i - 1) u) |
52426
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
88 |
|> Pretty.separate " $" |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
89 |
|> (if parens then Pretty.enclose "(" ")" else Pretty.block) |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
90 |
| Abs (a, T, b) => |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
91 |
prt_apps "Abs" |
80809
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
92 |
[Pretty.from_ML (ML_system_pretty (a, dp - 1)), |
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
93 |
Pretty.from_ML (ML_system_pretty (T, dp - 2)), |
52426
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
94 |
prt_term false (dp - 3) b] |
80809
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
95 |
| Const a => prt_app "Const" (Pretty.from_ML (ML_system_pretty (a, dp - 1))) |
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
96 |
| Free a => prt_app "Free" (Pretty.from_ML (ML_system_pretty (a, dp - 1))) |
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
97 |
| Var a => prt_app "Var" (Pretty.from_ML (ML_system_pretty (a, dp - 1))) |
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
98 |
| Bound a => prt_app "Bound" (Pretty.from_ML (ML_system_pretty (a, dp - 1)))); |
52426
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
99 |
|
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
100 |
fun prt_proof parens dp prf = |
80813
9dd4dcb08d37
clarified signature, following 1f718be3608b: Pretty.str is now value-oriented;
wenzelm
parents:
80812
diff
changeset
|
101 |
if dp <= 0 then Pretty.dots |
52426
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
102 |
else |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
103 |
(case prf of |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
104 |
_ % _ => prt_proofs parens dp prf |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
105 |
| _ %% _ => prt_proofs parens dp prf |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
106 |
| Abst (a, T, b) => |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
107 |
prt_apps "Abst" |
80809
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
108 |
[Pretty.from_ML (ML_system_pretty (a, dp - 1)), |
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
109 |
Pretty.from_ML (ML_system_pretty (T, dp - 2)), |
52426
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
110 |
prt_proof false (dp - 3) b] |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
111 |
| AbsP (a, t, b) => |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
112 |
prt_apps "AbsP" |
80809
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
113 |
[Pretty.from_ML (ML_system_pretty (a, dp - 1)), |
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
114 |
Pretty.from_ML (ML_system_pretty (t, dp - 2)), |
52426
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
115 |
prt_proof false (dp - 3) b] |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
116 |
| Hyp t => prt_app "Hyp" (prt_term true (dp - 1) t) |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
117 |
| MinProof => Pretty.str "MinProof" |
80809
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
118 |
| PBound a => prt_app "PBound" (Pretty.from_ML (ML_system_pretty (a, dp - 1))) |
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
119 |
| PAxm a => prt_app "PAxm" (Pretty.from_ML (ML_system_pretty (a, dp - 1))) |
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
120 |
| PClass a => prt_app "PClass" (Pretty.from_ML (ML_system_pretty (a, dp - 1))) |
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
121 |
| Oracle a => prt_app "Oracle" (Pretty.from_ML (ML_system_pretty (a, dp - 1))) |
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
122 |
| PThm a => prt_app "PThm" (Pretty.from_ML (ML_system_pretty (a, dp - 1)))) |
52426
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
123 |
|
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
124 |
and prt_proofs parens dp prf = |
33545
d8903f0002e5
home-grown pretty printer for term -- Poly/ML 5.3.0 does not observe infix status of constructors (notably $);
wenzelm
parents:
33538
diff
changeset
|
125 |
let |
52426
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
126 |
val (head, args) = strip_proof prf []; |
62387
ad3eb2889f9a
support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents:
62356
diff
changeset
|
127 |
val prts = |
ad3eb2889f9a
support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents:
62356
diff
changeset
|
128 |
head (dp - 1) :: flat (map_index (fn (i, prt) => prt (dp - FixedInt.fromInt i - 2)) args); |
52426
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
129 |
in if parens then Pretty.enclose "(" ")" prts else Pretty.block prts end |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
130 |
|
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
131 |
and strip_proof (p % t) res = |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
132 |
strip_proof p |
62665 | 133 |
((fn d => |
134 |
[Pretty.str " %", Pretty.brk 1, |
|
80809
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
80808
diff
changeset
|
135 |
Pretty.from_ML (ML_system_pretty (t, d))]) :: res) |
52426
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
136 |
| strip_proof (p %% q) res = |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
137 |
strip_proof p ((fn d => [Pretty.str " %%", Pretty.brk 1, prt_proof true d q]) :: res) |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
138 |
| strip_proof p res = (fn d => prt_proof true d p, res); |
33545
d8903f0002e5
home-grown pretty printer for term -- Poly/ML 5.3.0 does not observe infix status of constructors (notably $);
wenzelm
parents:
33538
diff
changeset
|
139 |
|
52426
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
140 |
in |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
141 |
|
80812 | 142 |
val _ = ML_system_pp (fn depth => fn _ => Pretty.to_ML o prt_term false depth); |
143 |
val _ = ML_system_pp (fn depth => fn _ => fn prf => Pretty.to_ML (prt_proof false depth prf)); |
|
52426
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
144 |
|
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
145 |
end; |
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
146 |
|
81e27230a8b7
ML pretty printer for proof terms, with specifica treatment of infix application (similar to terms);
wenzelm
parents:
52425
diff
changeset
|
147 |
end; |