23
|
1 |
theory Base
|
264
|
2 |
imports Main LaTeXsugar
|
23
|
3 |
uses
|
346
|
4 |
("output_tutorial.ML")
|
|
5 |
("antiquote_setup.ML")
|
23
|
6 |
begin
|
|
7 |
|
394
|
8 |
notation (latex output)
|
|
9 |
Cons ("_ # _" [66,65] 65)
|
|
10 |
|
317
d69214e47ef9
added an experimental antiquotation to replace eventually ML_response_fake
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
11 |
(* rebinding of writeln and tracing so that it can *)
|
d69214e47ef9
added an experimental antiquotation to replace eventually ML_response_fake
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
12 |
(* be compared in intiquotations *)
|
d69214e47ef9
added an experimental antiquotation to replace eventually ML_response_fake
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
13 |
ML {*
|
d69214e47ef9
added an experimental antiquotation to replace eventually ML_response_fake
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
14 |
fun writeln s = (Output.writeln s; s)
|
d69214e47ef9
added an experimental antiquotation to replace eventually ML_response_fake
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
15 |
fun tracing s = (Output.tracing s; s)
|
d69214e47ef9
added an experimental antiquotation to replace eventually ML_response_fake
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
16 |
*}
|
d69214e47ef9
added an experimental antiquotation to replace eventually ML_response_fake
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
17 |
|
346
|
18 |
(* re-definition of various ML antiquotations *)
|
|
19 |
(* to have a special tag for text enclosed in ML; *)
|
|
20 |
(* they also write the code into a separate file *)
|
301
|
21 |
|
64
9a6e5e0c4906
deleted old files and added code to give a special tag to the command ML
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
22 |
ML {*
|
346
|
23 |
val (filename, setup_filename) = Attrib.config_string "filename" "File_Code.ML"
|
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
24 |
*}
|
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
25 |
|
346
|
26 |
setup {* setup_filename *}
|
|
27 |
|
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
28 |
ML {*
|
346
|
29 |
fun write_file txt thy =
|
|
30 |
let
|
|
31 |
val stream = Config.get_thy thy filename
|
|
32 |
|> TextIO.openAppend
|
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
33 |
in
|
346
|
34 |
TextIO.output (stream, txt);
|
|
35 |
TextIO.flushOut stream; (* needed ?*)
|
|
36 |
TextIO.closeOut stream
|
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
37 |
end
|
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
38 |
*}
|
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
39 |
|
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
40 |
ML {*
|
346
|
41 |
fun write_file_ml_blk txt thy =
|
|
42 |
let
|
|
43 |
val pre = implode ["\n", "ML ", "{", "*", "\n"]
|
|
44 |
val post = implode ["\n", "*", "}", "\n"]
|
|
45 |
val _ = write_file (enclose pre post txt) thy
|
|
46 |
in
|
|
47 |
thy
|
|
48 |
end
|
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
49 |
|
346
|
50 |
fun write_file_setup_blk txt thy =
|
|
51 |
let
|
|
52 |
val pre = implode ["\n", "setup ", "{", "*", "\n"]
|
|
53 |
val post = implode ["\n", "*", "}", "\n"]
|
|
54 |
val _ = write_file (enclose pre post txt) thy
|
|
55 |
in
|
|
56 |
thy
|
|
57 |
end
|
|
58 |
|
|
59 |
fun write_file_lsetup_blk txt lthy =
|
|
60 |
let
|
|
61 |
val pre = implode ["\n", "local_setup ", "{", "*", "\n"]
|
|
62 |
val post = implode ["\n", "*", "}", "\n"]
|
|
63 |
val _ = write_file (enclose pre post txt) (ProofContext.theory_of lthy)
|
|
64 |
in
|
|
65 |
lthy
|
|
66 |
end
|
|
67 |
|
310
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
68 |
*}
|
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
69 |
|
007922777ff1
added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
70 |
ML {*
|
346
|
71 |
fun open_file name thy =
|
|
72 |
let
|
|
73 |
val _ = tracing ("Open File: " ^ name)
|
|
74 |
val _ = TextIO.openOut name
|
|
75 |
in
|
|
76 |
Config.put_thy filename name thy
|
|
77 |
end
|
|
78 |
*}
|
64
9a6e5e0c4906
deleted old files and added code to give a special tag to the command ML
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
79 |
|
346
|
80 |
ML {*
|
|
81 |
fun open_file_with_prelude name txts thy =
|
|
82 |
let
|
|
83 |
val thy' = open_file name thy
|
|
84 |
val _ = write_file (cat_lines txts) thy'
|
|
85 |
in
|
|
86 |
thy'
|
|
87 |
end
|
|
88 |
*}
|
|
89 |
|
|
90 |
ML {*
|
260
|
91 |
fun propagate_env (context as Context.Proof lthy) =
|
394
|
92 |
Context.Proof (Local_Theory.map_contexts (ML_Env.inherit context) lthy)
|
264
|
93 |
| propagate_env context = context
|
210
|
94 |
|
260
|
95 |
fun propagate_env_prf prf = Proof.map_contexts
|
264
|
96 |
(Context.proof_map (ML_Env.inherit (Context.Proof (Proof.context_of prf)))) prf
|
346
|
97 |
*}
|
210
|
98 |
|
346
|
99 |
ML {*
|
64
9a6e5e0c4906
deleted old files and added code to give a special tag to the command ML
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
100 |
val _ =
|
9a6e5e0c4906
deleted old files and added code to give a special tag to the command ML
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
101 |
OuterSyntax.command "ML" "eval ML text within theory"
|
106
bdd82350cf22
renamed in the pdf all instances of cookbook to tutorial (in order to sound more serious)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
102 |
(OuterKeyword.tag "TutorialML" OuterKeyword.thy_decl)
|
346
|
103 |
(OuterParse.position OuterParse.text >>
|
|
104 |
(fn (txt, pos) =>
|
|
105 |
Toplevel.generic_theory
|
|
106 |
(ML_Context.exec
|
|
107 |
(fn () => ML_Context.eval true pos txt)
|
|
108 |
#> propagate_env #> Context.map_theory (write_file_ml_blk txt))))
|
210
|
109 |
|
|
110 |
val _ =
|
|
111 |
OuterSyntax.command "ML_prf" "ML text within proof"
|
|
112 |
(OuterKeyword.tag "TutorialML" OuterKeyword.prf_decl)
|
|
113 |
(OuterParse.ML_source >> (fn (txt, pos) =>
|
|
114 |
Toplevel.proof (Proof.map_context (Context.proof_map
|
260
|
115 |
(ML_Context.exec (fn () => ML_Context.eval true pos txt))) #> propagate_env_prf)))
|
210
|
116 |
|
224
|
117 |
val _ =
|
|
118 |
OuterSyntax.command "ML_val" "diagnostic ML text"
|
|
119 |
(OuterKeyword.tag "TutorialML" OuterKeyword.diag)
|
264
|
120 |
(OuterParse.ML_source >> IsarCmd.ml_diag true)
|
346
|
121 |
*}
|
224
|
122 |
|
346
|
123 |
ML {*
|
|
124 |
val _ =
|
|
125 |
OuterSyntax.command "setup" "ML theory setup"
|
|
126 |
(OuterKeyword.tag_ml OuterKeyword.thy_decl)
|
|
127 |
(OuterParse.ML_source >> (fn (txt, pos) =>
|
|
128 |
(Toplevel.theory (IsarCmd.global_setup (txt, pos) #> write_file_setup_blk txt))))
|
64
9a6e5e0c4906
deleted old files and added code to give a special tag to the command ML
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
129 |
*}
|
239
|
130 |
|
346
|
131 |
ML {* Context.proof_map *}
|
|
132 |
ML {* IsarCmd.local_setup ; Context.map_proof (write_file_lsetup_blk "") *}
|
|
133 |
|
|
134 |
|
|
135 |
ML {*
|
|
136 |
val _ =
|
|
137 |
OuterSyntax.local_theory "local_setup" "ML local theory setup"
|
|
138 |
(OuterKeyword.tag_ml OuterKeyword.thy_decl)
|
|
139 |
(OuterParse.ML_source >> (fn (txt, pos) =>
|
|
140 |
IsarCmd.local_setup (txt, pos) #> write_file_lsetup_blk txt));
|
|
141 |
*}
|
|
142 |
|
396
|
143 |
ML {*
|
|
144 |
fun P n = @{term "P::nat \<Rightarrow> bool"} $ (HOLogic.mk_number @{typ "nat"} n)
|
|
145 |
|
|
146 |
fun rhs 1 = P 1
|
|
147 |
| rhs n = HOLogic.mk_conj (P n, rhs (n - 1))
|
|
148 |
|
|
149 |
fun lhs 1 n = HOLogic.mk_imp (HOLogic.mk_eq (P 1, P n), rhs n)
|
|
150 |
| lhs m n = HOLogic.mk_conj (HOLogic.mk_imp
|
|
151 |
(HOLogic.mk_eq (P (m - 1), P m), rhs n), lhs (m - 1) n)
|
|
152 |
|
|
153 |
fun de_bruijn n =
|
|
154 |
HOLogic.mk_Trueprop (HOLogic.mk_imp (lhs n n, rhs n))
|
|
155 |
*}
|
346
|
156 |
|
|
157 |
use "output_tutorial.ML"
|
|
158 |
use "antiquote_setup.ML"
|
|
159 |
|
|
160 |
|
239
|
161 |
end |