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
|
1 |
(* Auxiliary antiquotations for the tutorial. *)
|
45
78aeca00bb54
deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
2 |
|
256
|
3 |
structure AntiquoteSetup =
|
45
78aeca00bb54
deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
4 |
struct
|
78aeca00bb54
deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
5 |
|
256
|
6 |
open OutputTutorial
|
|
7 |
|
112
|
8 |
(* functions for generating appropriate expressions *)
|
258
|
9 |
fun ml_val_open ys istruc txt =
|
|
10 |
let
|
|
11 |
fun ml_val_open_aux ys txt =
|
45
78aeca00bb54
deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
12 |
"fn " ^ (case ys of [] => "_" | _ => enclose "(" ")" (commas ys)) ^ " => (" ^ txt ^ ")";
|
258
|
13 |
in
|
|
14 |
(case istruc of
|
|
15 |
NONE => ml_val_open_aux ys txt
|
|
16 |
| SOME st => ml_val_open_aux ys ("let open " ^ st ^ " in " ^ txt ^ " end"))
|
|
17 |
end
|
45
78aeca00bb54
deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
18 |
|
258
|
19 |
fun ml_val txt = ml_val_open [] NONE txt;
|
45
78aeca00bb54
deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
20 |
|
112
|
21 |
fun ml_pat (lhs, pat) =
|
51
|
22 |
let
|
261
|
23 |
val pat' = implode (map (fn "\<dots>" => "_" | s => s) (Symbol.explode pat))
|
112
|
24 |
in "val " ^ pat' ^ " = " ^ lhs end;
|
45
78aeca00bb54
deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
25 |
|
78aeca00bb54
deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
26 |
fun ml_struct txt = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end";
|
78aeca00bb54
deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
27 |
fun ml_type txt = "val _ = NONE : (" ^ txt ^ ") option";
|
78aeca00bb54
deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
28 |
|
112
|
29 |
(* eval function *)
|
171
|
30 |
fun eval_fn ctxt exp =
|
|
31 |
ML_Context.eval_in (SOME ctxt) false Position.none exp
|
57
|
32 |
|
112
|
33 |
(* string functions *)
|
256
|
34 |
fun string_explode prefix_str txt =
|
|
35 |
map (fn s => prefix_str ^ s) (split_lines txt)
|
45
78aeca00bb54
deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
36 |
|
112
|
37 |
val transform_cmts_str =
|
|
38 |
Source.of_string
|
|
39 |
#> ML_Lex.source
|
|
40 |
#> Source.exhaust
|
|
41 |
#> Chunks.transform_cmts
|
|
42 |
#> implode
|
256
|
43 |
#> string_explode ""
|
112
|
44 |
|
258
|
45 |
(* checks and prints open expressions, calculates index entry *)
|
|
46 |
fun output_ml {context = ctxt, ...} (txt, (ovars, istruc)) =
|
|
47 |
(eval_fn ctxt (ml_val_open ovars istruc txt);
|
|
48 |
case (istruc, Long_Name.base_name txt, Long_Name.qualifier txt) of
|
|
49 |
(NONE, bn, "") => output_indexed (transform_cmts_str txt) {main = Code txt, minor = NoString}
|
|
50 |
| (NONE, bn, qn) => output_indexed (transform_cmts_str txt) {main = Code bn, minor = IStruc qn}
|
|
51 |
| (SOME st, _, _) => output_indexed (transform_cmts_str txt) {main = Code txt, minor = IStruc st})
|
112
|
52 |
|
171
|
53 |
val parser_ml = Scan.lift (Args.name --
|
|
54 |
(Scan.optional (Args.$$$ "for" |-- OuterParse.!!! (Scan.repeat1 Args.name)) [] --
|
258
|
55 |
Scan.option (Args.$$$ "in" |-- OuterParse.!!! Args.name)))
|
45
78aeca00bb54
deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
56 |
|
112
|
57 |
(* checks and prints types and structures *)
|
256
|
58 |
fun output_struct {context = ctxt, ...} txt =
|
|
59 |
(eval_fn ctxt (ml_struct txt);
|
258
|
60 |
output_indexed (string_explode "" txt) {main = Code txt, minor = Plain "structure"})
|
256
|
61 |
|
|
62 |
fun output_type {context = ctxt, ...} txt =
|
|
63 |
(eval_fn ctxt (ml_type txt);
|
258
|
64 |
output_indexed (string_explode "" txt) {main = Code txt, minor = Plain "type"})
|
45
78aeca00bb54
deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
65 |
|
112
|
66 |
(* checks and expression agains a result pattern *)
|
171
|
67 |
fun output_response {context = ctxt, ...} (lhs, pat) =
|
|
68 |
(eval_fn ctxt (ml_pat (lhs, pat));
|
256
|
69 |
output ((string_explode "" lhs) @ (string_explode "> " pat)))
|
171
|
70 |
|
|
71 |
(* checks the expressions, but does not check it against a result pattern *)
|
|
72 |
fun output_response_fake {context = ctxt, ...} (lhs, pat) =
|
|
73 |
(eval_fn ctxt (ml_val lhs);
|
256
|
74 |
output ((string_explode "" lhs) @ (string_explode "> " pat)))
|
112
|
75 |
|
|
76 |
(* checks the expressions, but does not check it against a result pattern *)
|
171
|
77 |
fun ouput_response_fake_both _ (lhs, pat) =
|
256
|
78 |
output ((string_explode "" lhs) @ (string_explode "> " pat))
|
171
|
79 |
|
|
80 |
val single_arg = Scan.lift (Args.name)
|
|
81 |
val two_args = Scan.lift (Args.name -- Args.name)
|
112
|
82 |
|
171
|
83 |
val _ = ThyOutput.antiquotation "ML" parser_ml output_ml
|
256
|
84 |
val _ = ThyOutput.antiquotation "ML_type" single_arg output_type
|
|
85 |
val _ = ThyOutput.antiquotation "ML_struct" single_arg output_struct
|
171
|
86 |
val _ = ThyOutput.antiquotation "ML_response" two_args output_response
|
|
87 |
val _ = ThyOutput.antiquotation "ML_response_fake" two_args output_response_fake
|
|
88 |
val _ = ThyOutput.antiquotation "ML_response_fake_both" two_args ouput_response_fake_both
|
54
|
89 |
|
182
|
90 |
fun href_link txt =
|
|
91 |
let
|
|
92 |
val raw = Symbol.encode_raw
|
|
93 |
val path = "http://isabelle.in.tum.de/repos/isabelle/raw-file/tip/src/"
|
|
94 |
in
|
|
95 |
(raw "\\href{") ^ (raw path) ^ (raw txt) ^ (raw "}{") ^ txt ^ (raw "}")
|
|
96 |
end
|
|
97 |
|
112
|
98 |
(* checks whether a file exists in the Isabelle distribution *)
|
171
|
99 |
fun check_file_exists _ txt =
|
|
100 |
(if File.exists (Path.append (Path.explode ("~~/src")) (Path.explode txt))
|
256
|
101 |
then output [href_link txt]
|
171
|
102 |
else error ("Source file " ^ (quote txt) ^ " does not exist."))
|
|
103 |
|
|
104 |
val _ = ThyOutput.antiquotation "ML_file" single_arg check_file_exists
|
|
105 |
|
45
78aeca00bb54
deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
106 |
|
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
107 |
(* replaces the official subgoal antiquotation with one *)
|
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
108 |
(* that is closer to the actual output *)
|
171
|
109 |
fun output_goals {state = node, ...} _ =
|
|
110 |
let
|
98
0a5c95f4d70c
calculated the exact number of goals in the subgoal antiquotation
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
111 |
fun subgoals 0 = ""
|
96
018bfa718982
(re)defined the document antiquotation @subgoal in order to be closer to what is actually printed
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
112 |
| subgoals 1 = "goal (1 subgoal):"
|
171
|
113 |
| subgoals n = "goal (" ^ string_of_int n ^ " subgoals):"
|
|
114 |
|
|
115 |
fun proof_state state =
|
|
116 |
(case try Toplevel.proof_of state of
|
|
117 |
SOME prf => prf
|
|
118 |
| _ => error "No proof state")
|
96
018bfa718982
(re)defined the document antiquotation @subgoal in order to be closer to what is actually printed
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
119 |
|
98
0a5c95f4d70c
calculated the exact number of goals in the subgoal antiquotation
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
120 |
val state = proof_state node;
|
209
|
121 |
val goals = Pretty.chunks (Proof.pretty_goals false state);
|
98
0a5c95f4d70c
calculated the exact number of goals in the subgoal antiquotation
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
122 |
|
0a5c95f4d70c
calculated the exact number of goals in the subgoal antiquotation
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
123 |
val {prop, ...} = rep_thm (Proof.get_goal state |> snd |> snd);
|
0a5c95f4d70c
calculated the exact number of goals in the subgoal antiquotation
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
124 |
val (As, B) = Logic.strip_horn prop;
|
171
|
125 |
val output = (case (length As) of
|
209
|
126 |
0 => [goals]
|
|
127 |
| n => [Pretty.str (subgoals n), goals])
|
96
018bfa718982
(re)defined the document antiquotation @subgoal in order to be closer to what is actually printed
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
128 |
in
|
171
|
129 |
ThyOutput.output output
|
96
018bfa718982
(re)defined the document antiquotation @subgoal in order to be closer to what is actually printed
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
130 |
end
|
165
|
131 |
|
171
|
132 |
val _ = ThyOutput.antiquotation "subgoals" (Scan.succeed ()) output_goals
|
209
|
133 |
|
45
78aeca00bb54
deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
134 |
end;
|