diff -r bcbcf5c839ae -r f6f8f8ba1eb1 CookBook/Recipes/Antiquotes.thy --- a/CookBook/Recipes/Antiquotes.thy Thu Jan 15 13:42:28 2009 +0000 +++ b/CookBook/Recipes/Antiquotes.thy Fri Jan 16 14:57:36 2009 +0000 @@ -15,18 +15,21 @@ 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 available document antiquotations and their options. + obtain a list of all currently available document antiquotations and their options. 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. - We first describe the antiquotation @{text "@{ML_checked \"expr\"}"}. This - antiquotation takes a piece of code as argument. The code is checked - by sending the ML-expression @{text [quotes] "val _ = \"} containing the - given argument 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: + 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 + antiquotation is as follows: *} @@ -44,16 +47,16 @@ Note that the parser @{ML "(Scan.lift Args.name)"} in line 9 parses a string, in this case the code given as argument. As mentioned before, this argument - is send to the ML-compiler in the line 4 using the function @{ML ml_val}, - which constructs an apropriate ML-expression. + 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_list (fn _ => fn s => Pretty.str s)"} in the next line pretty prints the code. This function expects that the code is a list of 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_list} when printing the code (for - example @{text "[display]"}, @{text "[quotes]"} and @{text "[source]"}). + that are observed by @{ML ThyOutput.output_list} when printing the code (including + @{text "[display]"}, @{text "[quotes]"} and @{text "[source]"}). \begin{readmore} For more information about options of antiquotations see \rsccite{sec:antiq}). @@ -62,7 +65,6 @@ 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%linenumbers{*fun output_ml src ctxt (code_txt,pos) = @@ -84,22 +86,24 @@ somebody changes the definition of \mbox{@{ML "(op +)"}}. - The second antiquotation extends the first by allowing also to give - hints what the result of the ML-code should be and to check the consistency of - the actual result with these hints. For this we are going to implement the - antiquotation - @{text "@{ML_resp \"expr\" \"pat\"}"} - whose first argument is the ML-code and the second is a pattern specifying - the result. To add some convenience and allow dealing with large outputs, - the user can give a partial specification including abbreviations - @{text [quotes] "\"}. + The second antiquotation we describe extends the first by allowing also to give + a pattern that specifies what the result of the ML-code should be and to check + the consistency of the actual result with the given pattern. For this we are going + to implement the antiquotation + + @{text [display] "@{ML_resp \"a_piece_of_code\" \"pattern\"}"} + + To add some convenience and also to deal with large outputs, + the user can give a partial specification by giving the abbreviation + @{text [quotes] "\"}. For example @{text "(\,\)"} for a pair. - Whereas in the antiquotation @{text "@{ML_checked \"expr\"}"} above, we have - sent the expression - @{text [quotes] "val _ = expr"} to the compiler, in this antiquotation the wildcard - @{text "_"} we will be replaced by a proper pattern constructed the hints. To - do this we need to replace the @{text [quotes] "\"} by @{text [quotes] "_"} - before sending the code to the compiler. The following function will do this: + 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 a proper pattern. To do this we + need to replace the @{text [quotes] "\"} by + @{text [quotes] "_"} before sending the code to the compiler. The following + function will do this: *} @@ -154,8 +158,8 @@ @{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 antiquotation is that the hints can only be given for results that can actually - be constructed as a pattern. This excludes values that are abstract datatypes, like + of this antiquotation, however, is that the hints can only be given in case + they can be constructed as a pattern. This excludes values that are abstract datatypes, like theorems or cterms. *}