ProgTutorial/Recipes/Antiquotes.thy
changeset 189 069d525f8f1d
parent 188 8939b8fd8603
child 191 0150cf5982ae
equal deleted inserted replaced
188:8939b8fd8603 189:069d525f8f1d
       
     1 
       
     2 theory Antiquotes
       
     3 imports "../Base"
       
     4 begin
       
     5 
       
     6 
       
     7 section {* Useful Document Antiquotations *}
       
     8 
       
     9 text {*
       
    10   (FIXME: update to to new antiquotation setup)
       
    11 
       
    12   {\bf Problem:} 
       
    13   How to keep your ML-code inside a document synchronised with the actual code?\smallskip
       
    14 
       
    15   {\bf Solution:} This can be achieved using document antiquotations.\smallskip
       
    16 
       
    17   Document antiquotations can be used for ensuring consistent type-setting of
       
    18   various entities in a document. They can also be used for sophisticated
       
    19   \LaTeX-hacking. If you type @{text "Ctrl-c Ctrl-a h A"} inside ProofGeneral, you
       
    20   obtain a list of all currently available document antiquotations and their options.
       
    21   You obtain the same list on the ML-level by typing
       
    22 
       
    23   @{ML [display,gray] "ThyOutput.print_antiquotations ()"}
       
    24 
       
    25   Below we give the code for two additional document antiquotations that can
       
    26   be used to typeset ML-code and also to check whether the given code actually
       
    27   compiles. This provides a sanity check for the code and also allows one to
       
    28   keep documents in sync with other code, for example Isabelle.
       
    29 
       
    30   We first describe the antiquotation @{text "ML_checked"} with the syntax:
       
    31  
       
    32   @{text [display] "@{ML_checked \"a_piece_of_code\"}"}
       
    33 
       
    34   The code is checked by sending the ML-expression @{text [quotes] "val _ =
       
    35   a_piece_of_code"} to the ML-compiler (i.e.~the function @{ML
       
    36   "ML_Context.eval_in"} in Line 4 below). The complete code of the
       
    37   document antiquotation is as follows:
       
    38 
       
    39 *}
       
    40 
       
    41 ML%linenosgray{*fun ml_val code_txt = "val _ = " ^ code_txt
       
    42 
       
    43 fun output_ml {context = ctxt, ...} code_txt =
       
    44   (ML_Context.eval_in (SOME ctxt) false Position.none (ml_val code_txt); 
       
    45    ThyOutput.output (map Pretty.str (space_explode "\n" code_txt)))
       
    46 
       
    47 val _ = ThyOutput.antiquotation "ML_checked" (Scan.lift Args.name) output_ml*}
       
    48 
       
    49 text {*
       
    50   The parser @{ML "(Scan.lift Args.name)"} in line 9 parses a string, in this
       
    51   case the code. As mentioned before, the code is sent to the ML-compiler in
       
    52   the line 4 using the function @{ML ml_val}, which constructs the appropriate
       
    53   ML-expression.  If the code is ``approved'' by the compiler, then the output
       
    54   function @{ML "ThyOutput.output"} in the next line pretty prints the
       
    55   code. This function expects that the code is a list of (pretty)strings where
       
    56   each string correspond to a line in the output. Therefore the use of @{ML
       
    57   "(space_explode \"\\n\" txt)" for txt} which produces this list according to
       
    58   linebreaks.  There are a number of options for antiquotations that are
       
    59   observed by @{ML ThyOutput.output} when printing the code (including @{text
       
    60   "[display]"} and @{text "[quotes]"}). Line 7 sets up the new document
       
    61   antiquotation.
       
    62 
       
    63 
       
    64   \begin{readmore}
       
    65   For more information about options of document antiquotations see \rsccite{sec:antiq}).
       
    66   \end{readmore}
       
    67 
       
    68   Since we used the argument @{ML "Position.none"}, the compiler cannot give specific 
       
    69   information about the line number, in case an error is detected. We 
       
    70   can improve the code above slightly by writing 
       
    71 *}
       
    72 
       
    73 ML%linenosgray{*fun output_ml {context = ctxt, ...} (code_txt, pos) =
       
    74   (ML_Context.eval_in (SOME ctxt) false pos (ml_val code_txt);
       
    75    ThyOutput.output (map Pretty.str (space_explode "\n" code_txt)))
       
    76 
       
    77 val _ = ThyOutput.antiquotation "ML_checked"
       
    78          (Scan.lift (OuterParse.position Args.name)) output_ml *}
       
    79 
       
    80 text {*
       
    81   where in Lines 1 and 2 the positional information is properly treated. The
       
    82   parser @{ML OuterParse.position} encodes the positional information in the 
       
    83   result.
       
    84 
       
    85   We can now write in a document @{text "@{ML_checked \"2 + 3\"}"} in order to
       
    86   obtain @{ML_checked "2 + 3"} and be sure that this code compiles until
       
    87   somebody changes the definition of \mbox{@{ML "(op +)"}}.
       
    88 
       
    89 
       
    90   The second document antiquotation we describe extends the first by a pattern
       
    91   that specifies what the result of the ML-code should be and check the
       
    92   consistency of the actual result with the given pattern. For this we are
       
    93   going to implement the document antiquotation
       
    94 
       
    95   
       
    96   @{text [display] "@{ML_resp \"a_piece_of_code\" \"a_pattern\"}"}
       
    97   
       
    98   To add some convenience and also to deal with large outputs, the user can
       
    99   give a partial specification inside the pattern by giving abbreviations of
       
   100   the form @{text [quotes] "\<dots>"}. For example @{text "(\<dots>, \<dots>)"} for specifying a
       
   101   pair.
       
   102 
       
   103   In the document antiquotation @{text "@{ML_checked \"piece_of_code\"}"}
       
   104   above we have sent the expression @{text [quotes] "val _ = piece_of_code"}
       
   105   to the compiler, now instead the wildcard @{text "_"} we will be replaced by
       
   106   the given pattern. To do this we need to replace in the input the @{text
       
   107   [quotes] "\<dots>"} by @{text [quotes] "_"} before sending the code to the
       
   108   compiler. The following function will do this:
       
   109 *}
       
   110 
       
   111 ML{*fun ml_pat (code_txt, pat) =
       
   112 let val pat' = 
       
   113          implode (map (fn "\<dots>" => "_" | s => s) (Symbol.explode pat))
       
   114 in 
       
   115   "val " ^ pat' ^ " = " ^ code_txt 
       
   116 end*}
       
   117 
       
   118 text {* 
       
   119   Next we like to add a response indicator to the result using:
       
   120 *}
       
   121 
       
   122 
       
   123 ML{*fun add_resp pat = map (fn s => "> " ^ s) pat*}
       
   124 
       
   125 text {* 
       
   126   The rest of the code of the document antiquotation is
       
   127 *}
       
   128 
       
   129 ML{*fun output_ml_resp {context = ctxt, ...} ((code_txt, pat), pos) = 
       
   130   (ML_Context.eval_in (SOME ctxt) false pos (ml_pat (code_txt, pat));
       
   131    let 
       
   132      val output1 = space_explode "\n" code_txt 
       
   133      val output2 = add_resp (space_explode "\n" pat)
       
   134    in 
       
   135      ThyOutput.output (map Pretty.str (output1 @ output2)) 
       
   136    end)
       
   137 
       
   138 val _ = ThyOutput.antiquotation "ML_resp" 
       
   139          (Scan.lift (OuterParse.position (Args.name -- Args.name))) 
       
   140            output_ml_resp*}
       
   141 
       
   142 text {*
       
   143   This extended document antiquotation allows us to write
       
   144  
       
   145   @{text [display] "@{ML_resp [display] \"true andalso false\" \"false\"}"}
       
   146 
       
   147   to obtain
       
   148 
       
   149   @{ML_resp [display] "true andalso false" "false"} 
       
   150 
       
   151   or 
       
   152 
       
   153   @{text [display] "@{ML_resp [display] \"let val i = 3 in (i * i, \"foo\") end\" \"(9, \<dots>)\"}"}
       
   154   
       
   155   to obtain
       
   156 
       
   157   @{ML_resp [display] "let val i = 3 in (i * i, \"foo\") end" "(9, \<dots>)"} 
       
   158 
       
   159   In both cases, the check by the compiler ensures that code and result
       
   160   match. A limitation of this document antiquotation, however, is that the
       
   161   pattern can only be given for values that can be constructed. This excludes
       
   162   values that are abstract datatypes, like @{ML_type thm}s and @{ML_type cterm}s.
       
   163 
       
   164 *}
       
   165 end