author | Christian Urban <urbanc@in.tum.de> |
Wed, 29 Oct 2008 21:51:25 +0100 | |
changeset 46 | 81e2d73f7191 |
child 47 | 4daf913fdbe1 |
permissions | -rw-r--r-- |
46
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
1 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
2 |
theory Antiquotes |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
3 |
imports Main |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
4 |
begin |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
5 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
6 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
7 |
section {* Two Document Antiquotations *} |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
8 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
9 |
text {* |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
10 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
11 |
For writing documents using Isabelle it is often uesful to use |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
12 |
document antiquotations. Below we give the code for such an |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
13 |
antiquotation that typesets ML-code and also checks whether |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
14 |
the code is actually correct. In this way one can achieve that the |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
15 |
document is always in sync with code. |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
16 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
17 |
Below we introduce the antiquotation @{text "@{ML_checked \"\<dots>\"}"} which |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
18 |
takes a piece of code as argument. We will check this code by sending |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
19 |
the ML-expression @{text "val _ = \<dots>"} to the ML-compiler (i.e.~the |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
20 |
function @{ML "ML_Context.eval_in"}). The code for this antiquotation |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
21 |
is as follows: |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
22 |
*} |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
23 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
24 |
ML {* |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
25 |
fun ml_val txt = "val _ = " ^ txt |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
26 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
27 |
fun output_ml ml src ctxt txt = |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
28 |
(ML_Context.eval_in (SOME ctxt) false Position.none (ml txt); |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
29 |
ThyOutput.output_list (fn _ => fn s => Pretty.str s) src ctxt (space_explode "\n" txt)) |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
30 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
31 |
val _ = ThyOutput.add_commands |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
32 |
[("ML_checked", ThyOutput.args (Scan.lift Args.name) (output_ml ml_val))] |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
33 |
*} |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
34 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
35 |
text {* |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
36 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
37 |
Note that the parser @{ML "(Scan.lift Args.name)"} parses a string. If the |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
38 |
code is approved by the compiler, the output function |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
39 |
@{ML "ThyOutput.output_list (fn _ => fn s => Pretty.str s)"} |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
40 |
pretty prints the code. There are a number of options that are observed |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
41 |
when printing the code (for example @{text "[display]"}; for more information |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
42 |
about the options see \rsccite{sec:antiq}). |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
43 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
44 |
Since we |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
45 |
used the argument @{ML "Position.none"}, the compiler cannot give specific |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
46 |
information about the line number where an error might have occurred. We |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
47 |
can improve this code slightly by writing |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
48 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
49 |
*} |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
50 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
51 |
ML {* |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
52 |
fun output_ml ml src ctxt (txt,pos) = |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
53 |
(ML_Context.eval_in (SOME ctxt) false pos (ml txt); |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
54 |
ThyOutput.output_list (fn _ => fn s => Pretty.str s) src ctxt (space_explode "\n" txt)) |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
55 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
56 |
val _ = ThyOutput.add_commands |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
57 |
[("ML_checked", ThyOutput.args |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
58 |
(Scan.lift (OuterParse.position Args.name)) (output_ml ml_val))] |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
59 |
*} |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
60 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
61 |
text {* |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
62 |
where the antiquotation can also handle position information. |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
63 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
64 |
(FIXME: say something about OuterParse.position) |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
65 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
66 |
*} |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
67 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
68 |
ML {* |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
69 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
70 |
fun ml_pat (rhs, pat) = |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
71 |
let val pat' = implode (map (fn "\\<dots>" => "_" | s => s) (Symbol.explode pat)) |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
72 |
in |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
73 |
"val " ^ pat' ^ " = " ^ rhs |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
74 |
end; |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
75 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
76 |
fun add_response_indicator txt = |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
77 |
map (fn s => "> " ^ s) (space_explode "\n" txt) |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
78 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
79 |
fun output_ml_response ml src ctxt ((lhs,pat),pos) = |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
80 |
(ML_Context.eval_in (SOME ctxt) false pos (ml (lhs,pat)); |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
81 |
let val txt = (space_explode "\n" lhs) @ (add_response_indicator pat) |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
82 |
in ThyOutput.output_list (fn _ => fn s => Pretty.str s) src ctxt txt end) |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
83 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
84 |
*} |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
85 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
86 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
87 |
(* |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
88 |
val _ = ThyOutput.add_commands |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
89 |
[("ML_response", |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
90 |
ThyOutput.args (Scan.lift (OuterParse.position (Args.name -- Args.name))) |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
91 |
(output_ml_response ml_pat)] |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
92 |
*) |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
93 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
94 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
95 |
ML {* |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
96 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
97 |
let |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
98 |
val i = 1 + 2 |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
99 |
in |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
100 |
i * i |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
101 |
end |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
102 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
103 |
*} |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
104 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
105 |
(* |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
106 |
A test: |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
107 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
108 |
@{ML_response [display] "true andalso false" "false"} |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
109 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
110 |
@{ML_response [display] |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
111 |
"let |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
112 |
val i = 1 + 2 |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
113 |
in |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
114 |
i * i |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
115 |
end" "9"} |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
116 |
*) |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
117 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
118 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
119 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
120 |
end |
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
121 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
122 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
123 |
|
81e2d73f7191
added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
124 |