ProgTutorial/Recipes/Antiquotes.thy
author Norbert Schirmer <norbert.schirmer@web.de>
Fri, 17 May 2019 10:38:01 +0200
changeset 569 f875a25aa72d
parent 565 cecd7a941885
child 573 321e220a6baa
permissions -rw-r--r--
prefer cartouches over " in ML antiquotations
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
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
     6
section \<open>Useful Document Antiquotations\label{rec:docantiquotations}\<close>
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
     8
text \<open>
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
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    17
\<close>
191
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
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    21
text \<open>
191
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
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    32
  We first describe the antiquotation \<open>ML_checked\<close> with the syntax:
74
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
    33
 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 565
diff changeset
    34
  @{text [display] \<open>@{ML_checked "a_piece_of_code"}\<close>}
74
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 _ =
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 565
diff changeset
    37
  a_piece_of_code"} to the ML-compiler (i.e.~the function @{ML \<open>ML_Context.eval_source_in\<close>} in Line 7 below). The complete code of the
175
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
    38
  document antiquotation is as follows:
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    39
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    40
\<close>
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
    41
ML \<open>Input.pos_of\<close>
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    42
ML%linenosgray\<open>fun ml_enclose bg en source =
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    43
  ML_Lex.read bg @ ML_Lex.read_source false source @ ML_Lex.read en;\<close>
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    45
ML%linenosgray\<open>fun ml_val code_txt = (ml_enclose "val _ = " "" code_txt)
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
    47
fun output_ml 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
    48
let
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
    49
  val _ = ML_Context.eval_in (SOME ctxt) ML_Compiler.flags (Input.pos_of code_txt) (ml_val code_txt)
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
    50
in 
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
    51
   Pretty.str (Input.source_content code_txt)
553
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
    52
end
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    54
val ml_checked_setup = Thy_Output.antiquotation_pretty_source @{binding "ML_checked"} (Scan.lift Args.text_input) output_ml\<close>
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
    55
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    56
setup \<open>ml_checked_setup\<close>
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
    58
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    59
text \<open>
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 565
diff changeset
    60
  The parser @{ML \<open>(Scan.lift Args.name)\<close>} in Line 7 parses a string, in this
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    61
  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
    62
  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
    63
  function @{ML ml_val}, which constructs the appropriate ML-expression, and
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 565
diff changeset
    64
  using @{ML \<open>eval_in\<close> in ML_Context}, which calls the compiler.  If the code is
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 565
diff changeset
    65
  ``approved'' by the compiler, then the output function @{ML \<open>output\<close> in
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
    66
  Document_Antiquotation} 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
    67
  that the code is a list of (pretty)strings where each string correspond to a
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 565
diff changeset
    68
  line in the output. Therefore the use of @{ML \<open>(space_explode "\\n" txt)\<close>
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    69
  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
    70
  number of options for antiquotations that are observed by the function 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 565
diff changeset
    71
  @{ML \<open>output\<close> in Document_Antiquotation} when printing the code (including \<open>[display]\<close> 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 565
diff changeset
    72
  and \<open>[quotes]\<close>). The function @{ML \<open>antiquotation_raw\<close> in Thy_Output} in 
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    73
  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
    74
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    75
  \begin{readmore}
175
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
    76
  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
    77
  \end{readmore}
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 565
diff changeset
    79
  Since we used the argument @{ML \<open>Position.none\<close>}, the compiler cannot give specific 
59
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    80
  information about the line number, in case an error is detected. We 
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    81
  can improve the code above slightly by writing 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    82
\<close>
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
    83
(* FIXME: remove
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
    84
ML%linenosgray{*fun output_ml 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
    85
let
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
    86
  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
    87
in
555
2c34c69236ce updated to new isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 553
diff changeset
    88
  (ML_Context.eval_source_in (SOME ctxt) ML_Compiler.flags srcpos;
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
    89
   code_txt 
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
    90
   |> space_explode "\n"
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
    91
   |> map Pretty.str
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
    92
   |> Pretty.list "" ""
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
    93
   |> Document_Antiquotation.output ctxt
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
    94
   |> Latex.string)
553
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
    95
end
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
    97
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
    98
         (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
    99
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
   100
setup {* ml_checked_setup2 *}
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   101
*)
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   102
text \<open>
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   103
  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
   104
  parser @{ML Parse.position} encodes the positional information in the 
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   105
  result.
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   106
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   107
  We can now write \<open>@{ML_checked2 "2 + 3"}\<close> in a document in order to
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   108
  obtain @{ML_checked "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
   109
  somebody changes the definition of addition.
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   110
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   111
175
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   112
  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
   113
  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
   114
  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
   115
  going to implement the document antiquotation:
175
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   116
74
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
   117
  
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 565
diff changeset
   118
  @{text [display] \<open>@{ML_resp "a_piece_of_code" "a_pattern"}\<close>}
74
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
   119
  
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   120
  To add some convenience and also to deal with large outputs, the user can
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   121
  give a partial specification by using ellipses. For example \<open>(\<dots>, \<dots>)\<close>
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   122
  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
   123
  and the output of the code, we have to change the ML-expression that is sent 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   124
  to the compiler: in \<open>ML_checked2\<close> we sent the expression @{text [quotes]
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   125
  "val _ = a_piece_of_code"} to the compiler; now the wildcard \<open>_\<close>
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   126
  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
   127
  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
   128
  function will do this:
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   129
\<close>
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   131
ML%linenosgray\<open>fun ml_pat pat code =
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   132
  ML_Lex.read "val" @ ML_Lex.read_source false pat @ ML_Lex.read " = " @ ML_Lex.read_source false code\<close>
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   133
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   134
(*
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   135
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
   136
let val pat' = 
59
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
   137
         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
   138
in 
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   139
  ml_enclose ("val " ^ pat' ^ " = ") "" code_txt 
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   140
end*}
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   141
*)
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   142
text \<open>
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   143
  Next we add a response indicator to the result using:
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   144
\<close>
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   145
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   146
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   147
ML %grayML\<open>fun add_resp pat = map (fn s => "> " ^ s) pat\<close>
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   149
text \<open>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   150
  The rest of the code of \<open>ML_resp\<close> is: 
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   151
\<close>
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   152
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   153
ML %linenosgray\<open>
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   154
fun output_ml_resp ctxt (code_txt, pat) =
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   155
let
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   156
  val _ = ML_Context.eval_in (SOME ctxt) ML_Compiler.flags (Input.pos_of code_txt) (ml_pat pat code_txt)
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   157
  val code = space_explode "\n" (Input.source_content code_txt)
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   158
  val resp = add_resp (space_explode "\n" (Input.source_content pat))
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   159
in 
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   160
   Pretty.str (cat_lines (code @ resp))
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   161
end
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   162
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   163
val ml_response_setup = Thy_Output.antiquotation_pretty_source @{binding "ML_resp"} (Scan.lift (Args.text_input -- Args.text_input)) output_ml_resp
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   164
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   165
\<close>
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   166
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   167
(*
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   168
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
   169
  (let
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
   170
     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
   171
   in
555
2c34c69236ce updated to new isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 553
diff changeset
   172
     ML_Context.eval_source_in (SOME ctxt) ML_Compiler.flags srcpos 
553
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
   173
   end;
59
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
   174
   let 
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   175
     val code_output = space_explode "\n" code_txt 
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   176
     val resp_output = add_resp (space_explode "\n" pat)
59
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
   177
   in 
449
f952f2679a11 updated to new isabelle
Christian Urban <urbanc@in.tum.de>
parents: 438
diff changeset
   178
     Thy_Output.output ctxt (map Pretty.str (code_output @ resp_output)) 
59
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
   179
   end)
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
553
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 517
diff changeset
   181
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
   182
val ml_resp_setup = Thy_Output.antiquotation @{binding "ML_resp"} 
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   183
          (Scan.lift (Parse.position (Args.text_input -- Args.text_input))) 
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   184
             output_ml_resp*}
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   185
*)
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   186
setup \<open>ml_response_setup\<close>
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   188
(* FIXME *)
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   189
text \<open>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   190
  In comparison with \<open>ML_checked\<close>, we only changed the line about 
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   191
  the compiler (Line~2), the lines about
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   192
  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
   193
  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
   194
 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 565
diff changeset
   195
  @{text [display] \<open>@{ML_resp [display] "true andalso false" "false"}\<close>}
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   196
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   197
  to obtain
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   199
  @{ML_resp [display] "true andalso false" "false"} 
46
81e2d73f7191 added a section about document antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   201
  or 
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   202
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 565
diff changeset
   203
  @{text [display] \<open>@{ML_resp [display] "let val i = 3 in (i * i, "foo") end" "(9, \<dots>)"}\<close>}
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   204
  
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   205
  to obtain
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   206
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 555
diff changeset
   207
  @{ML_resp [display] "let val i = 3 in (i * i, \"foo\") end" "(9, _)"} 
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   208
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   209
  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
   210
  match. A limitation of this document antiquotation, however, is that the
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   211
  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
   212
  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
   213
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   214
\<close>
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
   215
end