CookBook/Recipes/Antiquotes.thy
changeset 189 069d525f8f1d
parent 188 8939b8fd8603
child 190 ca0ac2e75f6d
--- a/CookBook/Recipes/Antiquotes.thy	Wed Mar 18 23:52:51 2009 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,165 +0,0 @@
-
-theory Antiquotes
-imports "../Base"
-begin
-
-
-section {* Useful Document Antiquotations *}
-
-text {*
-  (FIXME: update to to new antiquotation setup)
-
-  {\bf Problem:} 
-  How to keep your ML-code inside a document synchronised with the actual code?\smallskip
-
-  {\bf Solution:} This can be achieved using document antiquotations.\smallskip
-
-  Document antiquotations can be used for ensuring consistent type-setting of
-  various entities in a document. They can also be used for sophisticated
-  \LaTeX-hacking. If you type @{text "Ctrl-c Ctrl-a h A"} inside ProofGeneral, you
-  obtain a list of all currently available document antiquotations and their options.
-  You obtain the same list on the ML-level by typing
-
-  @{ML [display,gray] "ThyOutput.print_antiquotations ()"}
-
-  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:
- 
-  @{text [display] "@{ML_checked \"a_piece_of_code\"}"}
-
-  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
-  document antiquotation is as follows:
-
-*}
-
-ML%linenosgray{*fun ml_val code_txt = "val _ = " ^ code_txt
-
-fun output_ml {context = ctxt, ...} code_txt =
-  (ML_Context.eval_in (SOME ctxt) false Position.none (ml_val code_txt); 
-   ThyOutput.output (map Pretty.str (space_explode "\n" code_txt)))
-
-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.
-
-
-  \begin{readmore}
-  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 
-  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) =
-  (ML_Context.eval_in (SOME ctxt) false pos (ml_val code_txt);
-   ThyOutput.output (map Pretty.str (space_explode "\n" code_txt)))
-
-val _ = ThyOutput.antiquotation "ML_checked"
-         (Scan.lift (OuterParse.position Args.name)) output_ml *}
-
-text {*
-  where in Lines 1 and 2 the positional information is properly treated. The
-  parser @{ML OuterParse.position} encodes the positional information in the 
-  result.
-
-  We can now write in a document @{text "@{ML_checked \"2 + 3\"}"} in order to
-  obtain @{ML_checked "2 + 3"} and be sure that this code compiles until
-  somebody changes the definition of \mbox{@{ML "(op +)"}}.
-
-
-  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>)"} for specifying a
-  pair.
-
-  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) =
-let val pat' = 
-         implode (map (fn "\<dots>" => "_" | s => s) (Symbol.explode pat))
-in 
-  "val " ^ pat' ^ " = " ^ code_txt 
-end*}
-
-text {* 
-  Next we like to add a response indicator to the result using:
-*}
-
-
-ML{*fun add_resp pat = map (fn s => "> " ^ s) pat*}
-
-text {* 
-  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 output1 = space_explode "\n" code_txt 
-     val output2 = add_resp (space_explode "\n" pat)
-   in 
-     ThyOutput.output (map Pretty.str (output1 @ output2)) 
-   end)
-
-val _ = ThyOutput.antiquotation "ML_resp" 
-         (Scan.lift (OuterParse.position (Args.name -- Args.name))) 
-           output_ml_resp*}
-
-text {*
-  This extended document antiquotation allows us to write
- 
-  @{text [display] "@{ML_resp [display] \"true andalso false\" \"false\"}"}
-
-  to obtain
-
-  @{ML_resp [display] "true andalso false" "false"} 
-
-  or 
-
-  @{text [display] "@{ML_resp [display] \"let val i = 3 in (i * i, \"foo\") end\" \"(9, \<dots>)\"}"}
-  
-  to obtain
-
-  @{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 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