CookBook/Recipes/Antiquotes.thy
changeset 175 7c09bd3227c5
parent 171 18f90044c777
child 188 8939b8fd8603
--- a/CookBook/Recipes/Antiquotes.thy	Fri Mar 13 01:15:55 2009 +0100
+++ b/CookBook/Recipes/Antiquotes.thy	Fri Mar 13 12:21:44 2009 +0100
@@ -20,10 +20,10 @@
 
   @{ML [display,gray] "ThyOutput.print_antiquotations ()"}
 
-  Below we give the code for two additional antiquotations that can be used to typeset
-  ML-code and also to check whether the given code actually compiles. This
-  provides a sanity check for the code and also allows one to keep documents
-  in sync with other code, for example Isabelle.
+  Below we give the code for two additional document antiquotations that can
+  be used to typeset ML-code and also to check whether the given code actually
+  compiles. This provides a sanity check for the code and also allows one to
+  keep documents in sync with other code, for example Isabelle.
 
   We first describe the antiquotation @{text "ML_checked"} with the syntax:
  
@@ -32,7 +32,7 @@
   The code is checked by sending the ML-expression @{text [quotes] "val _ =
   a_piece_of_code"} to the ML-compiler (i.e.~the function @{ML
   "ML_Context.eval_in"} in Line 4 below). The complete code of the
-  antiquotation is as follows:
+  document antiquotation is as follows:
 
 *}
 
@@ -45,23 +45,22 @@
 val _ = ThyOutput.antiquotation "ML_checked" (Scan.lift Args.name) output_ml*}
 
 text {*
+  The parser @{ML "(Scan.lift Args.name)"} in line 9 parses a string, in this
+  case the code. As mentioned before, the code is sent to the ML-compiler in
+  the line 4 using the function @{ML ml_val}, which constructs the appropriate
+  ML-expression.  If the code is ``approved'' by the compiler, then the output
+  function @{ML "ThyOutput.output"} 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 this list according to
+  linebreaks.  There are a number of options for antiquotations that are
+  observed by @{ML ThyOutput.output} when printing the code (including @{text
+  "[display]"} and @{text "[quotes]"}). Line 7 sets up the new document
+  antiquotation.
 
-  The parser @{ML "(Scan.lift Args.name)"} in line 9 parses a string, 
-  in this case the code. As mentioned before, the code
-  is sent to the ML-compiler in the line 4 using the function @{ML ml_val},
-  which constructs the appropriate ML-expression.
-  If the code is ``approved'' by the compiler, then the output function @{ML
-  "ThyOutput.output"} 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 this list according to linebreaks.  There are a number of options 
-  for antiquotations that are observed by @{ML ThyOutput.output} when printing the 
-  code (including @{text "[display]"} and @{text "[quotes]"}). Line 7 sets 
-  up the new antiquotation.
 
   \begin{readmore}
-  For more information about options of antiquotations see \rsccite{sec:antiq}).
+  For more information about options of document antiquotations see \rsccite{sec:antiq}).
   \end{readmore}
 
   Since we used the argument @{ML "Position.none"}, the compiler cannot give specific 
@@ -86,24 +85,25 @@
   somebody changes the definition of \mbox{@{ML "(op +)"}}.
 
 
-  The second antiquotation we describe extends the first by a pattern that 
-  specifies what the result of the ML-code should be and check 
-  the consistency of the actual result with the given pattern. For this we are going 
-  to implement the antiquotation  
+  The second document antiquotation we describe extends the first by a pattern
+  that specifies what the result of the ML-code should be and check the
+  consistency of the actual result with the given pattern. For this we are
+  going to implement the document antiquotation
+
   
   @{text [display] "@{ML_resp \"a_piece_of_code\" \"a_pattern\"}"}
   
   To add some convenience and also to deal with large outputs, the user can
   give a partial specification inside the pattern by giving abbreviations of
-  the form @{text [quotes] "\<dots>"}. For example @{text "(\<dots>, \<dots>)"} to specify a
+  the form @{text [quotes] "\<dots>"}. For example @{text "(\<dots>, \<dots>)"} for specifying a
   pair.
 
-  Whereas in the antiquotation @{text "@{ML_checked \"piece_of_code\"}"}
-  above, we have sent the expression @{text [quotes] "val _ = piece_of_code"}
-  to the compiler, in the second the wildcard @{text "_"} we will be replaced
-  by the given pattern. To do this we need to replace the @{text [quotes] "\<dots>"}
-  by @{text [quotes] "_"} before sending the code to the compiler. The
-  following function will do this:
+  In the document antiquotation @{text "@{ML_checked \"piece_of_code\"}"}
+  above we have sent the expression @{text [quotes] "val _ = piece_of_code"}
+  to the compiler, now instead the wildcard @{text "_"} we will be replaced by
+  the given pattern. To do this we need to replace in the input the @{text
+  [quotes] "\<dots>"} by @{text [quotes] "_"} before sending the code to the
+  compiler. The following function will do this:
 *}
 
 ML{*fun ml_pat (code_txt, pat) =
@@ -118,19 +118,19 @@
 *}
 
 
-ML{*fun add_resp_indicator pat =
-  map (fn s => "> " ^ s) (space_explode "\n" pat) *}
+ML{*fun add_resp pat = map (fn s => "> " ^ s) pat*}
 
 text {* 
-  The rest of the code of the antiquotation is
+  The rest of the code of the document antiquotation is
 *}
 
 ML{*fun output_ml_resp {context = ctxt, ...} ((code_txt, pat), pos) = 
   (ML_Context.eval_in (SOME ctxt) false pos (ml_pat (code_txt, pat));
    let 
-     val output = (space_explode "\n" code_txt) @ (add_resp_indicator pat)
+     val output1 = space_explode "\n" code_txt 
+     val output2 = add_resp (space_explode "\n" pat)
    in 
-     ThyOutput.output (map Pretty.str output) 
+     ThyOutput.output (map Pretty.str (output1 @ output2)) 
    end)
 
 val _ = ThyOutput.antiquotation "ML_resp" 
@@ -138,7 +138,7 @@
            output_ml_resp*}
 
 text {*
-  This extended antiquotation allows us to write
+  This extended document antiquotation allows us to write
  
   @{text [display] "@{ML_resp [display] \"true andalso false\" \"false\"}"}
 
@@ -155,8 +155,9 @@
   @{ML_resp [display] "let val i = 3 in (i * i, \"foo\") end" "(9, \<dots>)"} 
 
   In both cases, the check by the compiler ensures that code and result
-  match. A limitation of this antiquotation, however, is that the pattern can
-  only be given for values that can be constructed. This excludes
-  values that are abstract datatypes, like theorems or cterms.
+  match. A limitation of this document antiquotation, however, is that the
+  pattern can only be given for values that can be constructed. This excludes
+  values that are abstract datatypes, like @{ML_type thm}s and @{ML_type cterm}s.
+
 *}
 end
\ No newline at end of file