# HG changeset patch # User Christian Urban # Date 1237501286 -3600 # Node ID 0150cf5982ae3ed166f37f61008ab41810f6884b # Parent ca0ac2e75f6dbbbbd54029604c250056260fd726 polished some recipies diff -r ca0ac2e75f6d -r 0150cf5982ae ProgTutorial/Recipes/Antiquotes.thy --- a/ProgTutorial/Recipes/Antiquotes.thy Thu Mar 19 17:50:28 2009 +0100 +++ b/ProgTutorial/Recipes/Antiquotes.thy Thu Mar 19 23:21:26 2009 +0100 @@ -3,29 +3,31 @@ 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 + {\bf Solution:} This can be achieved with 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 + \LaTeX-hacking. If you type on the Isabelle level +*} + +print_antiquotations - @{ML [display,gray] "ThyOutput.print_antiquotations ()"} +text {* + you obtain a list of all currently available document antiquotations and + their options. - 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. + Below we will give the code for two additional document + antiquotations both of which are intended to typeset ML-code. The crucial point + of these document antiquotations is that they not just print the ML-code, but also + check whether it compiles. This will provide a sanity check for the code + and also allows you to keep documents in sync with other code, for example + Isabelle. We first describe the antiquotation @{text "ML_checked"} with the syntax: @@ -47,19 +49,20 @@ 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 7 parses a string, in this + case the code, and then calls the function @{ML output_ml}. As mentioned + before, the parsed code is sent to the ML-compiler in Line 4 using the + 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 + ThyOutput} 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 ThyOutput} when printing the code (including @{text "[display]"} + and @{text "[quotes]"}). The function @{ML "antiquotation" in ThyOutput} in + Line 7 sets up the new document antiquotation. \begin{readmore} For more information about options of document antiquotations see \rsccite{sec:antiq}). @@ -82,30 +85,28 @@ 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 + We can now write @{text "@{ML_checked \"2 + 3\"}"} in a document in order to obtain @{ML_checked "2 + 3"} and be sure that this code compiles until - somebody changes the definition of \mbox{@{ML "(op +)"}}. + somebody changes the definition of addition. 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 + that specifies what the result of the ML-code should be and checks the consistency of the actual result with the given pattern. For this we are - going to implement the document antiquotation + 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] "\"}. For example @{text "(\, \)"} 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] "\"} by @{text [quotes] "_"} before sending the code to the - compiler. The following function will do this: + give a partial specification by using ellipses. For example @{text "(\, \)"} + for specifying a pair. In order to check consistency between the pattern + and the output of the code, we have to change the ML-expression that is sent + to the compiler: in @{text "ML_checked"} we sent the expression @{text [quotes] + "val _ = a_piece_of_code"} to the compiler; now the wildcard @{text "_"} + must be be replaced by the given pattern. However, we have to remove all + ellipses from it and replace them by @{text [quotes] "_"}. The following + function will do this: *} ML{*fun ml_pat (code_txt, pat) = @@ -116,31 +117,34 @@ end*} text {* - Next we like to add a response indicator to the result using: + Next we 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 + The rest of the code of @{text "ML_resp"} is: *} -ML{*fun output_ml_resp {context = ctxt, ...} ((code_txt, pat), pos) = +ML %linenosgray{*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) + val code_output = space_explode "\n" code_txt + val resp_output = add_resp (space_explode "\n" pat) in - ThyOutput.output (map Pretty.str (output1 @ output2)) + ThyOutput.output (map Pretty.str (code_output @ resp_output)) 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 document antiquotation allows us to write + 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 @{text [display] "@{ML_resp [display] \"true andalso false\" \"false\"}"} diff -r ca0ac2e75f6d -r 0150cf5982ae ProgTutorial/Recipes/Config.thy --- a/ProgTutorial/Recipes/Config.thy Thu Mar 19 17:50:28 2009 +0100 +++ b/ProgTutorial/Recipes/Config.thy Thu Mar 19 23:21:26 2009 +0100 @@ -4,7 +4,6 @@ section {* Configuration Options\label{rec:config} *} - text {* {\bf Problem:} You would like to enhance your tool with options that can be changed @@ -29,7 +28,7 @@ setup {* setup_bval *} setup {* setup_ival *} -text {* or on the ML-level *} +text {* or on the ML-level with *} ML{*setup_sval @{theory} *} @@ -57,7 +56,7 @@ setup {* Config.put_thy sval "bar" *} text {* - Now the retrival of this value yields: + Now the retrieval of this value yields: @{ML_response [display,gray] "Config.get @{context} sval" "\"bar\""} @@ -66,9 +65,9 @@ @{ML_response [display,gray] "let - val ctxt = Config.map ival (fn i => i + 1) @{context} + val ctxt' = Config.map ival (fn i => i + 1) @{context} in - Config.get ctxt ival + Config.get ctxt' ival end" "4"} \begin{readmore} @@ -76,8 +75,8 @@ \end{readmore} There are many good reasons to control parameters in this way. One is - that it avoid global references, which cause many headaches with the - multithreaded execution of Isabelle. - - *} + that no global reference is needed, which would cause many headaches + with the multithreaded execution of Isabelle. +*} + end \ No newline at end of file diff -r ca0ac2e75f6d -r 0150cf5982ae ProgTutorial/Recipes/Sat.thy --- a/ProgTutorial/Recipes/Sat.thy Thu Mar 19 17:50:28 2009 +0100 +++ b/ProgTutorial/Recipes/Sat.thy Thu Mar 19 23:21:26 2009 +0100 @@ -62,7 +62,8 @@ "map (apfst (Syntax.string_of_term @{context})) (Termtab.dest table')" "(\x. P x, 1)"} - We used some pretty printing scaffolding to see better what the output is. + In the print out of the tabel, we used some pretty printing scaffolding + to see better which assignment the table contains. Having produced a propositional formula, you can now call the SAT solvers with the function @{ML "SatSolver.invoke_solver"}. For example @@ -104,8 +105,9 @@ done text {* - However, for proving anything more exciting you have to use a SAT solver - that can produce a proof. The internal one is not usuable for this. + However, for proving anything more exciting using @{ML "sat_tac" in sat} you + have to use a SAT solver that can produce a proof. The internal + one is not usuable for this. \begin{readmore} The interface for the external SAT solvers is implemented diff -r ca0ac2e75f6d -r 0150cf5982ae ProgTutorial/Recipes/TimeLimit.thy --- a/ProgTutorial/Recipes/TimeLimit.thy Thu Mar 19 17:50:28 2009 +0100 +++ b/ProgTutorial/Recipes/TimeLimit.thy Thu Mar 19 23:21:26 2009 +0100 @@ -8,8 +8,8 @@ {\bf Problem:} Your tool should run only a specified amount of time.\smallskip - {\bf Solution:} This can be achieved using the function - @{ML timeLimit in TimeLimit}.\smallskip + {\bf Solution:} In PolyML 5.2.1 and later, this can be achieved + using the function @{ML timeLimit in TimeLimit}.\smallskip Assume you defined the Ackermann function on the ML-level. *} diff -r ca0ac2e75f6d -r 0150cf5982ae ProgTutorial/Recipes/Timing.thy --- a/ProgTutorial/Recipes/Timing.thy Thu Mar 19 17:50:28 2009 +0100 +++ b/ProgTutorial/Recipes/Timing.thy Thu Mar 19 23:21:26 2009 +0100 @@ -11,7 +11,7 @@ {\bf Solution:} Time can be measured using the function @{ML start_timing} and @{ML end_timing}.\smallskip - Suppose you defined the Ackermann function inside Isabelle. + Suppose you defined the Ackermann function on the Isabelle level. *} fun @@ -23,7 +23,7 @@ text {* You can measure how long the simplifier takes to verify a datapoint - of this function. The timing of a tactic can be done using the following + of this function. The actual timing is done inside the wrapper function: *} @@ -40,11 +40,14 @@ Note that this function, in addition to a tactic, also takes a state @{text "st"} as argument and applies this state to the tactic (Line 4). The reason is that tactics are lazy functions and you need to force them to run, otherwise the - timing will be meaningless. The time between start and finish of the tactic - will be calculated as the end time minus the start time. An example of the + timing will be meaningless. The simplifier tactic, amongst others, can be + forced to run by just applying the state to it. But ``fully'' lazy tactics, + such as @{ML "resolve_tac"}, need even more ``standing-on-ones-head'' to force + them to run. + + The time between start and finish of the simplifier will be calculated + as the end time minus the start time. An example of the wrapper is the proof - - *} lemma "ackermann (3, 4) = 125" diff -r ca0ac2e75f6d -r 0150cf5982ae progtutorial.pdf Binary file progtutorial.pdf has changed