# HG changeset patch # User Christian Urban # Date 1236810889 0 # Node ID 009ca4807baad0f7511940c29e69356b6173843f # Parent 3e30ea95c7aa395246e8f9f5fdaa1698e82701aa polished somewhat the recipes and solutions diff -r 3e30ea95c7aa -r 009ca4807baa CookBook/Appendix.thy --- a/CookBook/Appendix.thy Wed Mar 11 17:38:17 2009 +0000 +++ b/CookBook/Appendix.thy Wed Mar 11 22:34:49 2009 +0000 @@ -11,23 +11,16 @@ text {* Possible further topics: - translations/print translations - + \begin{itemize} + \item translations/print translations; @{ML "ProofContext.print_syntax"} - user space type systems (in the form that already exists) - - unification and typing algorithms + \item user space type systems (in the form that already exists) - useful datastructures: + \item unification and typing algorithms - discrimination nets - - association lists + \item useful datastructures: discrimination nets, association lists + \end{itemize} *} -end - - - - +end \ No newline at end of file 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 diff -r 3e30ea95c7aa -r 009ca4807baa CookBook/Recipes/Config.thy --- a/CookBook/Recipes/Config.thy Wed Mar 11 17:38:17 2009 +0000 +++ b/CookBook/Recipes/Config.thy Wed Mar 11 22:34:49 2009 +0000 @@ -12,9 +12,9 @@ {\bf Solution:} This can be achieved using configuration values.\smallskip - Assume you want to control three values, namely @{text bval} containing a + Assume you want to control three values, say @{text bval} containing a boolean, @{text ival} containing an integer and @{text sval} - containing a string. These values can be declared on the ML-level with + containing a string. These values can be declared on the ML-level by *} ML{*val (bval, setup_bval) = Attrib.config_bool "bval" false @@ -23,7 +23,7 @@ text {* where each value needs to be given a default. To enable these values, they need to - be set up by + be set up with *} setup {* setup_bval *} @@ -51,18 +51,18 @@ @{ML_response [display,gray] "Config.put sval \"foo\" @{context}; Config.get @{context} sval" "foo"} - The same can be achived using the command \isacommand{setup}. + The same can be achieved using the command \isacommand{setup}. *} setup {* Config.put_thy sval "bar" *} text {* - The retrival of this value yields now + Now the retrival of this value yields: @{ML_response [display,gray] "Config.get @{context} sval" "\"bar\""} We can apply a function to a value using @{ML Config.map}. For example incrementing - @{ML ival} can be done by + @{ML ival} can be done by: @{ML_response [display,gray] "let @@ -80,5 +80,4 @@ multithreaded execution of Isabelle. *} - end \ No newline at end of file diff -r 3e30ea95c7aa -r 009ca4807baa CookBook/Recipes/TimeLimit.thy --- a/CookBook/Recipes/TimeLimit.thy Wed Mar 11 17:38:17 2009 +0000 +++ b/CookBook/Recipes/TimeLimit.thy Wed Mar 11 22:34:49 2009 +0000 @@ -11,16 +11,13 @@ {\bf Solution:} This can be achieved using the function @{ML timeLimit in TimeLimit}.\smallskip - Assume you defined the Ackermann function: - - *} + Assume you defined the Ackermann function on the ML-level. +*} ML{*fun ackermann (0, n) = n + 1 | ackermann (m, 0) = ackermann (m - 1, 1) | ackermann (m, n) = ackermann (m - 1, ackermann (m, n - 1)) *} -ML {* ackermann (3,4) *} - text {* Now the call @@ -28,7 +25,7 @@ @{ML_response_fake [display,gray] "ackermann (4, 12)" "\"} takes a bit of time before it finishes. To avoid this, the call can be encapsulated - in a time limit of five seconds. For this you have to write: + in a time limit of five seconds. For this you have to write @{ML_response [display,gray] "TimeLimit.timeLimit (Time.fromSeconds 5) ackermann (4, 12) @@ -39,7 +36,7 @@ is reached. Note that @{ML "timeLimit" in TimeLimit} is only meaningful when you use PolyML, - because PolyML has a rich infrastructure for multithreading programming on + because PolyML has the infrastructure for multithreading programming on which @{ML "timeLimit" in TimeLimit} relies. \begin{readmore} @@ -50,5 +47,4 @@ *} - end \ No newline at end of file diff -r 3e30ea95c7aa -r 009ca4807baa CookBook/Recipes/Timing.thy --- a/CookBook/Recipes/Timing.thy Wed Mar 11 17:38:17 2009 +0000 +++ b/CookBook/Recipes/Timing.thy Wed Mar 11 22:34:49 2009 +0000 @@ -11,18 +11,18 @@ {\bf Solution:} Time can be measured using the function @{ML start_timing} and @{ML end_timing}.\smallskip - Assume the following function defined in Isabelle. + Suppose you defined the Ackermann function inside Isabelle. *} fun - ackermann:: "(nat * nat) \ nat" + ackermann:: "(nat \ nat) \ nat" where "ackermann (0, n) = n + 1" | "ackermann (m, 0) = ackermann (m - 1, 1)" | "ackermann (m, n) = ackermann (m - 1, ackermann (m, n - 1))" text {* - We can now measure how long the simplifier takes to verify a datapoint + You can measure how long the simplifier takes to verify a datapoint of this function. The timing can be done using the following wrapper function: *} @@ -36,11 +36,13 @@ end*} text {* - Note that this function also takes a state @{text "st"} as an argument and - applies this state to the tactic. This is because tactics are lazy functions - and we need to force them to run, otherwise the timing will be meaningless. - The used time will be calculated as the end time minus the start time. - The wrapper can now be used in the proof + Note that this function, in addition to a tactic for which it measures the + time, also takes a state @{text "st"} as argument and applies this state to + the tactic. 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 for the wrapper is the proof + *} lemma "ackermann (3, 4) = 125" @@ -49,7 +51,9 @@ done text {* - where it returns something on the cale of 3 seconds. + where it returns something on the scale of 3 seconds. We choose to return + this information as a string, but the timing information is also accessible + in number format. \begin{readmore} Basic functions regarding timing are defined in @{ML_file diff -r 3e30ea95c7aa -r 009ca4807baa CookBook/Solutions.thy --- a/CookBook/Solutions.thy Wed Mar 11 17:38:17 2009 +0000 +++ b/CookBook/Solutions.thy Wed Mar 11 22:34:49 2009 +0000 @@ -24,18 +24,18 @@ ML{*val any = Scan.one (Symbol.not_eof) val scan_cmt = - let - val begin_cmt = Scan.this_string "(*" - val end_cmt = Scan.this_string "*)" - in - begin_cmt |-- Scan.repeat (Scan.unless end_cmt any) --| end_cmt - >> (enclose "(**" "**)" o implode) - end +let + val begin_cmt = Scan.this_string "(*" + val end_cmt = Scan.this_string "*)" +in + begin_cmt |-- Scan.repeat (Scan.unless end_cmt any) --| end_cmt + >> (enclose "(**" "**)" o implode) +end val parser = Scan.repeat (scan_cmt || any) val scan_all = - Scan.finite Symbol.stopper parser >> implode #> fst *} + Scan.finite Symbol.stopper parser >> implode #> fst *} text {* By using @{text "#> fst"} in the last line, the function @@ -95,6 +95,12 @@ text {* \solution{ex:addconversion} *} +text {* + The following code assumes the function @{ML dest_sum} from the previous + exercise. +*} + + ML{*fun add_simple_conv ctxt ctrm = let val trm = Thm.term_of ctrm @@ -122,10 +128,14 @@ Conv.concl_conv ~1 (add_conv ctxt))) ctxt) i end)*} +text {* + A test case is as follows +*} + lemma "P (Suc (99 + 1)) ((0 + 0)::nat) (Suc (3 + 3 + 3)) (4 + 1)" apply(tactic {* add_tac 1 *})? txt {* - where the simproc produces the goal state + where the conversion produces the goal state \begin{minipage}{\textwidth} @{subgoals [display]} diff -r 3e30ea95c7aa -r 009ca4807baa cookbook.pdf Binary file cookbook.pdf has changed