tuned
authorChristian Urban <urbanc@in.tum.de>
Wed, 17 Dec 2008 05:08:33 +0000
changeset 59 b5914f3c643c
parent 58 f3794c231898
child 60 5b9c6010897b
tuned
CookBook/Parsing.thy
CookBook/Readme.thy
CookBook/Recipes/Antiquotes.thy
cookbook.pdf
--- a/CookBook/Parsing.thy	Tue Dec 16 17:28:05 2008 +0000
+++ b/CookBook/Parsing.thy	Wed Dec 17 05:08:33 2008 +0000
@@ -304,7 +304,19 @@
 end"
 "((\"hh\", \"ee\"), [\"l\", \"l\", \"o\"])"}
 
-  doubles the two parsed input strings.
+  doubles the two parsed input strings. Or
+
+  @{ML_response [display] 
+"let 
+   val p = Scan.repeat (Scan.one Symbol.not_eof) >> implode
+   val input = (explode \"foo bar foo\") 
+in
+   Scan.finite Symbol.stopper p input
+end" 
+"(\"foo bar foo\",[])"}
+
+  where the single strings in the parsed output are transformed
+  back into one string.
  
   \begin{exercise}\label{ex:scancmts}
   Write a parser that parses an input string so that any comment enclosed
--- a/CookBook/Readme.thy	Tue Dec 16 17:28:05 2008 +0000
+++ b/CookBook/Readme.thy	Wed Dec 17 05:08:33 2008 +0000
@@ -13,6 +13,9 @@
   @{text "isabelle make"}
   \end{center}
 
+  You very likely need a recent snapshot of Isabelle in order to compile
+  the cookbook.
+
   \item You can include references to other Isabelle manuals using the 
   reference names from those manuals. To do this the following
   four \LaTeX{} commands are defined:
@@ -47,9 +50,9 @@
   
   \begin{center}\small
   \begin{tabular}{lll}
-  @{text "@{ML \"1 + 3\"}"}         &         & @{ML "1 + 3"}\\
-  @{text "@{ML \"a + b\" for a b}"} & produce & @{ML "a + b" for a b}\\
-  @{text "@{ML Ident in OuterLex}"} &         & @{ML Ident in OuterLex}\\
+  @{text "@{ML \"1 + 3\"}"}         &                 & @{ML "1 + 3"}\\
+  @{text "@{ML \"a + b\" for a b}"} & \;\;produce\;\; & @{ML "a + b" for a b}\\
+  @{text "@{ML Ident in OuterLex}"} &                 & @{ML Ident in OuterLex}\\
   \end{tabular}
   \end{center}
 
@@ -57,7 +60,7 @@
   display ML-expressions and their response.  The first expression is checked
   like in the antiquotation @{text "@{ML \"expr\"}"}; the second is a pattern
   that specifies the result the first expression produces. This pattern can
-  contain @{text "\<dots>"} for parts that you like to omit. The response of the
+  contain @{text [quotes] "\<dots>"} for parts that you like to omit. The response of the
   first expression will be checked against this pattern. Examples are:
 
   \begin{center}\small
@@ -88,14 +91,25 @@
 
   \begin{center}\small
   \begin{tabular}{ll}
-  @{text "@{ML_response"} & @{text "\"cterm_of @{theory} @{term \\\"a + b = c\\\"}\"}"}\\
-                          & @{text "\"a + b = c\"}"}\smallskip\\ 
-  @{text "@{ML_response"} & @{text "\"($$ \\\"x\\\") (explode \\\"world\\\")\""}\\ 
-                          & @{text "\"Exception FAIL raised\"}"}
+  @{text "@{ML_response_fake"} & @{text "\"cterm_of @{theory} @{term \\\"a + b = c\\\"}\"}"}\\
+                               & @{text "\"a + b = c\"}"}\smallskip\\ 
+  @{text "@{ML_response_fake"} & @{text "\"($$ \\\"x\\\") (explode \\\"world\\\")\""}\\ 
+                               & @{text "\"Exception FAIL raised\"}"}
   \end{tabular}
   \end{center}
 
+  which produce respectively
+
+  \begin{center}\small
+  \begin{tabular}{p{7.2cm}}
+  @{ML_response_fake "cterm_of @{theory} @{term \"a + b = c\"}" "a + b = c"}\smallskip\\
+  @{ML_response_fake "($$ \"x\") (explode \"world\")" "Exception FAIL raised"}
+  \end{tabular}
+  \end{center}
   
+  This output mimics to some extend what the user sees when running the
+  code.
+
   \item[$\bullet$] @{text "@{ML_response_fake_both \"expr\" \"pat\"}"} can be
   used to show erroneous code. Neither the code nor the response will be
   checked. An example is:
@@ -108,7 +122,10 @@
   \end{center}
 
   \item[$\bullet$] @{text "@{ML_file \"name\"}"} should be used when
-  referring to a file. It checks whether the file exists.  
+  referring to a file. It checks whether the file exists.  An example
+  is 
+
+  @{text [display] "@{ML_file \"Pure/General/basics.ML\"}"}
   \end{itemize}
 
   The listed antiquotations honour options including @{text "[display]"} and 
@@ -118,7 +135,7 @@
   @{text "@{ML [quotes] \"\\\"foo\\\" ^ \\\"bar\\\"\"}"} \;\;produces\;\; @{text [quotes] "foobar"}
   \end{center}
 
-  while 
+  whereas
   
   \begin{center}\small
   @{text "@{ML \"\\\"foo\\\" ^ \\\"bar\\\"\"}"} \;\;produces only\;\; @{text "foobar"}
--- a/CookBook/Recipes/Antiquotes.thy	Tue Dec 16 17:28:05 2008 +0000
+++ b/CookBook/Recipes/Antiquotes.thy	Wed Dec 17 05:08:33 2008 +0000
@@ -22,34 +22,36 @@
   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 argument is checked
+  antiquotation takes a piece of code as argument. The code is checked
   by sending the ML-expression @{text [quotes] "val _ = \<dots>"} containing the
   given argument to the ML-compiler (i.e.~the function @{ML "ML_Context.eval_in"}
-  in Line 4 in the code below). The code of @{text "@{ML_checked \"expr\"}"} 
-  is as follows:
+  in Line 4 below). The complete code of the antiquotation is as follows:
 
 *}
 
-ML %linenumbers {*fun ml_val txt = "val _ = " ^ txt
+ML %linenumbers {*fun ml_val code_txt = "val _ = " ^ code_txt
 
-fun output_ml ml src ctxt txt =
-  (ML_Context.eval_in (SOME ctxt) false Position.none (ml txt); 
+fun output_ml src ctxt code_txt =
+  (ML_Context.eval_in (SOME ctxt) false Position.none (ml_val code_txt); 
   ThyOutput.output_list (fn _ => fn s => Pretty.str s) src ctxt 
-                                            (space_explode "\n" txt))
+                                            (space_explode "\n" code_txt))
 
 val _ = ThyOutput.add_commands
- [("ML_checked", ThyOutput.args (Scan.lift Args.name) (output_ml ml_val))]
+ [("ML_checked", ThyOutput.args (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. This argument is send to the ML-compiler in the line 4.  
+  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.
   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. Therefore the @{ML "(space_explode \"\\n\" txt)" for txt} 
-  which produces this list.  There are a number of options for antiquotations
+  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]"}).
 
@@ -58,19 +60,19 @@
   \end{readmore}
 
   Since we used the argument @{ML "Position.none"}, the compiler cannot give specific 
-  information about the line number where an error might have occurred. We 
-  can improve this code slightly by writing 
+  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 ml src ctxt (txt,pos) =
-  (ML_Context.eval_in (SOME ctxt) false pos (ml txt);
+ML %linenumbers {* fun output_ml src ctxt (code_txt,pos) =
+  (ML_Context.eval_in (SOME ctxt) false pos (ml_val code_txt);
   ThyOutput.output_list (fn _ => fn s => Pretty.str s) src ctxt 
-                                            (space_explode "\n" txt))
+                                            (space_explode "\n" code_txt))
 
 val _ = ThyOutput.add_commands
  [("ML_checked", ThyOutput.args 
-       (Scan.lift (OuterParse.position Args.name)) (output_ml ml_val))]
+       (Scan.lift (OuterParse.position Args.name)) output_ml)]
 *}
 
 text {*
@@ -84,36 +86,41 @@
 
 
   The second antiquotation extends the first by allowing also to give
-  hints what the result of the ML-code is and check the consistency of 
-  the actual result with these hints. For this we use the antiquotation  
+  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_response \"expr\" \"pat\"}"}
   whose first argument is the ML-code and the second is a pattern specifying
-  the result. To add some convenience we allow the user to give a partial
-  specification using @{text "\<dots>"}.
+  the result. To add some convenience and allow dealing with large outputs,
+  the user can give a partial specification including abbreviations 
+  @{text [quotes] "\<dots>"}.
 
-  In the antiquotation @{text "@{ML_checked \"expr\"}"} we send the expression 
-  @{text [quotes] "val _ = expr"} to the compiler. Instead of the wildcard
-  @{text "_"}, we will here use the hints to construct a proper pattern. To
-  do this we need to replace the @{text "\<dots>"} by @{text "_"} before sending the 
-  code to the compiler. The function 
+  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] "\<dots>"} by @{text [quotes] "_"} 
+  before sending the code to the compiler. The following function will do this:
 
 *}
 
 ML {* 
-fun ml_pat (rhs, pat) =
-let val pat' = implode (map (fn "\<dots>" => "_" | s => s) (Symbol.explode pat))
-in "val " ^ pat' ^ " = " ^ rhs end;
+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 {* 
-  will construct the pattern that the compiler can use. Next we like to add 
-  a response indicator to the result using:
+  Next we like to add a response indicator to the result using:
 *}
 
 
 ML {*
-fun add_response_indicator txt =
-  map (fn s => "> " ^ s) (space_explode "\n" txt)
+fun add_response_indicator pat =
+  map (fn s => "> " ^ s) (space_explode "\n" pat)
 *}
 
 text {* 
@@ -121,16 +128,19 @@
   *}
 
 ML {*
-fun output_ml_response ml src ctxt ((lhs,pat),pos) = 
-  (ML_Context.eval_in (SOME ctxt) false pos (ml (lhs,pat));
-  let val txt = (space_explode "\n" lhs) @ (add_response_indicator pat)
-  in ThyOutput.output_list (fn _ => fn s => Pretty.str s) src ctxt txt end)
+fun output_ml_response src 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_response_indicator pat)
+   in 
+     ThyOutput.output_list (fn _ => fn s => Pretty.str s) src ctxt output 
+   end)
 
 val _ = ThyOutput.add_commands
  [("ML_response", 
      ThyOutput.args 
       (Scan.lift (OuterParse.position (Args.name -- Args.name))) 
-      (output_ml_response ml_pat))]
+        output_ml_response)]
 *}
 
 text {*
Binary file cookbook.pdf has changed