ProgTutorial/Recipes/Antiquotes.thy
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 13 Mar 2014 17:16:49 +0000
changeset 553 c53d74b34123
parent 517 d8c376662bb4
child 555 2c34c69236ce
permissions -rw-r--r--
updated to changes in Isabelle
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
theory Antiquotes
346
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 292
diff changeset
     3
imports "../Appendix"
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
begin
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
292
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 191
diff changeset
     6
section {* Useful Document Antiquotations\label{rec:docantiquotations} *}
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
text {*
47
4daf913fdbe1 hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
     9
  {\bf Problem:} 
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    10
  How to keep your ML-code inside a document synchronised with the actual code?\smallskip
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    12
  {\bf Solution:} This can be achieved with document antiquotations.\smallskip
47
4daf913fdbe1 hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
    13
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    14
  Document antiquotations can be used for ensuring consistent type-setting of
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    15
  various entities in a document. They can also be used for sophisticated
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    16
  \LaTeX-hacking. If you type on the Isabelle level
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    17
*}
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    18
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    19
print_antiquotations
153
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 114
diff changeset
    20
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    21
text {*
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    22
  you obtain a list of all currently available document antiquotations and
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    23
  their options.  
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    25
  Below we will give the code for two additional document
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    26
  antiquotations both of which are intended to typeset ML-code. The crucial point
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    27
  of these document antiquotations is that they not just print the ML-code, but also 
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    28
  check whether it compiles. This will provide a sanity check for the code
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    29
  and also allows you to keep documents in sync with other code, for example
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    30
  Isabelle.
47
4daf913fdbe1 hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
    31
74
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
    32
  We first describe the antiquotation @{text "ML_checked"} with the syntax:
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
    33
 
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
    34
  @{text [display] "@{ML_checked \"a_piece_of_code\"}"}
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
    35
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
    36
  The code is checked by sending the ML-expression @{text [quotes] "val _ =
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
    37
  a_piece_of_code"} to the ML-compiler (i.e.~the function @{ML
553
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
    38
  "ML_Context.eval_source_in"} in Line 7 below). The complete code of the
175
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
    39
  document antiquotation is as follows:
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    40
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
*}
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
114
13fd0a83d3c3 properly handled linenumbers in ML-text and Isar-proofs
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
    43
ML%linenosgray{*fun ml_val code_txt = "val _ = " ^ code_txt
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    45
fun output_ml {context = ctxt, ...} code_txt =
553
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
    46
let
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
    47
  val srcpos = {delimited = false, text = (ml_val code_txt), pos = Position.none}
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
    48
in
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
    49
  (ML_Context.eval_source_in (SOME ctxt) false srcpos; 
449
f952f2679a11 updated to new isabelle
Christian Urban <urbanc@in.tum.de>
parents: 438
diff changeset
    50
   Thy_Output.output ctxt (map Pretty.str (space_explode "\n" code_txt)))
553
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
    51
end
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
    53
val ml_checked_setup = Thy_Output.antiquotation @{binding "ML_checked"} (Scan.lift Args.name) output_ml*}
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
    54
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
    55
setup {* ml_checked_setup *}
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
text {*
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    58
  The parser @{ML "(Scan.lift Args.name)"} in Line 7 parses a string, in this
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    59
  case the code, and then calls the function @{ML output_ml}. As mentioned
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    60
  before, the parsed code is sent to the ML-compiler in Line 4 using the
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    61
  function @{ML ml_val}, which constructs the appropriate ML-expression, and
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    62
  using @{ML "eval_in" in ML_Context}, which calls the compiler.  If the code is
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    63
  ``approved'' by the compiler, then the output function @{ML "output" in
438
d9a223c023f6 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 426
diff changeset
    64
  Thy_Output} in the next line pretty prints the code. This function expects
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    65
  that the code is a list of (pretty)strings where each string correspond to a
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    66
  line in the output. Therefore the use of @{ML "(space_explode \"\\n\" txt)"
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    67
  for txt} which produces such a list according to linebreaks.  There are a
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    68
  number of options for antiquotations that are observed by the function 
438
d9a223c023f6 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 426
diff changeset
    69
  @{ML "output" in Thy_Output} when printing the code (including @{text "[display]"} 
d9a223c023f6 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 426
diff changeset
    70
  and @{text "[quotes]"}). The function @{ML "antiquotation" in Thy_Output} in 
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    71
  Line 7 sets up the new document antiquotation.
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    72
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    73
  \begin{readmore}
175
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
    74
  For more information about options of document antiquotations see \rsccite{sec:antiq}).
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    75
  \end{readmore}
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
47
4daf913fdbe1 hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
    77
  Since we used the argument @{ML "Position.none"}, the compiler cannot give specific 
59
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    78
  information about the line number, in case an error is detected. We 
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    79
  can improve the code above slightly by writing 
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
*}
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
    82
ML%linenosgray{*fun output_ml {context = ctxt, ...} (code_txt, pos) =
553
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
    83
let
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
    84
  val srcpos = {delimited = false, pos = pos, text = ml_val code_txt}
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
    85
in
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
    86
  (ML_Context.eval_source_in (SOME ctxt) false srcpos;
449
f952f2679a11 updated to new isabelle
Christian Urban <urbanc@in.tum.de>
parents: 438
diff changeset
    87
   Thy_Output.output ctxt (map Pretty.str (space_explode "\n" code_txt)))
553
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
    88
end
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
    90
val ml_checked_setup2 = Thy_Output.antiquotation @{binding "ML_checked2"}
426
d94755882e36 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
    91
         (Scan.lift (Parse.position Args.name)) output_ml *}
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
    93
setup {* ml_checked_setup2 *}
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
    94
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
text {*
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
    96
  where in Lines 1 and 2 the positional information is properly treated. The
426
d94755882e36 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
    97
  parser @{ML Parse.position} encodes the positional information in the 
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
    98
  result.
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    99
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
   100
  We can now write @{text "@{ML_checked2 \"2 + 3\"}"} in a document in order to
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
   101
  obtain @{ML_checked2 "2 + 3"} and be sure that this code compiles until
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   102
  somebody changes the definition of addition.
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   103
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   104
175
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   105
  The second document antiquotation we describe extends the first by a pattern
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   106
  that specifies what the result of the ML-code should be and checks the
175
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   107
  consistency of the actual result with the given pattern. For this we are
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   108
  going to implement the document antiquotation:
175
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   109
74
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
   110
  
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   111
  @{text [display] "@{ML_resp \"a_piece_of_code\" \"a_pattern\"}"}
74
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
   112
  
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   113
  To add some convenience and also to deal with large outputs, the user can
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   114
  give a partial specification by using ellipses. For example @{text "(\<dots>, \<dots>)"}
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   115
  for specifying a pair.  In order to check consistency between the pattern
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   116
  and the output of the code, we have to change the ML-expression that is sent 
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
   117
  to the compiler: in @{text "ML_checked2"} we sent the expression @{text [quotes]
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   118
  "val _ = a_piece_of_code"} to the compiler; now the wildcard @{text "_"}
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   119
  must be be replaced by the given pattern. However, we have to remove all
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   120
  ellipses from it and replace them by @{text [quotes] "_"}. The following 
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   121
  function will do this:
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
*}
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
517
d8c376662bb4 removed special ML-setup and replaced it by explicit markups (i.e., %grayML)
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   124
ML %grayML{*fun ml_pat (code_txt, pat) =
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   125
let val pat' = 
59
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
   126
         implode (map (fn "\<dots>" => "_" | s => s) (Symbol.explode pat))
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   127
in 
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   128
  "val " ^ pat' ^ " = " ^ code_txt 
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   129
end*}
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   131
text {* 
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   132
  Next we add a response indicator to the result using:
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   133
*}
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   134
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   135
517
d8c376662bb4 removed special ML-setup and replaced it by explicit markups (i.e., %grayML)
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   136
ML %grayML{*fun add_resp pat = map (fn s => "> " ^ s) pat*}
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   138
text {* 
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   139
  The rest of the code of @{text "ML_resp"} is: 
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   140
*}
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   141
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   142
ML %linenosgray{*fun output_ml_resp {context = ctxt, ...} ((code_txt, pat), pos) = 
553
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
   143
  (let
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
   144
     val srcpos = {delimited = false, text = ml_pat (code_txt, pat), pos = pos}
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
   145
   in
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
   146
     ML_Context.eval_source_in (SOME ctxt) false srcpos 
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
   147
   end;
59
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
   148
   let 
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   149
     val code_output = space_explode "\n" code_txt 
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   150
     val resp_output = add_resp (space_explode "\n" pat)
59
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
   151
   in 
449
f952f2679a11 updated to new isabelle
Christian Urban <urbanc@in.tum.de>
parents: 438
diff changeset
   152
     Thy_Output.output ctxt (map Pretty.str (code_output @ resp_output)) 
59
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
   153
   end)
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
553
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
   155
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
   156
val ml_resp_setup = Thy_Output.antiquotation @{binding "ML_resp"} 
426
d94755882e36 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   157
          (Scan.lift (Parse.position (Args.name -- Args.name))) 
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   158
             output_ml_resp*}
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
   160
setup {* ml_resp_setup *}
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
   161
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   162
text {*
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
   163
  In comparison with @{text "ML_checked2"}, we only changed the line about 
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   164
  the compiler (Line~2), the lines about
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   165
  the output (Lines 4 to 7) and the parser in the setup (Line 11). Now 
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   166
  you can write
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   167
 
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   168
  @{text [display] "@{ML_resp [display] \"true andalso false\" \"false\"}"}
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   169
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   170
  to obtain
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   172
  @{ML_resp [display] "true andalso false" "false"} 
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   174
  or 
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   175
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   176
  @{text [display] "@{ML_resp [display] \"let val i = 3 in (i * i, \"foo\") end\" \"(9, \<dots>)\"}"}
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   177
  
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   178
  to obtain
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   179
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   180
  @{ML_resp [display] "let val i = 3 in (i * i, \"foo\") end" "(9, \<dots>)"} 
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   181
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   182
  In both cases, the check by the compiler ensures that code and result
175
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   183
  match. A limitation of this document antiquotation, however, is that the
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   184
  pattern can only be given for values that can be constructed. This excludes
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   185
  values that are abstract datatypes, like @{ML_type thm}s and @{ML_type cterm}s.
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   186
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   187
*}
517
d8c376662bb4 removed special ML-setup and replaced it by explicit markups (i.e., %grayML)
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   188
end