diff -r 3e30ea95c7aa -r 009ca4807baa CookBook/Recipes/Antiquotes.thy --- a/CookBook/Recipes/Antiquotes.thy Wed Mar 11 17:38:17 2009 +0000 +++ b/CookBook/Recipes/Antiquotes.thy Wed Mar 11 22:34:49 2009 +0000 @@ -36,20 +36,18 @@ *} -ML {* Pretty.str *} - ML%linenosgray{*fun ml_val code_txt = "val _ = " ^ code_txt -fun output_ml {source = src, context = ctxt, ...} 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))) + ThyOutput.output (map Pretty.str (space_explode "\n" code_txt))) val _ = ThyOutput.antiquotation "ML_checked" (Scan.lift Args.name) output_ml*} text {* 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 + 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 @@ -59,7 +57,8 @@ @{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]"}). + 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}). @@ -70,50 +69,47 @@ can improve the code above slightly by writing *} -ML%linenosgray{*fun output_ml {source = src, context = ctxt, ...} (code_txt,pos) = +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))) + ThyOutput.output (map Pretty.str (space_explode "\n" code_txt))) val _ = ThyOutput.antiquotation "ML_checked" - (Scan.lift (OuterParse.position Args.name)) output_ml *} + (Scan.lift (OuterParse.position Args.name)) output_ml *} text {* where in Lines 1 and 2 the positional information is properly treated. - (FIXME: say something about OuterParse.position) - 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 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 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 - @{text [display] "@{ML_resp \"a_piece_of_code\" \"pattern\"}"} + @{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 by giving the abbreviation - @{text [quotes] "\"}. For example @{text "(\,\)"} for a pair. + 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] "\"}. For example @{text "(\, \)"} to specify 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 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: - + 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] "\"} + 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' = +let val pat' = implode (map (fn "\" => "_" | s => s) (Symbol.explode pat)) - in - "val " ^ pat' ^ " = " ^ code_txt - end*} +in + "val " ^ pat' ^ " = " ^ code_txt +end*} text {* Next we like to add a response indicator to the result using: @@ -127,8 +123,8 @@ The rest of the code of the antiquotation is *} -ML{*fun output_ml_resp {source = src, context = ctxt, ...} ((code_txt,pat),pos) = - (ML_Context.eval_in (SOME ctxt) false pos (ml_pat (code_txt,pat)); +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) in @@ -136,7 +132,8 @@ end) val _ = ThyOutput.antiquotation "ML_resp" - (Scan.lift (OuterParse.position (Args.name -- Args.name))) output_ml_resp*} + (Scan.lift (OuterParse.position (Args.name -- Args.name))) + output_ml_resp*} text {* This extended antiquotation allows us to write @@ -149,20 +146,15 @@ or - @{text [display] "@{ML_resp [display] \"let val i = 3 in (i * i,\"foo\") end\" \"(9,\)\"}"} + @{text [display] "@{ML_resp [display] \"let val i = 3 in (i * i, \"foo\") end\" \"(9, \)\"}"} to obtain - @{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, 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. + @{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, 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. *} -end - - - - +end \ No newline at end of file