--- a/ProgTutorial/Recipes/Antiquotes.thy Fri Jun 03 15:15:17 2016 +0100
+++ b/ProgTutorial/Recipes/Antiquotes.thy Tue May 14 11:10:53 2019 +0200
@@ -39,21 +39,24 @@
document antiquotation is as follows:
*}
+ML \<open>Input.pos_of\<close>
+ML%linenosgray{*fun ml_enclose bg en source =
+ ML_Lex.read bg @ ML_Lex.read_source false source @ ML_Lex.read en;*}
-ML%linenosgray{*fun ml_val code_txt = "val _ = " ^ code_txt
+ML%linenosgray{*fun ml_val code_txt = (ml_enclose "val _ = " "" code_txt)
-fun output_ml {context = ctxt, ...} code_txt =
+fun output_ml ctxt code_txt =
let
- val srcpos = {delimited = false, text = (ml_val code_txt), pos = Position.none}
-in
- (ML_Context.eval_source_in (SOME ctxt) ML_Compiler.flags srcpos;
- Thy_Output.output ctxt (map Pretty.str (space_explode "\n" code_txt)))
+ val _ = ML_Context.eval_in (SOME ctxt) ML_Compiler.flags (Input.pos_of code_txt) (ml_val code_txt)
+in
+ Pretty.str (Input.source_content code_txt)
end
-val ml_checked_setup = Thy_Output.antiquotation @{binding "ML_checked"} (Scan.lift Args.name) output_ml*}
+val ml_checked_setup = Thy_Output.antiquotation_pretty_source @{binding "ML_checked"} (Scan.lift Args.text_input) output_ml*}
setup {* ml_checked_setup *}
+
text {*
The parser @{ML "(Scan.lift Args.name)"} in Line 7 parses a string, in this
case the code, and then calls the function @{ML output_ml}. As mentioned
@@ -61,13 +64,13 @@
function @{ML ml_val}, which constructs the appropriate ML-expression, and
using @{ML "eval_in" in ML_Context}, which calls the compiler. If the code is
``approved'' by the compiler, then the output function @{ML "output" in
- Thy_Output} in the next line pretty prints the code. This function expects
+ Document_Antiquotation} in the next line pretty prints the code. This function expects
that the code is a list of (pretty)strings where each string correspond to a
line in the output. Therefore the use of @{ML "(space_explode \"\\n\" txt)"
for txt} which produces such a list according to linebreaks. There are a
number of options for antiquotations that are observed by the function
- @{ML "output" in Thy_Output} when printing the code (including @{text "[display]"}
- and @{text "[quotes]"}). The function @{ML "antiquotation" in Thy_Output} in
+ @{ML "output" in Document_Antiquotation} when printing the code (including @{text "[display]"}
+ and @{text "[quotes]"}). The function @{ML "antiquotation_raw" in Thy_Output} in
Line 7 sets up the new document antiquotation.
\begin{readmore}
@@ -78,27 +81,32 @@
information about the line number, in case an error is detected. We
can improve the code above slightly by writing
*}
-
-ML%linenosgray{*fun output_ml {context = ctxt, ...} (code_txt, pos) =
+(* FIXME: remove
+ML%linenosgray{*fun output_ml ctxt (code_txt, pos) =
let
val srcpos = {delimited = false, pos = pos, text = ml_val code_txt}
in
(ML_Context.eval_source_in (SOME ctxt) ML_Compiler.flags srcpos;
- Thy_Output.output ctxt (map Pretty.str (space_explode "\n" code_txt)))
+ code_txt
+ |> space_explode "\n"
+ |> map Pretty.str
+ |> Pretty.list "" ""
+ |> Document_Antiquotation.output ctxt
+ |> Latex.string)
end
val ml_checked_setup2 = Thy_Output.antiquotation @{binding "ML_checked2"}
(Scan.lift (Parse.position Args.name)) output_ml *}
setup {* ml_checked_setup2 *}
-
+*)
text {*
where in Lines 1 and 2 the positional information is properly treated. The
parser @{ML Parse.position} encodes the positional information in the
result.
We can now write @{text "@{ML_checked2 \"2 + 3\"}"} in a document in order to
- obtain @{ML_checked2 "2 + 3"} and be sure that this code compiles until
+ obtain @{ML_checked "2 + 3"} and be sure that this code compiles until
somebody changes the definition of addition.
@@ -121,13 +129,17 @@
function will do this:
*}
-ML %grayML{*fun ml_pat (code_txt, pat) =
+ML%linenosgray{*fun ml_pat pat code =
+ ML_Lex.read "val" @ ML_Lex.read_source false pat @ ML_Lex.read " = " @ ML_Lex.read_source false code*}
+
+(*
+ML %grayML{*fun ml_pat code_txt pat =
let val pat' =
implode (map (fn "\<dots>" => "_" | s => s) (Symbol.explode pat))
in
- "val " ^ pat' ^ " = " ^ code_txt
+ ml_enclose ("val " ^ pat' ^ " = ") "" code_txt
end*}
-
+*)
text {*
Next we add a response indicator to the result using:
*}
@@ -139,6 +151,21 @@
The rest of the code of @{text "ML_resp"} is:
*}
+ML %linenosgray\<open>
+fun output_ml_resp ctxt (code_txt, pat) =
+let
+ val _ = ML_Context.eval_in (SOME ctxt) ML_Compiler.flags (Input.pos_of code_txt) (ml_pat pat code_txt)
+ val code = space_explode "\n" (Input.source_content code_txt)
+ val resp = add_resp (space_explode "\n" (Input.source_content pat))
+in
+ Pretty.str (cat_lines (code @ resp))
+end
+
+val ml_response_setup = Thy_Output.antiquotation_pretty_source @{binding "ML_resp"} (Scan.lift (Args.text_input -- Args.text_input)) output_ml_resp
+
+\<close>
+
+(*
ML %linenosgray{*fun output_ml_resp {context = ctxt, ...} ((code_txt, pat), pos) =
(let
val srcpos = {delimited = false, text = ml_pat (code_txt, pat), pos = pos}
@@ -154,13 +181,14 @@
val ml_resp_setup = Thy_Output.antiquotation @{binding "ML_resp"}
- (Scan.lift (Parse.position (Args.name -- Args.name)))
+ (Scan.lift (Parse.position (Args.text_input -- Args.text_input)))
output_ml_resp*}
+*)
+setup {* ml_response_setup *}
-setup {* ml_resp_setup *}
-
-text {*
- In comparison with @{text "ML_checked2"}, we only changed the line about
+(* FIXME *)
+text {*
+ In comparison with @{text "ML_checked"}, we only changed the line about
the compiler (Line~2), the lines about
the output (Lines 4 to 7) and the parser in the setup (Line 11). Now
you can write
@@ -177,7 +205,7 @@
to obtain
- @{ML_resp [display] "let val i = 3 in (i * i, \"foo\") end" "(9, \<dots>)"}
+ @{ML_resp [display] "let val i = 3 in (i * i, \"foo\") end" "(9, _)"}
In both cases, the check by the compiler ensures that code and result
match. A limitation of this document antiquotation, however, is that the