CookBook/FirstSteps.thy
author Christian Urban <urbanc@in.tum.de>
Wed, 04 Mar 2009 13:15:29 +0000
changeset 157 76cdc8f562fc
parent 156 e8f11280c762
child 158 d7944bdf7b3f
permissions -rw-r--r--
added more to the simplifier section
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
theory FirstSteps
25
e2f9f94b26d4 Antiquotation setup is now contained in theory Base.
berghofe
parents: 21
diff changeset
     2
imports Base
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
begin
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
chapter {* First Steps *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
42
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
     7
text {*
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
     8
54
1783211b3494 tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents: 52
diff changeset
     9
  Isabelle programming is done in ML.  Just like lemmas and proofs, ML-code
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
    10
  in Isabelle is part of a theory. If you want to follow the code given in
54
1783211b3494 tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents: 52
diff changeset
    11
  this chapter, we assume you are working inside the theory starting with
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
6
007e09485351 some tuning
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    13
  \begin{center}
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    14
  \begin{tabular}{@ {}l}
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    15
  \isacommand{theory} FirstSteps\\
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    16
  \isacommand{imports} Main\\
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    17
  \isacommand{begin}\\
6
007e09485351 some tuning
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    18
  \ldots
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    19
  \end{tabular}
6
007e09485351 some tuning
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    20
  \end{center}
157
76cdc8f562fc added more to the simplifier section
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
    21
76cdc8f562fc added more to the simplifier section
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
    22
  We also generally assume you are working with HOL. The given examples might
76cdc8f562fc added more to the simplifier section
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
    23
  need to be adapted slightly if you work in a different logic.
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    24
*}
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    25
20
5ae6a1bb91c9 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    26
section {* Including ML-Code *}
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    27
47
4daf913fdbe1 hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents: 43
diff changeset
    28
4daf913fdbe1 hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents: 43
diff changeset
    29
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    30
text {*
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    31
  The easiest and quickest way to include code in a theory is
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
    32
  by using the \isacommand{ML}-command. For example:
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
    34
\begin{isabelle}
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
    35
\begin{graybox}
85
b02904872d6b better handling of {* and *}
Christian Urban <urbanc@in.tum.de>
parents: 84
diff changeset
    36
\isacommand{ML}~@{text "\<verbopen>"}\isanewline
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    37
\hspace{5mm}@{ML "3 + 4"}\isanewline
85
b02904872d6b better handling of {* and *}
Christian Urban <urbanc@in.tum.de>
parents: 84
diff changeset
    38
@{text "\<verbclose>"}\isanewline
b02904872d6b better handling of {* and *}
Christian Urban <urbanc@in.tum.de>
parents: 84
diff changeset
    39
@{text "> 7"}\smallskip
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
    40
\end{graybox}
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
    41
\end{isabelle}
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
102
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
    43
  Like normal Isabelle proof scripts, \isacommand{ML}-commands can be
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
    44
  evaluated by using the advance and undo buttons of your Isabelle
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
    45
  environment. The code inside the \isacommand{ML}-command can also contain
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
    46
  value and function bindings, and even those can be undone when the proof
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
    47
  script is retracted. As mentioned earlier, we will drop the
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
    48
  \isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"} scaffolding whenever we
107
258ce361ba1b polished and more material in the tactic chapter
Christian Urban <urbanc@in.tum.de>
parents: 104
diff changeset
    49
  show code. The lines prefixed with @{text [quotes] ">"} are not part of the
102
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
    50
  code, rather they indicate what the response is when the code is evaluated.
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    51
102
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
    52
  Once a portion of code is relatively stable, you usually want to export it
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
    53
  to a separate ML-file. Such files can then be included in a theory by using
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
    54
  the \isacommand{uses}-command in the header of the theory, like:
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    55
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    56
  \begin{center}
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    57
  \begin{tabular}{@ {}l}
54
1783211b3494 tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents: 52
diff changeset
    58
  \isacommand{theory} FirstSteps\\
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    59
  \isacommand{imports} Main\\
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
    60
  \isacommand{uses} @{text "\"file_to_be_included.ML\""} @{text "\<dots>"}\\
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    61
  \isacommand{begin}\\
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    62
  \ldots
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    63
  \end{tabular}
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    64
  \end{center}
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    65
  
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    66
*}
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    67
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    68
section {* Debugging and Printing\label{sec:printing} *}
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    69
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    70
text {*
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
    71
50
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
    72
  During development you might find it necessary to inspect some data
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    73
  in your code. This can be done in a ``quick-and-dirty'' fashion using 
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    74
  the function @{ML "warning"}. For example 
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    75
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
    76
  @{ML_response_fake [display,gray] "warning \"any string\"" "\"any string\""}
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    77
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
    78
  will print out @{text [quotes] "any string"} inside the response buffer
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
    79
  of Isabelle.  This function expects a string as argument. If you develop under PolyML,
50
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
    80
  then there is a convenient, though again ``quick-and-dirty'', method for
107
258ce361ba1b polished and more material in the tactic chapter
Christian Urban <urbanc@in.tum.de>
parents: 104
diff changeset
    81
  converting values into strings, namely the function @{ML makestring}:
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    82
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
    83
  @{ML_response_fake [display,gray] "warning (makestring 1)" "\"1\""} 
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    84
65
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    85
  However @{ML makestring} only works if the type of what is converted is monomorphic 
78
ef778679d3e0 added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
    86
  and not a function.
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    87
52
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    88
  The function @{ML "warning"} should only be used for testing purposes, because any
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    89
  output this function generates will be overwritten as soon as an error is
50
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
    90
  raised. For printing anything more serious and elaborate, the
54
1783211b3494 tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents: 52
diff changeset
    91
  function @{ML tracing} is more appropriate. This function writes all output into
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
    92
  a separate tracing buffer. For example:
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    93
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
    94
  @{ML_response_fake [display,gray] "tracing \"foo\"" "\"foo\""}
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    95
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
    96
  It is also possible to redirect the ``channel'' where the string @{text "foo"} is 
78
ef778679d3e0 added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
    97
  printed to a separate file, e.g.~to prevent ProofGeneral from choking on massive 
107
258ce361ba1b polished and more material in the tactic chapter
Christian Urban <urbanc@in.tum.de>
parents: 104
diff changeset
    98
  amounts of trace output. This redirection can be achieved with the code:
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    99
*}
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   100
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   101
ML{*val strip_specials =
42
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   102
let
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   103
  fun strip ("\^A" :: _ :: cs) = strip cs
42
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   104
    | strip (c :: cs) = c :: strip cs
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   105
    | strip [] = [];
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   106
in implode o strip o explode end;
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   107
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   108
fun redirect_tracing stream =
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   109
 Output.tracing_fn := (fn s =>
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   110
    (TextIO.output (stream, (strip_specials s));
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   111
     TextIO.output (stream, "\n");
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   112
     TextIO.flushOut stream)) *}
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   113
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   114
text {*
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   115
  Calling @{ML "redirect_tracing"} with @{ML "(TextIO.openOut \"foo.bar\")"} 
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   116
  will cause that all tracing information is printed into the file @{text "foo.bar"}.
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   117
107
258ce361ba1b polished and more material in the tactic chapter
Christian Urban <urbanc@in.tum.de>
parents: 104
diff changeset
   118
  You can print out error messages with the function @{ML error}; for example:
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   119
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   120
@{ML_response_fake [display,gray] "if 0=1 then true else (error \"foo\")" 
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   121
"Exception- ERROR \"foo\" raised
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   122
At command \"ML\"."}
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   123
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   124
  Most often you want to inspect data of type @{ML_type term}, @{ML_type cterm} 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   125
  or @{ML_type thm}. Isabelle contains elaborate pretty-printing functions for printing them, 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   126
  but  for quick-and-dirty solutions they are far too unwieldy. A simple way to transform 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   127
  a term into a string is to use the function @{ML Syntax.string_of_term}.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   128
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   129
  @{ML_response_fake [display,gray]
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   130
  "Syntax.string_of_term @{context} @{term \"1::nat\"}"
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   131
  "\"\\^E\\^Fterm\\^E\\^E\\^Fconst\\^Fname=HOL.one_class.one\\^E1\\^E\\^F\\^E\\^E\\^F\\^E\""}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   132
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   133
  This produces a string with some additional information encoded in it. The string
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   134
  can be properly printed by using the function @{ML warning}.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   135
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   136
  @{ML_response_fake [display,gray]
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   137
  "warning (Syntax.string_of_term @{context} @{term \"1::nat\"})"
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   138
  "\"1\""}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   139
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   140
  A @{ML_type cterm} can be transformed into a string by the following function.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   141
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   142
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   143
ML{*fun str_of_cterm ctxt t =  
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   144
   Syntax.string_of_term ctxt (term_of t)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   145
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   146
text {*
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   147
  In this example the function @{ML term_of} extracts the @{ML_type term} from
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   148
  a @{ML_type cterm}.  If there are more than one @{ML_type cterm}s to be
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   149
  printed, you can use the function @{ML commas} to separate them.
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   150
*} 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   151
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   152
ML{*fun str_of_cterms ctxt ts =  
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   153
   commas (map (str_of_cterm ctxt) ts)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   154
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   155
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   156
  The easiest way to get the string of a theorem is to transform it
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   157
  into a @{ML_type cterm} using the function @{ML crep_thm}.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   158
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   159
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   160
ML{*fun str_of_thm ctxt thm =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   161
  let
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   162
    val {prop, ...} = crep_thm thm
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   163
  in 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   164
    str_of_cterm ctxt prop
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   165
  end*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   166
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   167
text {* 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   168
  Again the function @{ML commas} helps with printing more than one theorem. 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   169
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   170
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   171
ML{*fun str_of_thms ctxt thms =  
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   172
  commas (map (str_of_thm ctxt) thms)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   173
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   174
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   175
section {* Combinators\label{sec:combinators} *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   176
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   177
text {*
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   178
  For beginners perhaps the most puzzling parts in the existing code of Isabelle are
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   179
  the combinators. At first they seem to greatly obstruct the
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   180
  comprehension of the code, but after getting familiar with them, they
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   181
  actually ease the understanding and also the programming.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   182
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   183
  The simplest combinator is @{ML I}, which is just the identity function defined as
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   184
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   185
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   186
ML{*fun I x = x*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   187
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   188
text {* Another simple combinator is @{ML K}, defined as *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   189
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   190
ML{*fun K x = fn _ => x*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   191
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   192
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   193
  @{ML K} ``wraps'' a function around the argument @{text "x"}. However, this 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   194
  function ignores its argument. As a result, @{ML K} defines a constant function 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   195
  always returning @{text x}.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   196
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   197
  The next combinator is reverse application, @{ML "|>"}, defined as: 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   198
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   199
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   200
ML{*fun x |> f = f x*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   201
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   202
text {* While just syntactic sugar for the usual function application,
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   203
  the purpose of this combinator is to implement functions in a  
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   204
  ``waterfall fashion''. Consider for example the function *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   205
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   206
ML %linenosgray{*fun inc_by_five x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   207
  x |> (fn x => x + 1)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   208
    |> (fn x => (x, x))
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   209
    |> fst
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   210
    |> (fn x => x + 4)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   211
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   212
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   213
  which increments its argument @{text x} by 5. It does this by first incrementing 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   214
  the argument by 1 (Line 2); then storing the result in a pair (Line 3); taking 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   215
  the first component of the pair (Line 4) and finally incrementing the first 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   216
  component by 4 (Line 5). This kind of cascading manipulations of values is quite
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   217
  common when dealing with theories (for example by adding a definition, followed by
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   218
  lemmas and so on). The reverse application allows you to read what happens in 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   219
  a top-down manner. This kind of coding should also be familiar, 
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   220
  if you have been exposed to Haskell's do-notation. Writing the function @{ML inc_by_five} using 
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   221
  the reverse application is much clearer than writing
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   222
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   223
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   224
ML{*fun inc_by_five x = fst ((fn x => (x, x)) (x + 1)) + 4*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   225
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   226
text {* or *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   227
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   228
ML{*fun inc_by_five x = 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   229
       ((fn x => x + 4) o fst o (fn x => (x, x)) o (fn x => x + 1)) x*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   230
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   231
text {* and typographically more economical than *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   232
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   233
ML{*fun inc_by_five x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   234
   let val y1 = x + 1
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   235
       val y2 = (y1, y1)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   236
       val y3 = fst y2
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   237
       val y4 = y3 + 4
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   238
   in y4 end*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   239
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   240
text {* 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   241
  Another reason why the let-bindings in the code above are better to be
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   242
  avoided: it is more than easy to get the intermediate values wrong, not to 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   243
  mention the nightmares the maintenance of this code causes!
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   244
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   245
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   246
  (FIXME: give a real world example involving theories)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   247
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   248
  Similarly, the combinator @{ML "#>"} is the reverse function 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   249
  composition. It can be used to define the following function
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   250
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   251
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   252
ML{*val inc_by_six = 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   253
      (fn x => x + 1)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   254
   #> (fn x => x + 2)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   255
   #> (fn x => x + 3)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   256
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   257
text {* 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   258
  which is the function composed of first the increment-by-one function and then
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   259
  increment-by-two, followed by increment-by-three. Again, the reverse function 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   260
  composition allows you to read the code top-down.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   261
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   262
  The remaining combinators described in this section add convenience for the
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   263
  ``waterfall method'' of writing functions. The combinator @{ML tap} allows
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   264
  you to get hold of an intermediate result (to do some side-calculations for
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   265
  instance). The function
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   266
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   267
 *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   268
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   269
ML %linenosgray{*fun inc_by_three x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   270
     x |> (fn x => x + 1)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   271
       |> tap (fn x => tracing (makestring x))
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   272
       |> (fn x => x + 2)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   273
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   274
text {* 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   275
  increments the argument first by @{text "1"} and then by @{text "2"}. In the
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   276
  middle (Line 3), however, it uses @{ML tap} for printing the ``plus-one''
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   277
  intermediate result inside the tracing buffer. The function @{ML tap} can
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   278
  only be used for side-calculations, because any value that is computed
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   279
  cannot be merged back into the ``main waterfall''. To do this, you can use
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   280
  the next combinator.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   281
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   282
  The combinator @{ML "`"} is similar to @{ML tap}, but applies a function to the value
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   283
  and returns the result together with the value (as a pair). For example
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   284
  the function 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   285
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   286
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   287
ML{*fun inc_as_pair x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   288
     x |> `(fn x => x + 1)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   289
       |> (fn (x, y) => (x, y + 1))*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   290
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   291
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   292
  takes @{text x} as argument, and then increments @{text x}, but also keeps
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   293
  @{text x}. The intermediate result is therefore the pair @{ML "(x + 1, x)"
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   294
  for x}. After that, the function increments the right-hand component of the
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   295
  pair. So finally the result will be @{ML "(x + 1, x + 1)" for x}.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   296
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   297
  The combinators @{ML "|>>"} and @{ML "||>"} are defined for 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   298
  functions manipulating pairs. The first applies the function to
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   299
  the first component of the pair, defined as
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   300
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   301
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   302
ML{*fun (x, y) |>> f = (f x, y)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   303
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   304
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   305
  and the second combinator to the second component, defined as
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   306
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   307
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   308
ML{*fun (x, y) ||> f = (x, f y)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   309
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   310
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   311
  With the combinator @{ML "|->"} you can re-combine the elements from a pair.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   312
  This combinator is defined as
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   313
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   314
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   315
ML{*fun (x, y) |-> f = f x y*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   316
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   317
text {* and can be used to write the following roundabout version 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   318
  of the @{text double} function: 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   319
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   320
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   321
ML{*fun double x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   322
      x |>  (fn x => (x, x))
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   323
        |-> (fn x => fn y => x + y)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   324
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   325
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   326
  Recall that @{ML "|>"} is the reverse function applications. Recall also that the related 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   327
  reverse function composition is @{ML "#>"}. In fact all the combinators @{ML "|->"},
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   328
  @{ML "|>>"} and @{ML "||>"} described above have related combinators for function
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   329
  composition, namely @{ML "#->"}, @{ML "#>>"} and @{ML "##>"}. Using @{ML "#->"}, 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   330
  for example, the function @{text double} can also be written as:
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   331
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   332
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   333
ML{*val double =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   334
            (fn x => (x, x))
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   335
        #-> (fn x => fn y => x + y)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   336
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   337
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   338
  
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   339
  (FIXME: find a good exercise for combinators)
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   340
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   341
  \begin{readmore}
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   342
  The most frequently used combinator are defined in the files @{ML_file "Pure/library.ML"}
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   343
  and @{ML_file "Pure/General/basics.ML"}. Also \isccite{sec:ML-linear-trans} 
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   344
  contains further information about combinators.
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   345
  \end{readmore}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   346
 
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   347
*}
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   348
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   349
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   350
section {* Antiquotations *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   351
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   352
text {*
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   353
  The main advantage of embedding all code in a theory is that the code can
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   354
  contain references to entities defined on the logical level of Isabelle. By
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   355
  this we mean definitions, theorems, terms and so on. This kind of reference is
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   356
  realised with antiquotations.  For example, one can print out the name of the current
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   357
  theory by typing
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   358
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   359
  
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   360
  @{ML_response [display,gray] "Context.theory_name @{theory}" "\"FirstSteps\""}
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   361
 
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   362
  where @{text "@{theory}"} is an antiquotation that is substituted with the
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   363
  current theory (remember that we assumed we are inside the theory 
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   364
  @{text FirstSteps}). The name of this theory can be extracted using
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   365
  the function @{ML "Context.theory_name"}. 
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   366
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   367
  Note, however, that antiquotations are statically linked, that is their value is
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   368
  determined at ``compile-time'', not ``run-time''. For example the function
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   369
*}
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   370
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   371
ML{*fun not_current_thyname () = Context.theory_name @{theory} *}
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   372
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   373
text {*
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   374
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   375
  does \emph{not} return the name of the current theory, if it is run in a 
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   376
  different theory. Instead, the code above defines the constant function 
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   377
  that always returns the string @{text [quotes] "FirstSteps"}, no matter where the
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   378
  function is called. Operationally speaking,  the antiquotation @{text "@{theory}"} is 
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   379
  \emph{not} replaced with code that will look up the current theory in 
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   380
  some data structure and return it. Instead, it is literally
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   381
  replaced with the value representing the theory name.
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   382
132
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   383
  In a similar way you can use antiquotations to refer to proved theorems: 
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   384
  @{text "@{thm \<dots>}"} for a single theorem
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   385
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   386
  @{ML_response_fake [display,gray] "@{thm allI}" "(\<And>x. ?P x) \<Longrightarrow> \<forall>x. ?P x"}
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   387
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   388
  and @{text "@{thms \<dots>}"} for more than one
132
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   389
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   390
@{ML_response_fake [display,gray] "@{thms conj_ac}" 
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   391
"(?P \<and> ?Q) = (?Q \<and> ?P)
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   392
(?P \<and> ?Q \<and> ?R) = (?Q \<and> ?P \<and> ?R)
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   393
((?P \<and> ?Q) \<and> ?R) = (?P \<and> ?Q \<and> ?R)"}  
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   394
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   395
  You can also refer to the current simpset. To illustrate this we implement the
132
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   396
  function that extracts the theorem names stored in a simpset.
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   397
*}
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   398
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   399
ML{*fun get_thm_names_from_ss simpset =
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   400
let
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   401
  val ({rules,...}, _) = MetaSimplifier.rep_ss simpset
70
bbb2d5f1d58d deleted the fixme about simpsets
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   402
in
bbb2d5f1d58d deleted the fixme about simpsets
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   403
  map #name (Net.entries rules)
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   404
end*}
54
1783211b3494 tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents: 52
diff changeset
   405
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   406
text {*
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   407
  The function @{ML rep_ss in MetaSimplifier} returns a record containing all
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   408
  information about the simpset. The rules of a simpset are stored in a
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   409
  \emph{discrimination net} (a data structure for fast indexing). From this
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   410
  net you can extract the entries using the function @{ML Net.entries}.
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   411
  You can now use @{ML get_thm_names_from_ss} to obtain all names of theorems stored
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   412
  in the current simpset. This simpset can be referred to using the antiquotation
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   413
  @{text "@{simpset}"}.
81
8fda6b452f28 polished
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   414
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   415
  @{ML_response_fake [display,gray] 
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   416
  "get_thm_names_from_ss @{simpset}" 
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   417
  "[\"Nat.of_nat_eq_id\", \"Int.of_int_eq_id\", \"Nat.One_nat_def\", \<dots>]"}
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   418
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   419
  Again, this way or referencing simpsets makes you independent from additions
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   420
  of lemmas to the simpset by the user that potentially cause loops.
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   421
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   422
  \begin{readmore}
81
8fda6b452f28 polished
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   423
  The infrastructure for simpsets is implemented in @{ML_file "Pure/meta_simplifier.ML"}
78
ef778679d3e0 added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   424
  and @{ML_file "Pure/simplifier.ML"}. Discrimination nets are implemented
ef778679d3e0 added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   425
  in @{ML_file "Pure/net.ML"}.
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   426
  \end{readmore}
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   427
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   428
  While antiquotations have many applications, they were originally introduced in order 
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   429
  to avoid explicit bindings for theorems such as:
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   430
*}
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   431
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   432
ML{*val allI = thm "allI" *}
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   433
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   434
text {*
78
ef778679d3e0 added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   435
  These bindings are difficult to maintain and also can be accidentally
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   436
  overwritten by the user. This often broke Isabelle
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   437
  packages. Antiquotations solve this problem, since they are ``linked''
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   438
  statically at compile-time.  However, this static linkage also limits their
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   439
  usefulness in cases where data needs to be build up dynamically. In the
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   440
  course of this chapter you will learn more about these antiquotations:
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   441
  they can simplify Isabelle programming since one can directly access all
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   442
  kinds of logical elements from th ML-level.
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   443
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   444
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   445
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   446
section {* Terms and Types *}
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   447
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   448
text {*
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   449
  One way to construct terms of Isabelle is by using the antiquotation 
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   450
  \mbox{@{text "@{term \<dots>}"}}. For example:
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   451
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   452
  @{ML_response [display,gray] 
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   453
"@{term \"(a::nat) + b = c\"}" 
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   454
"Const (\"op =\", \<dots>) $ 
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   455
  (Const (\"HOL.plus_class.plus\", \<dots>) $ \<dots> $ \<dots>) $ \<dots>"}
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   456
50
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
   457
  This will show the term @{term "(a::nat) + b = c"}, but printed using the internal
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   458
  representation of this term. This internal representation corresponds to the 
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   459
  datatype @{ML_type "term"}.
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   460
  
34
527fc0af45e3 Corrected typos.
berghofe
parents: 25
diff changeset
   461
  The internal representation of terms uses the usual de Bruijn index mechanism where bound 
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   462
  variables are represented by the constructor @{ML Bound}. The index in @{ML Bound} refers to
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   463
  the number of Abstractions (@{ML Abs}) we have to skip until we hit the @{ML Abs} that
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   464
  binds the corresponding variable. However, in Isabelle the names of bound variables are 
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   465
  kept at abstractions for printing purposes, and so should be treated only as ``comments''. 
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   466
  Application in Isabelle is realised with the term-constructor @{ML $}.
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   467
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   468
  \begin{readmore}
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   469
  Terms are described in detail in \isccite{sec:terms}. Their
78
ef778679d3e0 added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   470
  definition and many useful operations are implemented in @{ML_file "Pure/term.ML"}.
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   471
  \end{readmore}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   472
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   473
  Sometimes the internal representation of terms can be surprisingly different
157
76cdc8f562fc added more to the simplifier section
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
   474
  from what you see at the user-level, because the layers of
47
4daf913fdbe1 hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents: 43
diff changeset
   475
  parsing/type-checking/pretty printing can be quite elaborate. 
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   476
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   477
  \begin{exercise}
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   478
  Look at the internal term representation of the following terms, and
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   479
  find out why they are represented like this:
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   480
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   481
  \begin{itemize}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   482
  \item @{term "case x of 0 \<Rightarrow> 0 | Suc y \<Rightarrow> y"}  
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   483
  \item @{term "\<lambda>(x,y). P y x"}  
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   484
  \item @{term "{ [x::int] | x. x \<le> -2 }"}  
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   485
  \end{itemize}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   486
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   487
  Hint: The third term is already quite big, and the pretty printer
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   488
  may omit parts of it by default. If you want to see all of it, you
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   489
  can use the following ML-function to set the printing depth to a higher 
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   490
  value:
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   491
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   492
  @{ML [display,gray] "print_depth 50"}
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   493
  \end{exercise}
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   494
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   495
  The antiquotation @{text "@{prop \<dots>}"} constructs terms of propositional type, 
50
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
   496
  inserting the invisible @{text "Trueprop"}-coercions whenever necessary. 
68
e7519207c2b7 added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
   497
  Consider for example the pairs
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   498
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   499
@{ML_response [display,gray] "(@{term \"P x\"}, @{prop \"P x\"})" 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   500
"(Free (\"P\", \<dots>) $ Free (\"x\", \<dots>),
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   501
 Const (\"Trueprop\", \<dots>) $ (Free (\"P\", \<dots>) $ Free (\"x\", \<dots>)))"}
65
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   502
 
108
8bea3f74889d added to the tactical chapter; polished; added the tabularstar environment (which is just tabular*)
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   503
  where a coercion is inserted in the second component and 
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   504
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   505
  @{ML_response [display,gray] "(@{term \"P x \<Longrightarrow> Q x\"}, @{prop \"P x \<Longrightarrow> Q x\"})" 
65
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   506
  "(Const (\"==>\", \<dots>) $ \<dots> $ \<dots>, Const (\"==>\", \<dots>) $ \<dots> $ \<dots>)"}
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   507
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   508
  where it is not (since it is already constructed by a meta-implication). 
19
34b93dbf8c3c some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
   509
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   510
  Types can be constructed using the antiquotation @{text "@{typ \<dots>}"}. For example:
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   511
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   512
  @{ML_response_fake [display,gray] "@{typ \"bool \<Rightarrow> nat\"}" "bool \<Rightarrow> nat"}
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   513
19
34b93dbf8c3c some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
   514
  \begin{readmore}
34b93dbf8c3c some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
   515
  Types are described in detail in \isccite{sec:types}. Their
78
ef778679d3e0 added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   516
  definition and many useful operations are implemented 
ef778679d3e0 added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   517
  in @{ML_file "Pure/type.ML"}.
19
34b93dbf8c3c some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
   518
  \end{readmore}
47
4daf913fdbe1 hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents: 43
diff changeset
   519
*}
19
34b93dbf8c3c some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
   520
34b93dbf8c3c some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
   521
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   522
section {* Constructing Terms and Types Manually\label{sec:terms_types_manually} *} 
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   523
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   524
text {*
81
8fda6b452f28 polished
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   525
  While antiquotations are very convenient for constructing terms, they can
102
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
   526
  only construct fixed terms (remember they are ``linked'' at compile-time).
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
   527
  However, you often need to construct terms dynamically. For example, a
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
   528
  function that returns the implication @{text "\<And>(x::\<tau>). P x \<Longrightarrow> Q x"} taking
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
   529
  @{term P}, @{term Q} and the type @{term "\<tau>"} as arguments can only be
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
   530
  written as:
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
   531
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   532
*}
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   533
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   534
ML{*fun make_imp P Q tau =
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   535
let
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   536
  val x = Free ("x",tau)
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   537
in 
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   538
  Logic.all x (Logic.mk_implies (P $ x, Q $ x))
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   539
end *}
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   540
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   541
text {*
102
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
   542
  The reason is that you cannot pass the arguments @{term P}, @{term Q} and 
104
5dcad9348e4d polished
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
   543
  @{term "tau"} into an antiquotation. For example the following does \emph{not} work.
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   544
*}
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   545
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   546
ML{*fun make_wrong_imp P Q tau = @{prop "\<And>x. P x \<Longrightarrow> Q x"} *}
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   547
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   548
text {*
19
34b93dbf8c3c some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
   549
  To see this apply @{text "@{term S}"}, @{text "@{term T}"} and @{text "@{typ nat}"} 
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   550
  to both functions. With @{ML make_imp} we obtain the intended term involving 
102
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
   551
  the given arguments
65
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   552
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   553
  @{ML_response [display,gray] "make_imp @{term S} @{term T} @{typ nat}" 
65
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   554
    "Const \<dots> $ 
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   555
    Abs (\"x\", Type (\"nat\",[]),
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   556
      Const \<dots> $ (Free (\"S\",\<dots>) $ \<dots>) $ 
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   557
                  (Free (\"T\",\<dots>) $ \<dots>))"}
68
e7519207c2b7 added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
   558
81
8fda6b452f28 polished
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   559
  whereas with @{ML make_wrong_imp} we obtain a term involving the @{term "P"} 
68
e7519207c2b7 added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
   560
  and @{text "Q"} from the antiquotation.
e7519207c2b7 added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
   561
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   562
  @{ML_response [display,gray] "make_wrong_imp @{term S} @{term T} @{typ nat}" 
65
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   563
    "Const \<dots> $ 
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   564
    Abs (\"x\", \<dots>,
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   565
      Const \<dots> $ (Const \<dots> $ (Free (\"P\",\<dots>) $ \<dots>)) $ 
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   566
                  (Const \<dots> $ (Free (\"Q\",\<dots>) $ \<dots>)))"}
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   567
108
8bea3f74889d added to the tactical chapter; polished; added the tabularstar environment (which is just tabular*)
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   568
  Although types of terms can often be inferred, there are many
104
5dcad9348e4d polished
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
   569
  situations where you need to construct types manually, especially  
5dcad9348e4d polished
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
   570
  when defining constants. For example the function returning a function 
5dcad9348e4d polished
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
   571
  type is as follows:
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   572
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   573
*} 
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   574
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   575
ML{*fun make_fun_type tau1 tau2 = Type ("fun",[tau1,tau2]) *}
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   576
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   577
text {* This can be equally written with the combinator @{ML "-->"} as: *}
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   578
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   579
ML{*fun make_fun_type tau1 tau2 = tau1 --> tau2 *}
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   580
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   581
text {*
134
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   582
  A handy function for manipulating terms is @{ML map_types}: it takes a 
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   583
  function and applies it to every type in a term. You can, for example,
134
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   584
  change every @{typ nat} in a term into an @{typ int} using the function:
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   585
*}
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   586
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   587
ML{*fun nat_to_int t =
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   588
  (case t of
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   589
     @{typ nat} => @{typ int}
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   590
   | Type (s, ts) => Type (s, map nat_to_int ts)
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   591
   | _ => t)*}
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   592
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   593
text {*
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   594
  An example as follows:
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   595
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   596
@{ML_response_fake [display,gray] 
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   597
"map_types nat_to_int @{term \"a = (1::nat)\"}" 
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   598
"Const (\"op =\", \"int \<Rightarrow> int \<Rightarrow> bool\")
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   599
           $ Free (\"a\", \"int\") $ Const (\"HOL.one_class.one\", \"int\")"}
328370b75c33 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 133
diff changeset
   600
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   601
  \begin{readmore}
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   602
  There are many functions in @{ML_file "Pure/term.ML"}, @{ML_file "Pure/logic.ML"} and
102
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
   603
  @{ML_file "HOL/Tools/hologic.ML"} that make such manual constructions of terms 
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   604
  and types easier.\end{readmore}
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   605
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   606
  Have a look at these files and try to solve the following two exercises:
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   607
*}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   608
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   609
text {*
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   610
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   611
  \begin{exercise}\label{fun:revsum}
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   612
  Write a function @{text "rev_sum : term -> term"} that takes a
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   613
  term of the form @{text "t\<^isub>1 + t\<^isub>2 + \<dots> + t\<^isub>n"} (whereby @{text "n"} might be zero)
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   614
  and returns the reversed sum @{text "t\<^isub>n + \<dots> + t\<^isub>2 + t\<^isub>1"}. Assume
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   615
  the @{text "t\<^isub>i"} can be arbitrary expressions and also note that @{text "+"} 
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   616
  associates to the left. Try your function on some examples. 
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   617
  \end{exercise}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   618
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   619
  \begin{exercise}\label{fun:makesum}
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   620
  Write a function which takes two terms representing natural numbers
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   621
  in unary notation (like @{term "Suc (Suc (Suc 0))"}), and produce the
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   622
  number representing their sum.
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   623
  \end{exercise}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   624
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   625
  There are a few subtle issues with constants. They usually crop up when
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   626
  pattern matching terms or types, or when constructing them. While it is perfectly ok
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   627
  to write the function @{text is_true} as follows
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   628
*}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   629
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   630
ML{*fun is_true @{term True} = true
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   631
  | is_true _ = false*}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   632
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   633
text {*
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   634
  this does not work for picking out @{text "\<forall>"}-quantified terms. Because
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   635
  the function 
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   636
*}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   637
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   638
ML{*fun is_all (@{term All} $ _) = true
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   639
  | is_all _ = false*}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   640
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   641
text {* 
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   642
  will not correctly match the formula @{prop "\<forall>x::nat. P x"}: 
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   643
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   644
  @{ML_response [display,gray] "is_all @{term \"\<forall>x::nat. P x\"}" "false"}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   645
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   646
  The problem is that the @{text "@term"}-antiquotation in the pattern 
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   647
  fixes the type of the constant @{term "All"} to be @{typ "('a \<Rightarrow> bool) \<Rightarrow> bool"} for 
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   648
  an arbitrary, but fixed type @{typ "'a"}. A properly working alternative 
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   649
  for this function is
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   650
*}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   651
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   652
ML{*fun is_all (Const ("All", _) $ _) = true
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   653
  | is_all _ = false*}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   654
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   655
text {* 
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   656
  because now
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   657
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   658
@{ML_response [display,gray] "is_all @{term \"\<forall>x::nat. P x\"}" "true"}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   659
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   660
  matches correctly (the first wildcard in the pattern matches any type and the
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   661
  second any term).
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   662
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   663
  However there is still a problem: consider the similar function that
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   664
  attempts to pick out @{text "Nil"}-terms:
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   665
*}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   666
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   667
ML{*fun is_nil (Const ("Nil", _)) = true
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   668
  | is_nil _ = false *}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   669
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   670
text {* 
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   671
  Unfortunately, also this function does \emph{not} work as expected, since
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   672
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   673
  @{ML_response [display,gray] "is_nil @{term \"Nil\"}" "false"}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   674
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   675
  The problem is that on the ML-level the name of a constant is more
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   676
  subtle than you might expect. The function @{ML is_all} worked correctly,
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   677
  because @{term "All"} is such a fundamental constant, which can be referenced
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   678
  by @{ML "Const (\"All\", some_type)" for some_type}. However, if you look at
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   679
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   680
  @{ML_response [display,gray] "@{term \"Nil\"}" "Const (\"List.list.Nil\", \<dots>)"}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   681
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   682
  the name of the constant @{text "Nil"} depends on the theory in which the
128
693711a0c702 polished
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   683
  term constructor is defined (@{text "List"}) and also in which datatype
693711a0c702 polished
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   684
  (@{text "list"}). Even worse, some constants have a name involving
693711a0c702 polished
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   685
  type-classes. Consider for example the constants for @{term "zero"} and
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   686
  \mbox{@{text "(op *)"}}:
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   687
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   688
  @{ML_response [display,gray] "(@{term \"0::nat\"}, @{term \"op *\"})" 
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   689
 "(Const (\"HOL.zero_class.zero\", \<dots>), 
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   690
 Const (\"HOL.times_class.times\", \<dots>))"}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   691
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   692
  While you could use the complete name, for example 
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   693
  @{ML "Const (\"List.list.Nil\", some_type)" for some_type}, for referring to or
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   694
  matching against @{text "Nil"}, this would make the code rather brittle. 
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   695
  The reason is that the theory and the name of the datatype can easily change. 
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   696
  To make the code more robust, it is better to use the antiquotation 
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   697
  @{text "@{const_name \<dots>}"}. With this antiquotation you can harness the 
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   698
  variable parts of the constant's name. Therefore a functions for 
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   699
  matching against constants that have a polymorphic type should 
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   700
  be written as follows.
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   701
*}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   702
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   703
ML{*fun is_nil_or_all (Const (@{const_name "Nil"}, _)) = true
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   704
  | is_nil_or_all (Const (@{const_name "All"}, _) $ _) = true
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   705
  | is_nil_or_all _ = false *}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   706
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   707
text {*
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   708
  Occasional you have to calculate what the ``base'' name of a given
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   709
  constant is. For this you can use the function @{ML Sign.extern_const} or
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   710
  @{ML Sign.base_name}. For example:
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   711
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   712
  @{ML_response [display,gray] "Sign.extern_const @{theory} \"List.list.Nil\"" "\"Nil\""}
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   713
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   714
  The difference between both functions is that @{ML extern_const in Sign} returns
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   715
  the smallest name that is still unique, whereas @{ML base_name in Sign} always
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   716
  strips off all qualifiers.
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   717
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   718
  \begin{readmore}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   719
  FIXME
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   720
  \end{readmore}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   721
*}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   722
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   723
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   724
section {* Type-Checking *}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   725
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   726
text {* 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   727
  
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   728
  You can freely construct and manipulate @{ML_type "term"}s and @{ML_type
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   729
  typ}es, since they are just arbitrary unchecked trees. However, you
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   730
  eventually want to see if a term is well-formed, or type-checks, relative to
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   731
  a theory.  Type-checking is done via the function @{ML cterm_of}, which
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   732
  converts a @{ML_type term} into a @{ML_type cterm}, a \emph{certified}
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   733
  term. Unlike @{ML_type term}s, which are just trees, @{ML_type "cterm"}s are
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   734
  abstract objects that are guaranteed to be type-correct, and they can only
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   735
  be constructed via ``official interfaces''.
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   736
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   737
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   738
  Type-checking is always relative to a theory context. For now we use
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   739
  the @{ML "@{theory}"} antiquotation to get hold of the current theory.
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   740
  For example you can write:
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   741
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   742
  @{ML_response_fake [display,gray] "cterm_of @{theory} @{term \"(a::nat) + b = c\"}" "a + b = c"}
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   743
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   744
  This can also be written with an antiquotation:
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   745
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   746
  @{ML_response_fake [display,gray] "@{cterm \"(a::nat) + b = c\"}" "a + b = c"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   747
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   748
  Attempting to obtain the certified term for
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   749
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   750
  @{ML_response_fake_both [display,gray] "@{cterm \"1 + True\"}" "Type unification failed \<dots>"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   751
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   752
  yields an error (since the term is not typable). A slightly more elaborate
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   753
  example that type-checks is:
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   754
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   755
@{ML_response_fake [display,gray] 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   756
"let
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   757
  val natT = @{typ \"nat\"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   758
  val zero = @{term \"0::nat\"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   759
in
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   760
  cterm_of @{theory} 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   761
      (Const (@{const_name plus}, natT --> natT --> natT) $ zero $ zero)
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   762
end" "0 + 0"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   763
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   764
  \begin{exercise}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   765
  Check that the function defined in Exercise~\ref{fun:revsum} returns a
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   766
  result that type-checks.
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   767
  \end{exercise}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   768
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   769
  Remember that in Isabelle a term contains enough typing information
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   770
  (constants, free variables and abstractions all have typing 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   771
  information) so that it is always clear what the type of a term is. 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   772
  Given a well-typed term, the function @{ML type_of} returns the 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   773
  type of a term. Consider for example:
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   774
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   775
  @{ML_response [display,gray] 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   776
  "type_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::nat\"})" "bool"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   777
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   778
  To calculate the type, this function traverses the whole term and will
128
693711a0c702 polished
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   779
  detect any typing inconsistency. For examle changing the type of the variable 
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   780
  @{term "x"} from @{typ "nat"} to @{typ "int"} will result in the error message: 
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   781
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   782
  @{ML_response_fake [display,gray] 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   783
  "type_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::int\"})" 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   784
  "*** Exception- TYPE (\"type_of: type mismatch in application\" \<dots>"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   785
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   786
  Since the complete traversal might sometimes be too costly and
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   787
  not necessary, there is the function @{ML fastype_of}, which 
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   788
  also returns the type of a term.
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   789
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   790
  @{ML_response [display,gray] 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   791
  "fastype_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::nat\"})" "bool"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   792
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   793
  However, efficiency is gained on the expense of skiping some tests. You 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   794
  can see this in the following example
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   795
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   796
   @{ML_response [display,gray] 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   797
  "fastype_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::int\"})" "bool"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   798
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   799
  where no error is detected.
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   800
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   801
  Sometimes it is a bit inconvenient to construct a term with 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   802
  complete typing annotations, especially in cases where the typing 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   803
  information is redundant. A short-cut is to use the ``place-holder'' 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   804
  type @{ML "dummyT"} and then let type-inference figure out the 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   805
  complete type. An example is as follows:
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   806
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   807
  @{ML_response_fake [display,gray] 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   808
"let
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   809
  val c = Const (@{const_name \"plus\"}, dummyT) 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   810
  val o = @{term \"1::nat\"} 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   811
  val v = Free (\"x\", dummyT)
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   812
in   
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   813
  Syntax.check_term @{context} (c $ o $ v)
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   814
end"
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   815
"Const (\"HOL.plus_class.plus\", \"nat \<Rightarrow> nat \<Rightarrow> nat\") $
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   816
  Const (\"HOL.one_class.one\", \"nat\") $ Free (\"x\", \"nat\")"}
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   817
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   818
  Instead of giving explicitly the type for the constant @{text "plus"} and the free 
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   819
  variable @{text "x"}, the type-inference filles in the missing information.
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   820
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   821
  \begin{readmore}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   822
  See @{ML_file "Pure/Syntax/syntax.ML"} where more functions about reading,
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   823
  checking and pretty-printing of terms are defined.
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   824
  \end{readmore}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   825
*}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   826
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   827
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   828
section {* Theorems *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   829
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   830
text {*
50
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
   831
  Just like @{ML_type cterm}s, theorems are abstract objects of type @{ML_type thm} 
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   832
  that can only be build by going through interfaces. As a consequence, every proof 
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   833
  in Isabelle is correct by construction. This follows the tradition of the LCF approach
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   834
  \cite{GordonMilnerWadsworth79}.
107
258ce361ba1b polished and more material in the tactic chapter
Christian Urban <urbanc@in.tum.de>
parents: 104
diff changeset
   835
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   836
78
ef778679d3e0 added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   837
  To see theorems in ``action'', let us give a proof on the ML-level for the following 
ef778679d3e0 added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   838
  statement:
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   839
*}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   840
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   841
  lemma 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   842
   assumes assm\<^isub>1: "\<And>(x::nat). P x \<Longrightarrow> Q x" 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   843
   and     assm\<^isub>2: "P t"
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   844
   shows "Q t" (*<*)oops(*>*) 
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   845
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   846
text {*
78
ef778679d3e0 added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   847
  The corresponding ML-code is as follows:\footnote{Note that @{text "|>"} is reverse
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   848
  application. See Section~\ref{sec:combinators}.}
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   849
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   850
@{ML_response_fake [display,gray]
42
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   851
"let
138
e4d8dfb7e34a included comment from Sascha
Christian Urban <urbanc@in.tum.de>
parents: 136
diff changeset
   852
  val assm1 = @{cprop \"\<And>(x::nat). P x \<Longrightarrow> Q x\"}
e4d8dfb7e34a included comment from Sascha
Christian Urban <urbanc@in.tum.de>
parents: 136
diff changeset
   853
  val assm2 = @{cprop \"(P::nat\<Rightarrow>bool) t\"}
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   854
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   855
  val Pt_implies_Qt = 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   856
        assume assm1
138
e4d8dfb7e34a included comment from Sascha
Christian Urban <urbanc@in.tum.de>
parents: 136
diff changeset
   857
        |> forall_elim @{cterm \"t::nat\"};
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   858
  
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   859
  val Qt = implies_elim Pt_implies_Qt (assume assm2);
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   860
in
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   861
  Qt 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   862
  |> implies_intr assm2
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   863
  |> implies_intr assm1
48
609f9ef73494 fixed FIXME's in fake responses
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
   864
end" "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"}
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   865
21
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
   866
  This code-snippet constructs the following proof:
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
   867
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
   868
  \[
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
   869
  \infer[(@{text "\<Longrightarrow>"}$-$intro)]{\vdash @{prop "(\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> P t \<Longrightarrow> Q t"}}
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
   870
    {\infer[(@{text "\<Longrightarrow>"}$-$intro)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "P t \<Longrightarrow> Q t"}}
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
   871
       {\infer[(@{text "\<Longrightarrow>"}$-$elim)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"}, @{prop "P t"} \vdash @{prop "Q t"}}
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
   872
          {\infer[(@{text "\<And>"}$-$elim)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "P t \<Longrightarrow> Q t"}}
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
   873
                 {\infer[(assume)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "\<And>x. P x \<Longrightarrow> Q x"}}{}}
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
   874
                 &
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
   875
           \infer[(assume)]{@{prop "P t"} \vdash @{prop "P t"}}{}
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
   876
          }
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
   877
       }
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
   878
    }
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
   879
  \]
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
   880
102
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
   881
  However, while we obtained a theorem as result, this theorem is not
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
   882
  yet stored in Isabelle's theorem database. So it cannot be referenced later
128
693711a0c702 polished
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   883
  on. How to store theorems will be explained in Section~\ref{sec:storing}.
21
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
   884
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   885
  \begin{readmore}
50
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
   886
  For the functions @{text "assume"}, @{text "forall_elim"} etc 
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   887
  see \isccite{sec:thms}. The basic functions for theorems are defined in
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   888
  @{ML_file "Pure/thm.ML"}. 
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   889
  \end{readmore}
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   890
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   891
  (FIXME: how to add case-names to goal states - maybe in the next section)
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   892
*}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   893
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   894
section {* Theorem Attributes *}
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   895
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   896
text {*
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   897
  Theorem attributes are @{text "[simp]"}, @{text "[OF \<dots>]"}, @{text
136
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   898
  "[symmetric]"} and so on. Such attributes are \emph{neither} tags \emph{nor} flags
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   899
  annotated to theorems, but functions that do further processing once a
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   900
  theorem is proven. In particular, it is not possible to find out
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   901
  what are all theorems that have a given attribute in common, unless of course
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   902
  the function behind the attribute stores the theorems in a retrivable 
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   903
  datastructure. 
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   904
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   905
  If you want to print out all currently known attributes a theorem 
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   906
  can have, you can use the function:
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   907
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   908
  @{ML_response_fake [display,gray] "Attrib.print_attributes @{theory}" 
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   909
"COMP:  direct composition with rules (no lifting)
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   910
HOL.dest:  declaration of Classical destruction rule
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   911
HOL.elim:  declaration of Classical elimination rule 
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   912
\<dots>"}
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   913
136
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   914
  To explain how to write your own attribute, let us start with an extremely simple 
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   915
  version of the attribute @{text "[symmetric]"}. The purpose of this attribute is
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   916
  to produce the ``symmetric'' version of an equation. The main function behind 
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   917
  this attribute is
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   918
*}
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   919
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   920
ML{*val my_symmetric = Thm.rule_attribute (fn _ => fn thm => thm RS @{thm sym})*}
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   921
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   922
text {* 
136
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   923
  where the function @{ML "Thm.rule_attribute"} expects a function taking a 
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   924
  context (which we ignore in the code above) and a theorem (@{text thm}), and 
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   925
  returns another theorem (namely @{text thm} resolved with the lemma 
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   926
  @{thm [source] sym}: @{thm sym[no_vars]}). The function @{ML "Thm.rule_attribute"} then returns 
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   927
  an attribute.
136
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   928
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   929
  Before we can use the attribute, we need to set it up. This can be done
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   930
  using the function @{ML Attrib.add_attributes} as follows.
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   931
*}
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   932
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   933
setup {*
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   934
  Attrib.add_attributes 
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   935
    [("my_sym", Attrib.no_args my_symmetric, "applying the sym rule")] *}
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   936
136
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   937
text {*
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   938
  The attribute does not expect any further arguments (unlike @{text "[OF
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   939
  \<dots>]"}, for example, which can take a list of theorems as argument). Therefore
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   940
  we use the function @{ML Attrib.no_args}. Later on we will also consider
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   941
  attributes taking further arguments. An example for the attribute @{text
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   942
  "[my_sym]"} is the proof
136
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   943
*} 
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   944
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   945
lemma test[my_sym]: "2 = Suc (Suc 0)" by simp
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   946
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   947
text {*
136
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   948
  which stores the theorem @{thm test} under the name @{thm [source] test}. We 
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   949
  can also use the attribute when referring to this theorem.
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   950
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   951
  \begin{isabelle}
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   952
  \isacommand{thm}~@{text "test[my_sym]"}\\
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   953
  @{text "> "}~@{thm test[my_sym]}
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   954
  \end{isabelle}
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
   955
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   956
  The purpose of @{ML Thm.rule_attribute} is to directly manipulate theorems.
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   957
  Another usage of attributes is to add and delete theorems from stored data.
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   958
  For example the attribute @{text "[simp]"} adds or deletes a theorem from the
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   959
  current simpset. For these applications, you can use @{ML Thm.declaration_attribute}. 
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   960
  To illustrate this function, let us introduce a reference containing a list
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   961
  of theorems.
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   962
*}
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   963
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   964
ML{*val my_thms = ref ([]:thm list)*}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   965
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   966
text {* 
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   967
  A word of warning: such references must not be used in any code that
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   968
  is meant to be more than just for testing purposes! Here it is only used 
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   969
  to illustrate matters. We will show later how to store data properly without 
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   970
  using references. The function @{ML Thm.declaration_attribute} expects us to 
153
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
   971
  provide two functions that add and delete theorems from this list. 
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
   972
  For this we use the two functions:
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   973
*}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   974
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   975
ML{*fun my_thms_add thm ctxt =
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   976
  (my_thms := Thm.add_thm thm (!my_thms); ctxt)
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   977
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   978
fun my_thms_del thm ctxt =
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   979
  (my_thms := Thm.del_thm thm (!my_thms); ctxt)*}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   980
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   981
text {*
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   982
  These functions take a theorem and a context and, for what we are explaining
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   983
  here it is sufficient that they just return the context unchanged. They change
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   984
  however the reference @{ML my_thms}, whereby the function @{ML Thm.add_thm}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   985
  adds a theorem if it is not already included in the list, and @{ML
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   986
  Thm.del_thm} deletes one. Both functions use the predicate @{ML
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   987
  Thm.eq_thm_prop} which compares theorems according to their proved
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   988
  propositions (modulo alpha-equivalence).
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   989
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   990
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   991
  You can turn both functions into attributes using
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   992
*}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   993
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   994
ML{*val my_add = Thm.declaration_attribute my_thms_add
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   995
val my_del = Thm.declaration_attribute my_thms_del *}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   996
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   997
text {* 
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   998
  and set up the attributes as follows
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
   999
*}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1000
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1001
setup {*
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1002
  Attrib.add_attributes 
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1003
    [("my_thms", Attrib.add_del_args my_add my_del, 
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1004
        "maintaining a list of my_thms")] *}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1005
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1006
text {*
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1007
  Now if you prove the lemma attaching the attribute @{text "[my_thms]"}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1008
*}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1009
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1010
lemma trueI_2[my_thms]: "True" by simp
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1011
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1012
text {*
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1013
  then you can see the lemma is added to the initially empty list.
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1014
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1015
  @{ML_response_fake [display,gray]
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1016
  "!my_thms" "[\"True\"]"}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1017
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
  1018
  You can also add theorems using the command \isacommand{declare}
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1019
*}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1020
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1021
declare test[my_thms] trueI_2[my_thms add]
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1022
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1023
text {*
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1024
  The @{text "add"} is the default operation and does not need
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1025
  to be given. This declaration will cause the theorem list to be 
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1026
  updated as follows.
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1027
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1028
  @{ML_response_fake [display,gray]
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1029
  "!my_thms"
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1030
  "[\"True\", \"Suc (Suc 0) = 2\"]"}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1031
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1032
  The theorem @{thm [source] trueI_2} only appears once, since the 
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1033
  function @{ML Thm.add_thm} tests for duplicates, before extending
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1034
  the list. Deletion from the list works as follows:
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1035
*}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1036
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1037
declare test[my_thms del]
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1038
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
  1039
text {* After this, the theorem list is again: 
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1040
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1041
  @{ML_response_fake [display,gray]
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1042
  "!my_thms"
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1043
  "[\"True\"]"}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1044
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1045
  We used in this example two functions declared as @{ML Thm.declaration_attribute}, 
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1046
  but there can be any number of them. We just have to change the parser for reading
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1047
  the arguments accordingly. 
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1048
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
  1049
  However, as said at the beginning of this example, using references for storing theorems is
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1050
  \emph{not} the received way of doing such things. The received way is to 
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1051
  start a ``data slot'' in a context by using the functor @{text GenericDataFun}:
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1052
*}
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1053
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1054
ML {*structure Data = GenericDataFun
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1055
 (type T = thm list
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1056
  val empty = []
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1057
  val extend = I
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1058
  fun merge _ = Thm.merge_thms) *}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1059
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1060
text {*
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
  1061
  To use this data slot, you only have to change @{ML my_thms_add} and
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1062
  @{ML my_thms_del} to:
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1063
*}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1064
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1065
ML{*val thm_add = Data.map o Thm.add_thm
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1066
val thm_del = Data.map o Thm.del_thm*}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1067
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1068
text {*
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1069
  where @{ML Data.map} updates the data appropriately in the context. Since storing
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1070
  theorems in a special list is such a common task, there is the functor @{text NamedThmsFun},
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1071
  which does most of the work for you. To obtain such a named theorem lists, you just
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1072
  declare 
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1073
*}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1074
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1075
ML{*structure FooRules = NamedThmsFun 
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1076
 (val name = "foo" 
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1077
  val description = "Rules for foo"); *}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1078
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1079
text {*
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1080
  and set up the @{ML_struct FooRules} with the command
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1081
*}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1082
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1083
setup {* FooRules.setup *}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1084
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1085
text {*
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1086
  This code declares a data slot where the theorems are stored,
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1087
  an attribute @{text foo} (with the @{text add} and @{text del} options
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1088
  for adding and deleting theorems) and an internal ML interface to retrieve and 
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1089
  modify the theorems.
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1090
157
76cdc8f562fc added more to the simplifier section
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
  1091
  Furthermore, the facts are made available on the user-level under the dynamic 
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
  1092
  fact name @{text foo}. For example you can declare three lemmas to be of the kind
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1093
  @{text foo} by:
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1094
*}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1095
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1096
lemma rule1[foo]: "A" sorry
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1097
lemma rule2[foo]: "B" sorry
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1098
lemma rule3[foo]: "C" sorry
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1099
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1100
text {* and undeclare the first one by: *}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1101
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1102
declare rule1[foo del]
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1103
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1104
text {* and query the remaining ones with:
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1105
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1106
  \begin{isabelle}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1107
  \isacommand{thm}~@{text "foo"}\\
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1108
  @{text "> ?C"}\\
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1109
  @{text "> ?B"}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1110
  \end{isabelle}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1111
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
  1112
  On the ML-level the rules marked with @{text "foo"} can be retrieved
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1113
  using the function @{ML FooRules.get}:
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1114
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1115
  @{ML_response_fake [display,gray] "FooRules.get @{context}" "[\"?C\",\"?B\"]"}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1116
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1117
  \begin{readmore}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1118
  For more information see @{ML_file "Pure/Tools/named_thms.ML"} and also
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1119
  the recipe in Section~\ref{recipe:storingdata} about storing arbitrary
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1120
  data.
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1121
  \end{readmore}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 149
diff changeset
  1122
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
  1123
  (FIXME What are: @{text "theory_attributes"}, @{text "proof_attributes"}?)
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1124
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1125
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1126
  \begin{readmore}
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1127
  FIXME: @{ML_file "Pure/more_thm.ML"} @{ML_file "Pure/Isar/attrib.ML"}
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1128
  \end{readmore}
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1129
*}
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1130
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
  1131
153
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
  1132
section {* Theories, Contexts and Local Theories (TBD) *}
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
  1133
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1134
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1135
  (FIXME: expand)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1136
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1137
  (FIXME: explain \isacommand{setup})
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1138
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1139
  There are theories, proof contexts and local theories (in this order, if you
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1140
  want to order them). 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1141
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1142
  In contrast to an ordinary theory, which simply consists of a type
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1143
  signature, as well as tables for constants, axioms and theorems, a local
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1144
  theory also contains additional context information, such as locally fixed
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1145
  variables and local assumptions that may be used by the package. The type
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1146
  @{ML_type local_theory} is identical to the type of \emph{proof contexts}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1147
  @{ML_type "Proof.context"}, although not every proof context constitutes a
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1148
  valid local theory.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1149
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1150
 *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1151
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1152
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1153
153
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
  1154
section {* Storing Theorems\label{sec:storing} (TBD) *}
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
  1155
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
  1156
text {* @{ML PureThy.add_thms_dynamic} *}
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
  1157
100
dd8eebae11ec polished
Christian Urban <urbanc@in.tum.de>
parents: 92
diff changeset
  1158
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
  1159
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1160
(* FIXME: some code below *)
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1161
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1162
(*<*)
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1163
setup {*
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1164
 Sign.add_consts_i [("bar", @{typ "nat"},NoSyn)] 
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1165
*}
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1166
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1167
lemma "bar = (1::nat)"
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1168
  oops
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1169
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1170
setup {*   
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1171
  Sign.add_consts_i [("foo", @{typ "nat"},NoSyn)] 
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1172
 #> PureThy.add_defs false [((Binding.name "foo_def", 
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1173
       Logic.mk_equals (Const ("FirstSteps.foo", @{typ "nat"}), @{term "1::nat"})), [])] 
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1174
 #> snd
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1175
*}
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1176
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1177
lemma "foo = (1::nat)"
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1178
  apply(simp add: foo_def)
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1179
  done
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1180
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1181
thm foo_def
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1182
(*>*)
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1183
153
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
  1184
section {* Pretty-Printing (TBD) *}
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
  1185
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
  1186
text {*
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
  1187
  @{ML Pretty.big_list}
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
  1188
*}
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
  1189
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
  1190
section {* Misc (TBD) *}
92
Christian Urban <urbanc@in.tum.de>
parents: 89
diff changeset
  1191
Christian Urban <urbanc@in.tum.de>
parents: 89
diff changeset
  1192
ML {*DatatypePackage.get_datatype @{theory} "List.list"*}
Christian Urban <urbanc@in.tum.de>
parents: 89
diff changeset
  1193
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1194
end