ProgTutorial/General.thy
author Christian Urban <urbanc@in.tum.de>
Tue, 10 Nov 2009 06:24:00 +0100
changeset 382 3f153aa4f231
parent 381 97518188ef0e
child 383 72a53b07d264
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
theory General
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
imports Base FirstSteps
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
begin
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
346
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 345
diff changeset
     5
(*<*)
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 345
diff changeset
     6
setup{*
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 345
diff changeset
     7
open_file_with_prelude 
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 345
diff changeset
     8
  "General_Code.thy"
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 345
diff changeset
     9
  ["theory General", "imports Base FirstSteps", "begin"]
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 345
diff changeset
    10
*}
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 345
diff changeset
    11
(*>*)
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 345
diff changeset
    12
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 345
diff changeset
    13
358
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    14
chapter {* Isabelle Essentials *}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
319
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    16
text {*
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
    17
  Isabelle is build around a few central ideas. One central idea is the
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
    18
  LCF-approach to theorem proving where there is a small trusted core and
351
f118240ab44a updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 350
diff changeset
    19
  everything else is build on top of this trusted core 
f118240ab44a updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 350
diff changeset
    20
  \cite{GordonMilnerWadsworth79}. The fundamental data
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    21
  structures involved in this core are certified terms and certified types, 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    22
  as well as theorems.
319
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    23
*}
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    24
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    25
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
section {* Terms and Types *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
text {*
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    29
  In Isabelle, there are certified terms and uncertified terms (respectively types). 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    30
  Uncertified terms are often just called terms. One way to construct them is by 
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
    31
  using the antiquotation \mbox{@{text "@{term \<dots>}"}}. For example
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  @{ML_response [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
"@{term \"(a::nat) + b = c\"}" 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
"Const (\"op =\", \<dots>) $ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  (Const (\"HOL.plus_class.plus\", \<dots>) $ \<dots> $ \<dots>) $ \<dots>"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    38
  constructs the term @{term "(a::nat) + b = c"}. The resulting term is printed using 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    39
  the internal representation corresponding to the datatype @{ML_type_ind "term"}, 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    40
  which is defined as follows: 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
*}  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
ML_val %linenosgray{*datatype term =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
  Const of string * typ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
| Free of string * typ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
| Var of indexname * typ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
| Bound of int 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
| Abs of string * typ * term 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
| $ of term * term *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
text {*
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
    52
  This datatype implements Church-style lambda-terms, where types are
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    53
  explicitly recorded in variables, constants and abstractions.  As can be
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
    54
  seen in Line 5, terms use the usual de Bruijn index mechanism for
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
    55
  representing bound variables.  For example in
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  @{ML_response_fake [display, gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
  "@{term \"\<lambda>x y. x y\"}"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
  "Abs (\"x\", \"'a \<Rightarrow> 'b\", Abs (\"y\", \"'a\", Bound 1 $ Bound 0))"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
  the indices refer to the number of Abstractions (@{ML Abs}) that we need to
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
  skip until we hit the @{ML Abs} that binds the corresponding
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  variable. Constructing a term with dangling de Bruijn indices is possible,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  but will be flagged as ill-formed when you try to typecheck or certify it
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  (see Section~\ref{sec:typechecking}). Note that the names of bound variables
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  are kept at abstractions for printing purposes, and so should be treated
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
  only as ``comments''.  Application in Isabelle is realised with the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
  term-constructor @{ML $}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  Isabelle makes a distinction between \emph{free} variables (term-constructor
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
  @{ML Free} and written on the user level in blue colour) and
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
  \emph{schematic} variables (term-constructor @{ML Var} and written with a
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  leading question mark). Consider the following two examples
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
  @{ML_response_fake [display, gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
  val v1 = Var ((\"x\", 3), @{typ bool})
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
  val v2 = Var ((\"x1\", 3), @{typ bool})
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
  val v3 = Free (\"x\", @{typ bool})
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
  string_of_terms @{context} [v1, v2, v3]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
  |> tracing
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
"?x3, ?x1.3, x"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
  When constructing terms, you are usually concerned with free variables (as
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
  mentioned earlier, you cannot construct schematic variables using the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
  antiquotation @{text "@{term \<dots>}"}). If you deal with theorems, you have to,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  however, observe the distinction. The reason is that only schematic
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
    90
  variables can be instantiated with terms when a theorem is applied. A
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
  similar distinction between free and schematic variables holds for types
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
  (see below).
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
  Terms and types are described in detail in \isccite{sec:terms}. Their
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
  definition and many useful operations are implemented in @{ML_file "Pure/term.ML"}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  For constructing terms involving HOL constants, many helper functions are defined
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
  in @{ML_file "HOL/Tools/hologic.ML"}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
  Constructing terms via antiquotations has the advantage that only typable
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
  terms can be constructed. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
  @{ML_response_fake_both [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
  "@{term \"x x\"}"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
  "Type unification failed: Occurs check!"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
  raises a typing error, while it perfectly ok to construct the term
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
  @{ML_response_fake [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
"let
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   112
  val omega = Free (\"x\", @{typ \"nat \<Rightarrow> nat\"}) $ Free (\"x\", @{typ nat})
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
in 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
  tracing (string_of_term @{context} omega)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
  "x x"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
  with the raw ML-constructors.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
  Sometimes the internal representation of terms can be surprisingly different
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
  from what you see at the user-level, because the layers of
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
  parsing/type-checking/pretty printing can be quite elaborate. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
  \begin{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
  Look at the internal term representation of the following terms, and
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
  find out why they are represented like this:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
  \begin{itemize}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
  \item @{term "case x of 0 \<Rightarrow> 0 | Suc y \<Rightarrow> y"}  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
  \item @{term "\<lambda>(x,y). P y x"}  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
  \item @{term "{ [x::int] | x. x \<le> -2 }"}  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
  \end{itemize}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
  Hint: The third term is already quite big, and the pretty printer
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
  may omit parts of it by default. If you want to see all of it, you
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
  can use the following ML-function to set the printing depth to a higher 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
  value:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
  @{ML [display,gray] "print_depth 50"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
  \end{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
  The antiquotation @{text "@{prop \<dots>}"} constructs terms by inserting the 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
  usually invisible @{text "Trueprop"}-coercions whenever necessary. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
  Consider for example the pairs
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
@{ML_response [display,gray] "(@{term \"P x\"}, @{prop \"P x\"})" 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
"(Free (\"P\", \<dots>) $ Free (\"x\", \<dots>),
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
 Const (\"Trueprop\", \<dots>) $ (Free (\"P\", \<dots>) $ Free (\"x\", \<dots>)))"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
  where a coercion is inserted in the second component and 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
  @{ML_response [display,gray] "(@{term \"P x \<Longrightarrow> Q x\"}, @{prop \"P x \<Longrightarrow> Q x\"})" 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
  "(Const (\"==>\", \<dots>) $ \<dots> $ \<dots>, 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
 Const (\"==>\", \<dots>) $ \<dots> $ \<dots>)"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
  where it is not (since it is already constructed by a meta-implication). 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
  The purpose of the @{text "Trueprop"}-coercion is to embed formulae of
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   158
  an object logic, for example HOL, into the meta-logic of Isabelle. The coercion
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
  is needed whenever a term is constructed that will be proved as a theorem. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
  As already seen above, types can be constructed using the antiquotation 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
  @{text "@{typ \<dots>}"}. For example:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
  @{ML_response_fake [display,gray] "@{typ \"bool \<Rightarrow> nat\"}" "bool \<Rightarrow> nat"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
  The corresponding datatype is
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
ML_val{*datatype typ =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
  Type  of string * typ list 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
| TFree of string * sort 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
| TVar  of indexname * sort *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
  Like with terms, there is the distinction between free type
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   176
  variables (term-constructor @{ML "TFree"}) and schematic
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   177
  type variables (term-constructor @{ML "TVar"} and printed with
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   178
  a leading question mark). A type constant,
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
  like @{typ "int"} or @{typ bool}, are types with an empty list
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   180
  of argument types. However, it needs a bit of effort to show an
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   181
  example, because Isabelle always pretty prints types (unlike terms).
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   182
  Using just the antiquotation @{text "@{typ \"bool\"}"} we only see
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
  @{ML_response [display, gray]
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   185
  "@{typ \"bool\"}"
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   186
  "bool"}
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   187
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 375
diff changeset
   188
  that is the pretty printed version of @{text "bool"}. However, in PolyML it is 
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   189
  easy to install your own pretty printer. With the function below we 
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   190
  mimic the behaviour of the usual pretty printer for 
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   191
  datatypes.\footnote{Thanks to David Matthews for providing this
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   192
  code.}
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   193
*}
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   194
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   195
ML{*fun typ_raw_pretty_printer depth _ ty =
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   196
let
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   197
 fun cond str a =
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   198
   if depth <= 0 
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   199
   then PolyML.PrettyString "..."
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   200
   else PolyML.PrettyBlock(1, false, [], 
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   201
          [PolyML.PrettyString str, PolyML.PrettyBreak(1, 0), a])
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   202
in 
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   203
  case ty of
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   204
    Type a  => cond "Type"  (PolyML.prettyRepresentation(a, depth - 1))
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   205
  | TFree a => cond "TFree" (PolyML.prettyRepresentation(a, depth - 1)) 
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   206
  | TVar a  => cond "TVar"  (PolyML.prettyRepresentation(a, depth - 1))
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   207
end*}
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   208
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   209
text {*
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   210
  We can install this pretty printer with the function 
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   211
  @{ML_ind addPrettyPrinter in PolyML} as follows.
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   212
*}
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   213
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   214
ML{*PolyML.addPrettyPrinter typ_raw_pretty_printer*}
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   215
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   216
text {*
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 375
diff changeset
   217
  Now the type bool is printed out in full detail.
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   218
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   219
  @{ML_response [display,gray]
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   220
  "@{typ \"bool\"}"
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   221
  "Type (\"bool\", [])"}
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   222
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   223
  When printing out a list-type
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   224
  
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   225
  @{ML_response [display,gray]
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   226
  "@{typ \"'a list\"}"
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   227
  "Type (\"List.list\", [TFree (\"'a\", [\"HOL.type\"])])"}
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   228
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   229
  we can see the full name of the type is actually @{text "List.list"}, indicating
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   230
  that it is defined in the theory @{text "List"}. However, one has to be 
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 375
diff changeset
   231
  careful with names of types, because even if
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   232
  @{text "fun"}, @{text "bool"} and @{text "nat"} are defined in the 
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   233
  theories @{text "HOL"} and @{text "Nat"}, respectively, they are 
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   234
  still represented by their simple name.
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   235
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   236
   @{ML_response [display,gray]
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   237
  "@{typ \"bool \<Rightarrow> nat\"}"
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   238
  "Type (\"fun\", [Type (\"bool\", []), Type (\"nat\", [])])"}
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   239
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   240
  We can restore the usual behaviour of Isabelle's pretty printer
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   241
  with the code
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   242
*}
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   243
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   244
ML{*fun stnd_pretty_printer _ _ =   
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   245
  ml_pretty o Pretty.to_ML o Proof_Display.pp_typ Pure.thy;
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   246
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   247
PolyML.addPrettyPrinter stnd_pretty_printer*}
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   248
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   249
text {*
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   250
  After that the types for booleans, lists and so on are printed out again 
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   251
  the standard Isabelle way.
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   252
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   253
  @{ML_response_fake [display, gray]
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   254
  "@{typ \"bool\"};
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   255
@{typ \"'a list\"}"
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   256
  "\"bool\"
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   257
\"'a List.list\""}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   259
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
  Types are described in detail in \isccite{sec:types}. Their
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   261
  definition and many useful operations are implemented 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
  in @{ML_file "Pure/type.ML"}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   264
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   265
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
section {* Constructing Terms and Types Manually\label{sec:terms_types_manually} *} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
  While antiquotations are very convenient for constructing terms, they can
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
  only construct fixed terms (remember they are ``linked'' at compile-time).
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   271
  However, you often need to construct terms manually. For example, a
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   272
  function that returns the implication @{text "\<And>(x::nat). P x \<Longrightarrow> Q x"} taking
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   273
  @{term P} and @{term Q} as arguments can only be written as:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   274
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   275
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   276
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   277
ML{*fun make_imp P Q =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   278
let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   279
  val x = Free ("x", @{typ nat})
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   280
in 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   281
  Logic.all x (Logic.mk_implies (P $ x, Q $ x))
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   282
end *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   283
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   284
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   285
  The reason is that you cannot pass the arguments @{term P} and @{term Q} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   286
  into an antiquotation.\footnote{At least not at the moment.} For example 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   287
  the following does \emph{not} work.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   288
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   289
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   290
ML{*fun make_wrong_imp P Q = @{prop "\<And>(x::nat). P x \<Longrightarrow> Q x"} *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   291
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   292
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   293
  To see this, apply @{text "@{term S}"} and @{text "@{term T}"} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   294
  to both functions. With @{ML make_imp} you obtain the intended term involving 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   295
  the given arguments
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   296
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   297
  @{ML_response [display,gray] "make_imp @{term S} @{term T}" 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   298
"Const \<dots> $ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   299
  Abs (\"x\", Type (\"nat\",[]),
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   300
    Const \<dots> $ (Free (\"S\",\<dots>) $ \<dots>) $ (Free (\"T\",\<dots>) $ \<dots>))"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   301
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   302
  whereas with @{ML make_wrong_imp} you obtain a term involving the @{term "P"} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   303
  and @{text "Q"} from the antiquotation.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   305
  @{ML_response [display,gray] "make_wrong_imp @{term S} @{term T}" 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   306
"Const \<dots> $ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   307
  Abs (\"x\", \<dots>,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   308
    Const \<dots> $ (Const \<dots> $ (Free (\"P\",\<dots>) $ \<dots>)) $ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   309
                (Const \<dots> $ (Free (\"Q\",\<dots>) $ \<dots>)))"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   310
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   311
  There are a number of handy functions that are frequently used for
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   312
  constructing terms. One is the function @{ML_ind list_comb in Term}, which
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   313
  takes as argument a term and a list of terms, and produces as output the
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   314
  term list applied to the term. For example
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   315
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   316
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   317
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   318
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   319
  val trm = @{term \"P::nat\"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   320
  val args = [@{term \"True\"}, @{term \"False\"}]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   321
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   322
  list_comb (trm, args)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   323
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   324
"Free (\"P\", \"nat\") $ Const (\"True\", \"bool\") $ Const (\"False\", \"bool\")"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   325
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   326
  Another handy function is @{ML_ind lambda in Term}, which abstracts a variable 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   327
  in a term. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   328
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   329
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   330
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   331
  val x_nat = @{term \"x::nat\"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   332
  val trm = @{term \"(P::nat \<Rightarrow> bool) x\"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   333
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   334
  lambda x_nat trm
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   335
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   336
  "Abs (\"x\", \"nat\", Free (\"P\", \"bool \<Rightarrow> bool\") $ Bound 0)"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   337
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   338
  In this example, @{ML lambda} produces a de Bruijn index (i.e.~@{ML "Bound 0"}), 
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   339
  and an abstraction, where it also records the type of the abstracted
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   340
  variable and for printing purposes also its name.  Note that because of the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   341
  typing annotation on @{text "P"}, the variable @{text "x"} in @{text "P x"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   342
  is of the same type as the abstracted variable. If it is of different type,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   343
  as in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   344
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   345
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   346
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   347
  val x_int = @{term \"x::int\"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   348
  val trm = @{term \"(P::nat \<Rightarrow> bool) x\"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   349
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   350
  lambda x_int trm
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   351
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   352
  "Abs (\"x\", \"int\", Free (\"P\", \"nat \<Rightarrow> bool\") $ Free (\"x\", \"nat\"))"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   353
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   354
  then the variable @{text "Free (\"x\", \"int\")"} is \emph{not} abstracted. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   355
  This is a fundamental principle
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   356
  of Church-style typing, where variables with the same name still differ, if they 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   357
  have different type.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   358
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   359
  There is also the function @{ML_ind subst_free in Term} with which terms can be
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   360
  replaced by other terms. For example below, we will replace in @{term
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   361
  "(f::nat \<Rightarrow> nat \<Rightarrow> nat) 0 x"} the subterm @{term "(f::nat \<Rightarrow> nat \<Rightarrow> nat) 0"} by
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   362
  @{term y}, and @{term x} by @{term True}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   363
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   364
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   365
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   366
  val sub1 = (@{term \"(f::nat \<Rightarrow> nat \<Rightarrow> nat) 0\"}, @{term \"y::nat \<Rightarrow> nat\"})
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   367
  val sub2 = (@{term \"x::nat\"}, @{term \"True\"})
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   368
  val trm = @{term \"((f::nat \<Rightarrow> nat \<Rightarrow> nat) 0) x\"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   369
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   370
  subst_free [sub1, sub2] trm
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   371
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   372
  "Free (\"y\", \"nat \<Rightarrow> nat\") $ Const (\"True\", \"bool\")"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   373
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   374
  As can be seen, @{ML subst_free} does not take typability into account.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   375
  However it takes alpha-equivalence into account:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   376
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   377
  @{ML_response_fake [display, gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   378
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   379
  val sub = (@{term \"(\<lambda>y::nat. y)\"}, @{term \"x::nat\"})
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   380
  val trm = @{term \"(\<lambda>x::nat. x)\"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   381
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   382
  subst_free [sub] trm
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   383
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   384
  "Free (\"x\", \"nat\")"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   385
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   386
  Similarly the function @{ML_ind subst_bounds in Term}, replaces lose bound 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   387
  variables with terms. To see how this function works, let us implement a
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   388
  function that strips off the outermost quantifiers in a term.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   389
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   390
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   391
ML{*fun strip_alls t =
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   392
let 
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   393
  fun aux (x, T, t) = strip_alls t |>> cons (Free (x, T))
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   394
in
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   395
  case t of
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   396
    Const ("All", _) $ Abs body => aux body
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   397
  | _ => ([], t)
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   398
end*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   399
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   400
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   401
  The function returns a pair consisting of the stripped off variables and
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   402
  the body of the universal quantification. For example
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   403
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   404
  @{ML_response_fake [display, gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   405
  "strip_alls @{term \"\<forall>x y. x = (y::bool)\"}"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   406
"([Free (\"x\", \"bool\"), Free (\"y\", \"bool\")],
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   407
  Const (\"op =\", \<dots>) $ Bound 1 $ Bound 0)"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   408
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   409
  After calling @{ML strip_alls}, you obtain a term with lose bound variables. With
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   410
  the function @{ML subst_bounds}, you can replace these lose @{ML_ind 
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   411
  Bound in Term}s with the stripped off variables.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   412
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   413
  @{ML_response_fake [display, gray, linenos]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   414
  "let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   415
  val (vrs, trm) = strip_alls @{term \"\<forall>x y. x = (y::bool)\"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   416
in 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   417
  subst_bounds (rev vrs, trm) 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   418
  |> string_of_term @{context}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   419
  |> tracing
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   420
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   421
  "x = y"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   422
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   423
  Note that in Line 4 we had to reverse the list of variables that @{ML
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   424
  strip_alls} returned. The reason is that the head of the list the function
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   425
  @{ML subst_bounds} takes is the replacement for @{ML "Bound 0"}, the next
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   426
  element for @{ML "Bound 1"} and so on. 
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   427
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   428
  Notice also that this function might introduce name clashes, since we
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   429
  substitute just a variable with the name recorded in an abstraction. This
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   430
  name is by no means unique. If clashes need to be avoided, then we should
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   431
  use the function @{ML_ind dest_abs in Term}, which returns the body where
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   432
  the lose de Bruijn index is replaced by a unique free variable. For example
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   433
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   434
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   435
  @{ML_response_fake [display,gray]
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   436
  "let
374
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   437
  val body = Bound 0 $ Free (\"x\", @{typ nat})
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   438
in
374
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   439
  Term.dest_abs (\"x\", @{typ \"nat \<Rightarrow> bool\"}, body)
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   440
end"
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   441
  "(\"xa\", Free (\"xa\", \"nat \<Rightarrow> bool\") $ Free (\"x\", \"nat\"))"}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   442
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   443
  There are also many convenient functions that construct specific HOL-terms
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   444
  in the structure @{ML_struct HOLogic}. For
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   445
  example @{ML_ind mk_eq in HOLogic} constructs an equality out of two terms.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   446
  The types needed in this equality are calculated from the type of the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   447
  arguments. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   448
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   449
@{ML_response_fake [gray,display]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   450
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   451
  val eq = HOLogic.mk_eq (@{term \"True\"}, @{term \"False\"})
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   452
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   453
  string_of_term @{context} eq
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   454
  |> tracing
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   455
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   456
  "True = False"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   457
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   458
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   459
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   460
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   461
  There are many functions in @{ML_file "Pure/term.ML"}, @{ML_file
374
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   462
  "Pure/logic.ML"} and @{ML_file "HOL/Tools/hologic.ML"} that make manual
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   463
  constructions of terms and types easier.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   464
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   465
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   466
  When constructing terms manually, there are a few subtle issues with
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   467
  constants. They usually crop up when pattern matching terms or types, or
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   468
  when constructing them. While it is perfectly ok to write the function
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   469
  @{text is_true} as follows
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   470
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   471
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   472
ML{*fun is_true @{term True} = true
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   473
  | is_true _ = false*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   474
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   475
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   476
  this does not work for picking out @{text "\<forall>"}-quantified terms. Because
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   477
  the function 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   478
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   479
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   480
ML{*fun is_all (@{term All} $ _) = true
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   481
  | is_all _ = false*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   482
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   483
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   484
  will not correctly match the formula @{prop[source] "\<forall>x::nat. P x"}: 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   485
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   486
  @{ML_response [display,gray] "is_all @{term \"\<forall>x::nat. P x\"}" "false"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   487
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   488
  The problem is that the @{text "@term"}-antiquotation in the pattern 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   489
  fixes the type of the constant @{term "All"} to be @{typ "('a \<Rightarrow> bool) \<Rightarrow> bool"} for 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   490
  an arbitrary, but fixed type @{typ "'a"}. A properly working alternative 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   491
  for this function is
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   492
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   493
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   494
ML{*fun is_all (Const ("All", _) $ _) = true
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   495
  | is_all _ = false*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   496
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   497
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   498
  because now
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   499
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   500
@{ML_response [display,gray] "is_all @{term \"\<forall>x::nat. P x\"}" "true"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   501
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   502
  matches correctly (the first wildcard in the pattern matches any type and the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   503
  second any term).
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   504
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   505
  However there is still a problem: consider the similar function that
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   506
  attempts to pick out @{text "Nil"}-terms:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   507
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   508
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   509
ML{*fun is_nil (Const ("Nil", _)) = true
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   510
  | is_nil _ = false *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   511
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   512
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   513
  Unfortunately, also this function does \emph{not} work as expected, since
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   514
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   515
  @{ML_response [display,gray] "is_nil @{term \"Nil\"}" "false"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   516
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   517
  The problem is that on the ML-level the name of a constant is more
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   518
  subtle than you might expect. The function @{ML is_all} worked correctly,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   519
  because @{term "All"} is such a fundamental constant, which can be referenced
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   520
  by @{ML "Const (\"All\", some_type)" for some_type}. However, if you look at
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   521
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   522
  @{ML_response [display,gray] "@{term \"Nil\"}" "Const (\"List.list.Nil\", \<dots>)"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   523
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   524
  the name of the constant @{text "Nil"} depends on the theory in which the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   525
  term constructor is defined (@{text "List"}) and also in which datatype
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   526
  (@{text "list"}). Even worse, some constants have a name involving
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   527
  type-classes. Consider for example the constants for @{term "zero"} and
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   528
  \mbox{@{text "(op *)"}}:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   529
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   530
  @{ML_response [display,gray] "(@{term \"0::nat\"}, @{term \"(op *)\"})" 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   531
 "(Const (\"HOL.zero_class.zero\", \<dots>), 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   532
 Const (\"HOL.times_class.times\", \<dots>))"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   533
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   534
  While you could use the complete name, for example 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   535
  @{ML "Const (\"List.list.Nil\", some_type)" for some_type}, for referring to or
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   536
  matching against @{text "Nil"}, this would make the code rather brittle. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   537
  The reason is that the theory and the name of the datatype can easily change. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   538
  To make the code more robust, it is better to use the antiquotation 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   539
  @{text "@{const_name \<dots>}"}. With this antiquotation you can harness the 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   540
  variable parts of the constant's name. Therefore a function for 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   541
  matching against constants that have a polymorphic type should 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   542
  be written as follows.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   543
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   544
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   545
ML{*fun is_nil_or_all (Const (@{const_name "Nil"}, _)) = true
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   546
  | is_nil_or_all (Const (@{const_name "All"}, _) $ _) = true
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   547
  | is_nil_or_all _ = false *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   548
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   549
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   550
  The antiquotation for properly referencing type constants is @{text "@{type_name \<dots>}"}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   551
  For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   552
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   553
  @{ML_response [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   554
  "@{type_name \"list\"}" "\"List.list\""}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   555
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   556
  \footnote{\bf FIXME: Explain the following better; maybe put in a separate
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   557
  section and link with the comment in the antiquotation section.}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   558
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   559
  Occasionally you have to calculate what the ``base'' name of a given
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   560
  constant is. For this you can use the function @{ML_ind "Sign.extern_const"} or
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   561
  @{ML_ind  Long_Name.base_name}. For example:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   562
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   563
  @{ML_response [display,gray] "Sign.extern_const @{theory} \"List.list.Nil\"" "\"Nil\""}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   564
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   565
  The difference between both functions is that @{ML extern_const in Sign} returns
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   566
  the smallest name that is still unique, whereas @{ML base_name in Long_Name} always
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   567
  strips off all qualifiers.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   568
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   569
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   570
  Functions about naming are implemented in @{ML_file "Pure/General/name_space.ML"};
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   571
  functions about signatures in @{ML_file "Pure/sign.ML"}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   572
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   573
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   574
  Although types of terms can often be inferred, there are many
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   575
  situations where you need to construct types manually, especially  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   576
  when defining constants. For example the function returning a function 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   577
  type is as follows:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   578
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   579
*} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   580
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   581
ML{*fun make_fun_type ty1 ty2 = Type ("fun", [ty1, ty2]) *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   582
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   583
text {* This can be equally written with the combinator @{ML_ind "-->" in Term} as: *}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   584
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   585
ML{*fun make_fun_type ty1 ty2 = ty1 --> ty2 *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   586
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   587
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   588
  If you want to construct a function type with more than one argument
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   589
  type, then you can use @{ML_ind "--->" in Term}.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   590
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   591
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   592
ML{*fun make_fun_types tys ty = tys ---> ty *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   593
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   594
text {*
369
74ba778b09c9 tuned index
Christian Urban <urbanc@in.tum.de>
parents: 368
diff changeset
   595
  A handy function for manipulating terms is @{ML_ind map_types in Term}: it takes a 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   596
  function and applies it to every type in a term. You can, for example,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   597
  change every @{typ nat} in a term into an @{typ int} using the function:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   598
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   599
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   600
ML{*fun nat_to_int ty =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   601
  (case ty of
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   602
     @{typ nat} => @{typ int}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   603
   | Type (s, tys) => Type (s, map nat_to_int tys)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   604
   | _ => ty)*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   605
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   606
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   607
  Here is an example:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   608
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   609
@{ML_response_fake [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   610
"map_types nat_to_int @{term \"a = (1::nat)\"}" 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   611
"Const (\"op =\", \"int \<Rightarrow> int \<Rightarrow> bool\")
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   612
           $ Free (\"a\", \"int\") $ Const (\"HOL.one_class.one\", \"int\")"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   613
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   614
  If you want to obtain the list of free type-variables of a term, you
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   615
  can use the function @{ML_ind  add_tfrees in Term} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   616
  (similarly @{ML_ind  add_tvars in Term} for the schematic type-variables). 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   617
  One would expect that such functions
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   618
  take a term as input and return a list of types. But their type is actually 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   619
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   620
  @{text[display] "Term.term -> (string * Term.sort) list -> (string * Term.sort) list"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   621
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   622
  that is they take, besides a term, also a list of type-variables as input. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   623
  So in order to obtain the list of type-variables of a term you have to 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   624
  call them as follows
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   625
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   626
  @{ML_response [gray,display]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   627
  "Term.add_tfrees @{term \"(a, b)\"} []"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   628
  "[(\"'b\", [\"HOL.type\"]), (\"'a\", [\"HOL.type\"])]"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   629
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   630
  The reason for this definition is that @{ML add_tfrees in Term} can
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   631
  be easily folded over a list of terms. Similarly for all functions
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   632
  named @{text "add_*"} in @{ML_file "Pure/term.ML"}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   633
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   634
  \begin{exercise}\label{fun:revsum}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   635
  Write a function @{text "rev_sum : term -> term"} that takes a
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   636
  term of the form @{text "t\<^isub>1 + t\<^isub>2 + \<dots> + t\<^isub>n"} (whereby @{text "n"} might be one)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   637
  and returns the reversed sum @{text "t\<^isub>n + \<dots> + t\<^isub>2 + t\<^isub>1"}. Assume
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   638
  the @{text "t\<^isub>i"} can be arbitrary expressions and also note that @{text "+"} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   639
  associates to the left. Try your function on some examples. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   640
  \end{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   641
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   642
  \begin{exercise}\label{fun:makesum}
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   643
  Write a function that takes two terms representing natural numbers
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   644
  in unary notation (like @{term "Suc (Suc (Suc 0))"}), and produces the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   645
  number representing their sum.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   646
  \end{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   647
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   648
  \begin{exercise}\label{ex:debruijn}
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   649
  Implement the function, which we below name deBruijn, that depends on a natural
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   650
  number n$>$0 and constructs terms of the form:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   651
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   652
  \begin{center}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   653
  \begin{tabular}{r@ {\hspace{2mm}}c@ {\hspace{2mm}}l}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   654
  {\it rhs n} & $\dn$ & {\large$\bigwedge$}{\it i=1\ldots n.  P\,i}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   655
  {\it lhs n} & $\dn$ & {\large$\bigwedge$}{\it i=1\ldots n. P\,i = P (i + 1 mod n)}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   656
                        $\longrightarrow$ {\it rhs n}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   657
  {\it deBruijn n} & $\dn$ & {\it lhs n} $\longrightarrow$ {\it rhs n}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   658
  \end{tabular}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   659
  \end{center}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   660
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   661
  This function returns for n=3 the term
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   662
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   663
  \begin{center}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   664
  \begin{tabular}{l}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   665
  (P 1 = P 2 $\longrightarrow$ P 1 $\wedge$ P 2 $\wedge$ P 3) $\wedge$\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   666
  (P 2 = P 3 $\longrightarrow$ P 1 $\wedge$ P 2 $\wedge$ P 3) $\wedge$\\ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   667
  (P 3 = P 1 $\longrightarrow$ P 1 $\wedge$ P 2 $\wedge$ P 3) $\longrightarrow$ P 1 $\wedge$ P 2 $\wedge$ P 3
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   668
  \end{tabular}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   669
  \end{center}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   670
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   671
  Make sure you use the functions defined in @{ML_file "HOL/Tools/hologic.ML"}
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   672
  for constructing the terms for the logical connectives.\footnote{Thanks to Roy
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
   673
  Dyckhoff for suggesting this exercise and working out the details.} 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   674
  \end{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   675
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   676
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   677
section {* Unification and Matching (TBD) *}
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 375
diff changeset
   678
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 375
diff changeset
   679
text {* 
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   680
  Isabelle's terms and types may contain schematic term variables
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   681
  (term-constructor @{ML Var}) and schematic type variables (term-constructor
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   682
  @{ML TVar}). These variables stand for unknown entities, which can be made
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   683
  more concrete by instantiations. Such instantiations might be a result of
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   684
  unification or matching. While in case of types, unification and matching is
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   685
  relatively straightforward, in case of terms the algorithms are
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   686
  substantially more complicated, because terms need higher-order versions of
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   687
  the unification and matching algorithms. Below we shall use the
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   688
  antiquotations @{text "@{typ_pat \<dots>}"} and @{text "@{term_pat \<dots>}"} from
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   689
  Section~\ref{sec:antiquote} in order to construct examples involving
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   690
  schematic variables.
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   691
382
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   692
  Let us begin with describing the unification and matching function for
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   693
  types.  Both return type environments, ML-type @{ML_type "Type.tyenv"},
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   694
  which map schematic type variables to types (and sorts). Below we use the
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   695
  function @{ML_ind typ_unify in Sign} from the structure @{ML_struct Sign}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   696
  for unifying the types @{text "?'a * ?'b"} and @{text "?'b list *
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   697
  nat"}. This will produce the mapping, or type environment, @{text "[?'a :=
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   698
  ?'b list, ?'b := nat]"}.
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   699
*}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   700
382
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   701
ML %linenosgray{*val (tyenv_unif, _) = let
379
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
   702
  val ty1 = @{typ_pat "?'a * ?'b"}
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
   703
  val ty2 = @{typ_pat "?'b list * nat"}
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   704
in
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   705
  Sign.typ_unify @{theory} (ty1, ty2) (Vartab.empty, 0) 
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   706
end*}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   707
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   708
text {* 
382
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   709
  The environment @{ML_ind "Vartab.empty"} in line 5 stands for the 
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   710
  empty type environment, which is needed for starting the unification 
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   711
  without any (pre)instantiations.  In case of a failure 
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   712
  @{ML typ_unify in Sign} will throw the exception @{text TUNIFY}.
379
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
   713
  We can print out the resulting type environment @{ML tyenv_unif} 
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   714
  with the built-in function @{ML_ind dest in Vartab} from the structure
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   715
  @{ML_struct Vartab}.
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   716
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   717
  @{ML_response_fake [display,gray]
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   718
  "Vartab.dest tyenv_unif"
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   719
  "[((\"'a\", 0), ([\"HOL.type\"], \"?'b List.list\")), 
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   720
 ((\"'b\", 0), ([\"HOL.type\"], \"nat\"))]"} 
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 375
diff changeset
   721
*}
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 375
diff changeset
   722
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   723
text_raw {*
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   724
  \begin{figure}[t]
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   725
  \begin{minipage}{\textwidth}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   726
  \begin{isabelle}*}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   727
ML{*fun pretty_helper f env =
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   728
  env |> Vartab.dest  
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   729
      |> map f 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   730
      |> commas 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   731
      |> enclose "[" "]" 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   732
      |> tracing
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   733
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   734
fun pretty_tyenv ctxt tyenv =
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   735
let
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   736
  fun aux (v, (s, T)) =
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   737
        Syntax.string_of_typ ctxt (TVar (v, s)) 
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   738
          ^ " := " ^ Syntax.string_of_typ ctxt T
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   739
in 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   740
  pretty_helper aux tyenv
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   741
end*}
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   742
text_raw {*
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   743
  \end{isabelle}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   744
  \end{minipage}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   745
  \caption{A pretty printing function for type environments, which are 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   746
  produced by unification and matching.\label{fig:prettyenv}}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   747
  \end{figure}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   748
*}
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 375
diff changeset
   749
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   750
text {*
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   751
  The first components in this list stand for the schematic type variables and
382
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   752
  the second are the associated sorts and types. In this example the sort
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   753
  is the default sort @{text "HOL.type"}. We will use in what follows
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   754
  the pretty-printing function from Figure~\ref{fig:prettyenv} for 
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   755
  @{ML_type "Type.tyenv"}s. For the type environment in the example 
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   756
  this function prints out the more legible: 
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   757
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   758
  @{ML_response_fake [display, gray]
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   759
  "pretty_tyenv @{context} tyenv_unif"
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   760
  "[?'a := ?'b list, ?'b := nat]"}
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 375
diff changeset
   761
382
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   762
  The index @{text 0} in Line 5 in the example above is the maximal index of
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   763
  the schematic type variables occuring in @{text ty1} and @{text ty2}. This
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   764
  index will be increased whenever a new schematic type variable is introduced
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   765
  during unification.  This is for example the case when two schematic type
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   766
  variables have different, incomparable sorts. Then a new schematic type
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   767
  variable is introduced with the combined sorts. To show this let us assume two
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   768
  sorts, say @{text "s1"} and @{text "s2"}, which we attach to the schematic type
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   769
  variables @{text "?'a"} and @{text "?'b"}. Since we do not make any assumption
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   770
  about the sors, they are incomparable, leading to the type environment:
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   771
*}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   772
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   773
ML{*val (tyenv, index) = let
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   774
  val ty2 = @{typ_pat "?'a::s1"}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   775
  val ty1 = @{typ_pat "?'b::s2"}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   776
in
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   777
  Sign.typ_unify @{theory} (ty1, ty2) (Vartab.empty, 0) 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   778
end*}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   779
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   780
text {*
382
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   781
  To print out this type environment we switch on the printing of sort 
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   782
  information.
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   783
*}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   784
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   785
ML{*show_sorts := true*}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   786
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   787
text {*
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   788
  Now we can see
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   789
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   790
  @{ML_response_fake [display,gray]
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   791
  "pretty_tyenv @{context} tyenv"
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   792
  "[?'a::s1 := ?'a1::{s1,s2}, ?'b::s2 := ?'a1::{s1,s2}]"}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   793
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   794
  the type variables @{text "?'a"} and @{text "?'b"} are instantiated
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   795
  with a new type variable @{text "?'a1"} with sort @{text "{s1,s2}"}. Since a new
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   796
  type variable has been introduced the @{ML index}, originally being @{text 0}, 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   797
  has been increased by @{text 1}.
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   798
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   799
  @{ML_response [display,gray]
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   800
  "index"
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   801
  "1"}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   802
*}(*<*)ML %linenos{*show_sorts := false*}(*>*)
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   803
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   804
text {*
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   805
  The way the unification function @{ML typ_unify in Sign} is implemented 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   806
  using an initial type environment and initial index makes it easy to
382
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   807
  unify more than two terms. For example 
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   808
*}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   809
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   810
ML{*val (tyenvs, _) = let
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   811
  val tys1 = (@{typ_pat "?'a"}, @{typ_pat "?'b list"})
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   812
  val tys2 = (@{typ_pat "?'b"}, @{typ_pat "nat"})
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   813
in
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   814
  fold (Sign.typ_unify @{theory}) [tys1, tys2] (Vartab.empty, 0) 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   815
end*}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   816
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   817
text {*
382
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   818
  Let us now return again to the unifier from the first example in this
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
   819
  section.
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   820
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   821
  @{ML_response_fake [display, gray]
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   822
  "pretty_tyenv @{context} tyenv_unif"
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   823
  "[?'a := ?'b list, ?'b := nat]"}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   824
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   825
  Observe that the type environment which the function @{ML typ_unify in
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   826
  Sign} returns is in \emph{not} an instantiation in fully solved form: while @{text
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   827
  "?'b"} is instantiated to @{typ nat}, this is not propagated to the
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   828
  instantiation for @{text "?'a"}.  In unification theory, this is often
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   829
  called an instantiation in \emph{triangular form}. These not fully solved 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   830
  instantiations are used because of performance reasons.
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   831
  To apply a type environment in triangular form to a type, say @{text "?'a *
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   832
  ?'b"}, you should use the function @{ML_ind norm_type in Envir}:
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 375
diff changeset
   833
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   834
  @{ML_response_fake [display, gray]
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   835
  "Envir.norm_type tyenv_unif @{typ_pat \"?'a * ?'b\"}"
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   836
  "nat list * nat"}
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   837
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   838
  With this function the type variables @{text "?'a"} and @{text "?'b"} are 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   839
  correctly instantiated.
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   840
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   841
  Matching of types can be done with the function @{ML_ind typ_match in Sign}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   842
  from the structure @{ML_struct Sign}. It also returns a @{ML_type
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   843
  Type.tyenv} and might raise the exception @{text TYPE_MATCH} in case
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   844
  of failure. For example
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   845
*}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   846
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   847
ML{*val tyenv_match = let
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   848
  val pat = @{typ_pat "?'a * ?'b"}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   849
  and ty  = @{typ_pat "bool list * nat"}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   850
in
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   851
  Sign.typ_match @{theory} (pat, ty) Vartab.empty 
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   852
end*}
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 375
diff changeset
   853
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 375
diff changeset
   854
text {*
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   855
  Printing out the calculated matcher gives
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   856
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   857
  @{ML_response_fake [display,gray]
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   858
  "pretty_tyenv @{context} tyenv_match"
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   859
  "[?'a := bool list, ?'b := nat]"}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   860
  
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   861
  Unlike the unification, which uses the function @{ML norm_type in Envir}, 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   862
  applying the matcher to a type needs to be done with the function
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   863
  @{ML_ind subst_type in Envir}.
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   864
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   865
  @{ML_response_fake [display, gray]
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   866
  "Envir.subst_type tyenv_match @{typ_pat \"?'a * ?'b\"}"
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   867
  "bool list * nat"}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   868
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   869
  Be careful to observe the difference: use always 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   870
  @{ML subst_type in Envir} for matchers and @{ML norm_type in Envir} 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   871
  for unifiers. To make the difference explicit, let us calculate the 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   872
  following matcher:
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   873
*}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   874
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   875
ML{*val tyenv_match' = let
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   876
  val pat = @{typ_pat "?'a * ?'b"}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   877
  and ty  = @{typ_pat "?'b list * nat"}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   878
in
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   879
  Sign.typ_match @{theory} (pat, ty) Vartab.empty 
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   880
end*}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   881
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   882
text {*
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   883
  Now @{ML tyenv_unif} is equal to @{ML tyenv_match'}. If we apply 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   884
  @{ML norm_type in Envir} to the matcher we obtain
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   885
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   886
  @{ML_response_fake [display, gray]
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   887
  "Envir.norm_type tyenv_match' @{typ_pat \"?'a * ?'b\"}"
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   888
  "nat list * nat"}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   889
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   890
  which is not a matcher for the second matching problem, and if
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   891
  we apply @{ML subst_type in Envir} to the unifyier we obtain
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   892
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   893
  @{ML_response_fake [display, gray]
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   894
  "Envir.subst_type tyenv_unif @{typ_pat \"?'a * ?'b\"}"
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   895
  "?'b list * nat"}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   896
  
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   897
  which is not the correct unifier for the unification problem.
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   898
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   899
  \begin{readmore}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   900
  Unification and matching of types is implemented
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   901
  in @{ML_file "Pure/type.ML"}. The ``interface'' functions for them
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   902
  are in @{ML_file "Pure/sign.ML"}. Matching and unification produce type environments
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   903
  as results. These are implemented in @{ML_file "Pure/envir.ML"}.
379
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
   904
  This file also includes the substitution and normalisation functions,
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
   905
  that apply a type environment to a type. Type environments are lookup 
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
   906
  tables which are implemented in @{ML_file "Pure/term_ord.ML"}.
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   907
  \end{readmore}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   908
*}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   909
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   910
text {*
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   911
  Unification and matching of terms is substantially more complicated than the
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   912
  type-case. The reason is that terms have abstractions and unification
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   913
  or matching modulo plain equality is often not meaningful in this
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   914
  context. Nevertheless, Isabelle implements the function @{ML_ind
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   915
  first_order_match in Pattern} for terms.  This matching function returns a
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   916
  type environment and a term environment.  To pretty print the latter we use
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   917
  the function @{text "pretty_env"}:
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   918
*}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   919
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   920
ML{*fun pretty_env ctxt env =
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   921
let
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   922
  fun aux (v, (T, t)) =
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   923
        string_of_term ctxt (Var (v, T)) ^ " := " ^ string_of_term ctxt t
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   924
in
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   925
  pretty_helper aux env 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   926
end*}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   927
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   928
text {*
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   929
  As can be seen from the @{text "aux"}-function, a term environment associates 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   930
  a schematic term variable with a type and a term.  An example of a first-order 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   931
  matching problem is the term @{term "P (\<lambda>a b. Q b a)"} and the pattern 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   932
  @{text "?X ?Y"}.
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   933
*}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   934
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   935
ML{*val (_, fo_env) = let
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   936
  val fo_pat = @{term_pat "(?X::(nat\<Rightarrow>nat\<Rightarrow>nat)\<Rightarrow>bool) ?Y"}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   937
  val trm = @{term "P::(nat\<Rightarrow>nat\<Rightarrow>nat)\<Rightarrow>bool"} 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   938
              $ @{term "\<lambda>a b. (Q::nat\<Rightarrow>nat\<Rightarrow>nat) b a"}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   939
  val init = (Vartab.empty, Vartab.empty) 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   940
in
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   941
  Pattern.first_order_match @{theory} (fo_pat, trm) init
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   942
end *}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   943
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   944
text {*
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   945
  In this example we annotated explicitly types because then 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   946
  the type environment is empty and can be ignored. The 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   947
  resulting term environment is:
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   948
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   949
  @{ML_response_fake [display, gray]
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   950
  "pretty_env @{context} fo_env"
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   951
  "[?X := P, ?Y := \<lambda>a b. Q b a]"}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   952
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   953
  The matcher can be applied to a term using the function @{ML_ind subst_term
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   954
  in Envir} (remember the same convention for types applies to terms: @{ML
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   955
  subst_term in Envir} is for matchers and @{ML norm_term in Envir} is for
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   956
  unifyiers). The function @{ML subst_term in Envir} expects a type environment,
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   957
  which is set to empty in the example below, and a term environment.
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   958
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   959
  @{ML_response_fake [display, gray]
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   960
  "let
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   961
  val trm = @{term_pat \"(?X::(nat\<Rightarrow>nat\<Rightarrow>nat)\<Rightarrow>bool) ?Y\"}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   962
in
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   963
  Envir.subst_term (Vartab.empty, fo_env) trm
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   964
  |> string_of_term @{context}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   965
  |> tracing
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   966
end"
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   967
  "P (\<lambda>a b. Q b a)"}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   968
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   969
  First-order matching is useful for matching against applications and
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   970
  variables. It can deal also with abstractions and a limited form of
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   971
  alpha-equivalence, but this kind of matching should be used with care, since
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   972
  it is not clear whether the result is meaningful. A working example is
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   973
  matching @{text "\<lambda>x. P x"} against the pattern @{text "\<lambda>y. ?X y"}. In this
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   974
  case first-order matching produces @{text "[?X := P]"}.
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   975
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   976
  @{ML_response_fake [display, gray]
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   977
  "let
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   978
  val fo_pat = @{term_pat \"\<lambda>y. (?X::nat\<Rightarrow>bool) y\"}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   979
  val trm = @{term \"\<lambda>x. (P::nat\<Rightarrow>bool) x\"} 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   980
  val init = (Vartab.empty, Vartab.empty) 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   981
in
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   982
  Pattern.first_order_match @{theory} (fo_pat, trm) init
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   983
  |> snd 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   984
  |> pretty_env @{context} 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   985
end"
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   986
  "[?X := P]"}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   987
*}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   988
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   989
text {*
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   990
  In the context of unifying abstractions, more thouroughly studied is
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   991
  higher-order pattern unification and higher-order pattern matching.  A
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   992
  \emph{\index*{pattern}} is a lambda term whose ``head symbol'' (that is the
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   993
  first symbol under abstractions) is either a constant, or a schematic or a free
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   994
  variable. If it is a schematic variable then it can be only applied by
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   995
  distinct bound variables. This excludes that a schematic variable is an
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   996
  argument of a schematic variable and that a schematic variable is applied
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   997
  twice to the same bound variable. The function @{ML_ind pattern in Pattern}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   998
  in the structure @{ML_struct Pattern} tests whether a term satisfies these
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   999
  restrictions.
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1000
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1001
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1002
  @{ML_response [display, gray]
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1003
  "let
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1004
  val trm_list = 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1005
        [@{term_pat \"?X\"},              @{term_pat \"a\"},
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1006
         @{term_pat \"\<lambda>a b. ?X a b\"},    @{term_pat \"\<lambda>a b. (op +) a b\"},
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1007
         @{term_pat \"\<lambda>a. (op +) a ?Y\"}, @{term_pat \"?X ?Y\"},
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1008
         @{term_pat \"\<lambda>a b. ?X a b ?Y\"}, @{term_pat \"\<lambda>a. ?X a a\"}] 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1009
in
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1010
  map Pattern.pattern trm_list
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1011
end"
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1012
  "[true, true, true, true, true, false, false, false]"}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1013
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1014
  The point of restricting unification and matching to patterns is that
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1015
  it is decidable and produces most general unifiers. In this way 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1016
  matching and unify can be implemented so that they produce a type
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1017
  and term environment (the latter actually produces only a term 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1018
  environment).
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1019
*}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1020
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1021
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1022
text {*
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
  1023
  \begin{readmore}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
  1024
  For term, unification and matching of higher-order patterns is implemented in 
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1025
  @{ML_file "Pure/pattern.ML"}. This file also contains a first-order matcher
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1026
  for terms. Full higher-order unification is implemented
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
  1027
  in @{ML_file "Pure/unify.ML"}. 
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
  1028
  \end{readmore}
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 375
diff changeset
  1029
*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1030
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1031
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1032
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1033
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1034
section {* Type-Checking\label{sec:typechecking} *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1035
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1036
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1037
  Remember Isabelle follows the Church-style typing for terms, i.e., a term contains 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1038
  enough typing information (constants, free variables and abstractions all have typing 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1039
  information) so that it is always clear what the type of a term is. 
369
74ba778b09c9 tuned index
Christian Urban <urbanc@in.tum.de>
parents: 368
diff changeset
  1040
  Given a well-typed term, the function @{ML_ind type_of in Term} returns the 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1041
  type of a term. Consider for example:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1042
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1043
  @{ML_response [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1044
  "type_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::nat\"})" "bool"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1045
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1046
  To calculate the type, this function traverses the whole term and will
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1047
  detect any typing inconsistency. For example changing the type of the variable 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1048
  @{term "x"} from @{typ "nat"} to @{typ "int"} will result in the error message: 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1049
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1050
  @{ML_response_fake [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1051
  "type_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::int\"})" 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1052
  "*** Exception- TYPE (\"type_of: type mismatch in application\" \<dots>"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1053
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1054
  Since the complete traversal might sometimes be too costly and
369
74ba778b09c9 tuned index
Christian Urban <urbanc@in.tum.de>
parents: 368
diff changeset
  1055
  not necessary, there is the function @{ML_ind fastype_of in Term}, which 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1056
  also returns the type of a term.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1057
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1058
  @{ML_response [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1059
  "fastype_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::nat\"})" "bool"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1060
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1061
  However, efficiency is gained on the expense of skipping some tests. You 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1062
  can see this in the following example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1063
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1064
   @{ML_response [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1065
  "fastype_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::int\"})" "bool"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1066
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1067
  where no error is detected.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1068
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1069
  Sometimes it is a bit inconvenient to construct a term with 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1070
  complete typing annotations, especially in cases where the typing 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1071
  information is redundant. A short-cut is to use the ``place-holder'' 
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
  1072
  type @{ML_ind dummyT in Term} and then let type-inference figure out the 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1073
  complete type. An example is as follows:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1074
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1075
  @{ML_response_fake [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1076
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1077
  val c = Const (@{const_name \"plus\"}, dummyT) 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1078
  val o = @{term \"1::nat\"} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1079
  val v = Free (\"x\", dummyT)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1080
in   
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1081
  Syntax.check_term @{context} (c $ o $ v)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1082
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1083
"Const (\"HOL.plus_class.plus\", \"nat \<Rightarrow> nat \<Rightarrow> nat\") $
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1084
  Const (\"HOL.one_class.one\", \"nat\") $ Free (\"x\", \"nat\")"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1085
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1086
  Instead of giving explicitly the type for the constant @{text "plus"} and the free 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1087
  variable @{text "x"}, type-inference fills in the missing information.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1088
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1089
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1090
  See @{ML_file "Pure/Syntax/syntax.ML"} where more functions about reading,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1091
  checking and pretty-printing of terms are defined. Functions related to
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1092
  type-inference are implemented in @{ML_file "Pure/type.ML"} and 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1093
  @{ML_file "Pure/type_infer.ML"}. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1094
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1095
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1096
  \footnote{\bf FIXME: say something about sorts.}
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1097
  \footnote{\bf FIXME: give a ``readmore''.}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1098
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1099
  \begin{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1100
  Check that the function defined in Exercise~\ref{fun:revsum} returns a
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1101
  result that type-checks. See what happens to the  solutions of this 
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1102
  exercise given in Appendix \ref{ch:solutions} when they receive an 
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1103
  ill-typed term as input.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1104
  \end{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1105
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1106
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1107
section {* Certified Terms and Certified Types *}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1108
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1109
text {*
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1110
  You can freely construct and manipulate @{ML_type "term"}s and @{ML_type
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1111
  typ}es, since they are just arbitrary unchecked trees. However, you
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1112
  eventually want to see if a term is well-formed, or type-checks, relative to
369
74ba778b09c9 tuned index
Christian Urban <urbanc@in.tum.de>
parents: 368
diff changeset
  1113
  a theory.  Type-checking is done via the function @{ML_ind cterm_of in Thm}, which
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1114
  converts a @{ML_type term} into a @{ML_type cterm}, a \emph{certified}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1115
  term. Unlike @{ML_type term}s, which are just trees, @{ML_type "cterm"}s are
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1116
  abstract objects that are guaranteed to be type-correct, and they can only
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1117
  be constructed via ``official interfaces''.
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1118
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1119
  Certification is always relative to a theory context. For example you can 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1120
  write:
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1121
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1122
  @{ML_response_fake [display,gray] 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1123
  "cterm_of @{theory} @{term \"(a::nat) + b = c\"}" 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1124
  "a + b = c"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1125
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1126
  This can also be written with an antiquotation:
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1127
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1128
  @{ML_response_fake [display,gray] 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1129
  "@{cterm \"(a::nat) + b = c\"}" 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1130
  "a + b = c"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1131
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1132
  Attempting to obtain the certified term for
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1133
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1134
  @{ML_response_fake_both [display,gray] 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1135
  "@{cterm \"1 + True\"}" 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1136
  "Type unification failed \<dots>"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1137
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1138
  yields an error (since the term is not typable). A slightly more elaborate
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1139
  example that type-checks is:
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1140
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1141
@{ML_response_fake [display,gray] 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1142
"let
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1143
  val natT = @{typ \"nat\"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1144
  val zero = @{term \"0::nat\"}
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1145
  val plus = Const (@{const_name plus}, [natT, natT] ---> natT)
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1146
in
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1147
  cterm_of @{theory} (plus $ zero $ zero)
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1148
end" 
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1149
  "0 + 0"}
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1150
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1151
  In Isabelle not just terms need to be certified, but also types. For example, 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1152
  you obtain the certified type for the Isabelle type @{typ "nat \<Rightarrow> bool"} on 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1153
  the ML-level as follows:
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1154
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1155
  @{ML_response_fake [display,gray]
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1156
  "ctyp_of @{theory} (@{typ nat} --> @{typ bool})"
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1157
  "nat \<Rightarrow> bool"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1158
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1159
  or with the antiquotation:
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1160
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1161
  @{ML_response_fake [display,gray]
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1162
  "@{ctyp \"nat \<Rightarrow> bool\"}"
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1163
  "nat \<Rightarrow> bool"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1164
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1165
  Since certified terms are, unlike terms, abstract objects, we cannot
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1166
  pattern-match against them. However, we can construct them. For example
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1167
  the function @{ML_ind capply in Thm} produces a certified application.
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1168
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1169
  @{ML_response_fake [display,gray]
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1170
  "Thm.capply @{cterm \"P::nat \<Rightarrow> bool\"} @{cterm \"3::nat\"}"
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1171
  "P 3"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1172
351
f118240ab44a updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 350
diff changeset
  1173
  Similarly the function @{ML_ind list_comb in Drule} from the structure @{ML_struct Drule}
f118240ab44a updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 350
diff changeset
  1174
  applies a list of @{ML_type cterm}s.
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1175
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1176
  @{ML_response_fake [display,gray]
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1177
  "let
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1178
  val chead = @{cterm \"P::unit \<Rightarrow> nat \<Rightarrow> bool\"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1179
  val cargs = [@{cterm \"()\"}, @{cterm \"3::nat\"}]
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1180
in
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1181
  Drule.list_comb (chead, cargs)
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1182
end"
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1183
  "P () 3"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1184
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1185
  \begin{readmore}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1186
  For functions related to @{ML_type cterm}s and @{ML_type ctyp}s see 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1187
  the files @{ML_file "Pure/thm.ML"}, @{ML_file "Pure/more_thm.ML"} and
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1188
  @{ML_file "Pure/drule.ML"}.
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1189
  \end{readmore}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1190
*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1191
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1192
section {* Theorems *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1193
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1194
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1195
  Just like @{ML_type cterm}s, theorems are abstract objects of type @{ML_type thm} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1196
  that can only be built by going through interfaces. As a consequence, every proof 
351
f118240ab44a updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 350
diff changeset
  1197
  in Isabelle is correct by construction. This follows the tradition of the LCF approach.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1198
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1199
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1200
  To see theorems in ``action'', let us give a proof on the ML-level for the following 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1201
  statement:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1202
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1203
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1204
  lemma 
351
f118240ab44a updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 350
diff changeset
  1205
    assumes assm\<^isub>1: "\<And>(x::nat). P x \<Longrightarrow> Q x" 
f118240ab44a updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 350
diff changeset
  1206
    and     assm\<^isub>2: "P t"
f118240ab44a updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 350
diff changeset
  1207
    shows "Q t"(*<*)oops(*>*) 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1208
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1209
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1210
  The corresponding ML-code is as follows:
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1211
*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1212
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1213
ML{*val my_thm = 
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1214
let
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1215
  val assm1 = @{cprop "\<And>(x::nat). P x \<Longrightarrow> Q x"}
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1216
  val assm2 = @{cprop "(P::nat \<Rightarrow> bool) t"}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1217
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1218
  val Pt_implies_Qt = 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1219
        assume assm1
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1220
        |> forall_elim @{cterm "t::nat"}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1221
  
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1222
  val Qt = implies_elim Pt_implies_Qt (assume assm2)
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1223
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1224
  Qt 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1225
  |> implies_intr assm2
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1226
  |> implies_intr assm1
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1227
end*}
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1228
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1229
text {* 
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1230
  If we print out the value of @{ML my_thm} then we see only the 
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1231
  final statement of the theorem.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1232
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1233
  @{ML_response_fake [display, gray]
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1234
  "tracing (string_of_thm @{context} my_thm)"
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1235
  "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"}
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1236
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1237
  However, internally the code-snippet constructs the following 
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1238
  proof.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1239
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1240
  \[
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1241
  \infer[(@{text "\<Longrightarrow>"}$-$intro)]{\vdash @{prop "(\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> P t \<Longrightarrow> Q t"}}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1242
    {\infer[(@{text "\<Longrightarrow>"}$-$intro)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "P t \<Longrightarrow> Q t"}}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1243
       {\infer[(@{text "\<Longrightarrow>"}$-$elim)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"}, @{prop "P t"} \vdash @{prop "Q t"}}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1244
          {\infer[(@{text "\<And>"}$-$elim)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "P t \<Longrightarrow> Q t"}}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1245
                 {\infer[(assume)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "\<And>x. P x \<Longrightarrow> Q x"}}{}}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1246
                 &
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1247
           \infer[(assume)]{@{prop "P t"} \vdash @{prop "P t"}}{}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1248
          }
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1249
       }
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1250
    }
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1251
  \]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1252
339
c588e8422737 used a better implementation of \index in Latex; added more to the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 338
diff changeset
  1253
  While we obtained a theorem as result, this theorem is not
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1254
  yet stored in Isabelle's theorem database. Consequently, it cannot be 
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1255
  referenced on the user level. One way to store it in the theorem database is
351
f118240ab44a updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 350
diff changeset
  1256
  by using the function @{ML_ind note in LocalTheory}.\footnote{\bf FIXME: make sure a pointer
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1257
  to the section about local-setup is given earlier.}
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1258
*}
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1259
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1260
local_setup %gray {*
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1261
  LocalTheory.note Thm.theoremK
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1262
     ((@{binding "my_thm"}, []), [my_thm]) #> snd *}
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1263
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1264
text {*
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1265
  The fourth argument of @{ML note in LocalTheory} is the list of theorems we
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1266
  want to store under a name. We can store more than one under a single name. 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1267
  The first argument @{ML_ind theoremK in Thm} is
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1268
  a kind indicator, which classifies the theorem. There are several such kind
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1269
  indicators: for a theorem arising from a definition you should use @{ML_ind
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1270
  definitionK in Thm}, for an axiom @{ML_ind axiomK in Thm}, and for
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1271
  ``normal'' theorems the kinds @{ML_ind theoremK in Thm} or @{ML_ind lemmaK
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1272
  in Thm}.  The second argument of @{ML note in LocalTheory} is the name under
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1273
  which we store the theorem or theorems. The third argument can contain a
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1274
  list of theorem attributes, which we will explain in detail in
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1275
  Section~\ref{sec:attributes}. Below we just use one such attribute for
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1276
  adding the theorem to the simpset:
339
c588e8422737 used a better implementation of \index in Latex; added more to the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 338
diff changeset
  1277
*}
c588e8422737 used a better implementation of \index in Latex; added more to the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 338
diff changeset
  1278
c588e8422737 used a better implementation of \index in Latex; added more to the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 338
diff changeset
  1279
local_setup %gray {*
c588e8422737 used a better implementation of \index in Latex; added more to the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 338
diff changeset
  1280
  LocalTheory.note Thm.theoremK
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1281
    ((@{binding "my_thm_simp"}, 
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1282
       [Attrib.internal (K Simplifier.simp_add)]), [my_thm]) #> snd *}
339
c588e8422737 used a better implementation of \index in Latex; added more to the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 338
diff changeset
  1283
c588e8422737 used a better implementation of \index in Latex; added more to the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 338
diff changeset
  1284
text {*
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1285
  Note that we have to use another name under which the theorem is stored,
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1286
  since Isabelle does not allow us to call  @{ML_ind note in LocalTheory} twice
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1287
  with the same name. The attribute needs to be wrapped inside the function @{ML_ind
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1288
  internal in Attrib} from the structure @{ML_struct Attrib}. If we use the function 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1289
  @{ML get_thm_names_from_ss} from
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1290
  the previous chapter, we can check whether the theorem has actually been
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1291
  added.
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1292
340
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1293
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1294
  @{ML_response [display,gray]
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1295
  "let
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1296
  fun pred s = match_string \"my_thm_simp\" s
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1297
in
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1298
  exists pred (get_thm_names_from_ss @{simpset})
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1299
end"
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1300
  "true"}
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1301
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1302
  The main point of storing the theorems @{thm [source] my_thm} and @{thm
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1303
  [source] my_thm_simp} is that they can now also be referenced with the
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1304
  \isacommand{thm}-command on the user-level of Isabelle
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1305
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1306
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1307
  \begin{isabelle}
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1308
  \isacommand{thm}~@{text "my_thm"}\isanewline
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1309
   @{text ">"}~@{prop "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"}
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1310
  \end{isabelle}
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1311
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1312
  or with the @{text "@{thm \<dots>}"}-antiquotation on the ML-level. Otherwise the 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1313
  user has no access to these theorems. 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1314
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1315
  Recall that Isabelle does not let you call @{ML note in LocalTheory} twice
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1316
  with the same theorem name. In effect, once a theorem is stored under a name, 
358
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1317
  this association is fixed. While this is a ``safety-net'' to make sure a
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1318
  theorem name refers to a particular theorem or collection of theorems, it is 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1319
  also a bit too restrictive in cases where a theorem name should refer to a 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1320
  dynamically expanding list of theorems (like a simpset). Therefore Isabelle 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1321
  also implements a mechanism where a theorem name can refer to a custom theorem 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1322
  list. For this you can use the function @{ML_ind add_thms_dynamic in PureThy}. 
358
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1323
  To see how it works let us assume we defined our own theorem list @{text MyThmList}.
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1324
*}
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1325
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1326
ML{*structure MyThmList = GenericDataFun
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1327
  (type T = thm list
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1328
   val empty = []
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1329
   val extend = I
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1330
   val merge = K (op @))
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1331
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1332
fun update thm = Context.theory_map (MyThmList.map (fn thms => thm::thms))*}
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1333
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1334
text {*
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1335
  The function @{ML update} allows us to update the theorem list, for example
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1336
  by adding the theorem @{thm [source] TrueI}.
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1337
*}
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1338
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1339
setup %gray {* update @{thm TrueI} *}
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1340
 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1341
text {*
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1342
  We can now install the theorem list so that it is visible to the user and 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1343
  can be refered to by a theorem name. For this need to call 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1344
  @{ML_ind add_thms_dynamic in PureThy}
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1345
*}
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1346
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1347
setup %gray {* 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1348
  PureThy.add_thms_dynamic (@{binding "mythmlist"}, MyThmList.get) 
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1349
*}
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1350
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1351
text {*
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1352
  with a name and a function that accesses the theorem list. Now if the
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1353
  user issues the command
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1354
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1355
  \begin{isabelle}
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1356
  \isacommand{thm}~@{text "mythmlist"}\\
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1357
  @{text "> True"}
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1358
  \end{isabelle}
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1359
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1360
  the current content of the theorem list is displayed. If more theorems are stored in 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1361
  the list, say
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1362
*}
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1363
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1364
setup %gray {* update @{thm FalseE} *}
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1365
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1366
text {*
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1367
  then the same command produces
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1368
  
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1369
  \begin{isabelle}
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1370
  \isacommand{thm}~@{text "mythmlist"}\\
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1371
  @{text "> False \<Longrightarrow> ?P"}\\
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1372
  @{text "> True"}
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1373
  \end{isabelle}
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1374
358
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1375
  There is a multitude of functions in the structures @{ML_struct Thm} and @{ML_struct Drule} 
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1376
  for managing or manipulating theorems. For example 
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1377
  we can test theorems for alpha equality. Suppose you proved the following three 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1378
  theorems.
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1379
*}
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1380
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1381
lemma
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1382
  shows thm1: "\<forall>x. P x" 
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1383
  and   thm2: "\<forall>y. P y" 
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1384
  and   thm3: "\<forall>y. Q y" sorry
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1385
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1386
text {*
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1387
  Testing them for alpha equality using the function @{ML_ind eq_thm in Thm} produces:
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1388
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1389
  @{ML_response [display,gray]
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1390
"(Thm.eq_thm (@{thm thm1}, @{thm thm2}),
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1391
 Thm.eq_thm (@{thm thm2}, @{thm thm3}))"
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1392
"(true, false)"}
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1393
340
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1394
  Many functions destruct theorems into @{ML_type cterm}s. For example
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1395
  the functions @{ML_ind lhs_of in Thm} and @{ML_ind rhs_of in Thm} return 
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1396
  the left and right-hand side, respectively, of a meta-equality.
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1397
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1398
  @{ML_response_fake [display,gray]
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1399
  "let
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1400
  val eq = @{thm True_def}
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1401
in
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1402
  (Thm.lhs_of eq, Thm.rhs_of eq) 
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1403
  |> pairself (string_of_cterm @{context})
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1404
end"
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1405
  "(True, (\<lambda>x. x) = (\<lambda>x. x))"}
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1406
340
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1407
  Other function produce terms that can be pattern-matched. 
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1408
  Suppose the following two theorems.
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1409
*}
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1410
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1411
lemma  
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1412
  shows foo_test1: "A \<Longrightarrow> B \<Longrightarrow> C" 
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1413
  and   foo_test2: "A \<longrightarrow> B \<longrightarrow> C" sorry
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1414
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1415
text {*
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1416
  We can destruct them into premises and conclusions as follows. 
340
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1417
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1418
 @{ML_response_fake [display,gray]
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1419
"let
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1420
  val ctxt = @{context}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1421
  fun prems_and_concl thm =
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1422
     [\"Premises: \"   ^ (string_of_terms ctxt (Thm.prems_of thm))] @ 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1423
     [\"Conclusion: \" ^ (string_of_term ctxt (Thm.concl_of thm))]
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1424
     |> cat_lines
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1425
     |> tracing
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1426
in
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1427
  prems_and_concl @{thm foo_test1};
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1428
  prems_and_concl @{thm foo_test2}
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1429
end"
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1430
"Premises: ?A, ?B
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1431
Conclusion: ?C
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1432
Premises: 
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1433
Conclusion: ?A \<longrightarrow> ?B \<longrightarrow> ?C"}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1434
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1435
  Note that in the second case, there is no premise.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1436
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1437
  \begin{readmore}
358
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1438
  The basic functions for theorems are defined in
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1439
  @{ML_file "Pure/thm.ML"}, @{ML_file "Pure/more_thm.ML"} and @{ML_file "Pure/drule.ML"}. 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1440
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1441
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1442
  The simplifier can be used to unfold definition in theorems. To show
382
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
  1443
  this, we build the theorem @{term "True \<equiv> True"} (Line 1) and then 
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1444
  unfold the constant @{term "True"} according to its definition (Line 2).
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1445
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1446
  @{ML_response_fake [display,gray,linenos]
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1447
  "Thm.reflexive @{cterm \"True\"}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1448
  |> Simplifier.rewrite_rule [@{thm True_def}]
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1449
  |> string_of_thm @{context}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1450
  |> tracing"
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1451
  "(\<lambda>x. x) = (\<lambda>x. x) \<equiv> (\<lambda>x. x) = (\<lambda>x. x)"}
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1452
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1453
  Often it is necessary to transform theorems to and from the object 
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1454
  logic, that is replacing all @{text "\<longrightarrow>"} and @{text "\<forall>"} by @{text "\<Longrightarrow>"} 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1455
  and @{text "\<And>"}, or the other way around.  A reason for such a transformation 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1456
  might be stating a definition. The reason is that definitions can only be 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1457
  stated using object logic connectives, while theorems using the connectives 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1458
  from the meta logic are more convenient for reasoning. Therefore there are
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1459
  some build in functions which help with these transformations. The function 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1460
  @{ML_ind rulify in ObjectLogic} 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1461
  replaces all object connectives by equivalents in the meta logic. For example
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1462
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1463
  @{ML_response_fake [display, gray]
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1464
  "ObjectLogic.rulify @{thm foo_test2}"
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1465
  "\<lbrakk>?A; ?B\<rbrakk> \<Longrightarrow> ?C"}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1466
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1467
  The transformation in the other direction can be achieved with function
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1468
  @{ML_ind atomize in ObjectLogic} and the following code.
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1469
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1470
  @{ML_response_fake [display,gray]
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1471
  "let
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1472
  val thm = @{thm foo_test1}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1473
  val meta_eq = ObjectLogic.atomize (cprop_of thm)
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1474
in
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1475
  MetaSimplifier.rewrite_rule [meta_eq] thm
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1476
end"
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1477
  "?A \<longrightarrow> ?B \<longrightarrow> ?C"}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1478
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1479
  In this code the function @{ML atomize in ObjectLogic} produces 
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1480
  a meta-equation between the given theorem and the theorem transformed
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1481
  into the object logic. The result is the theorem with object logic 
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1482
  connectives. However, in order to completely transform a theorem
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1483
  involving meta variables, such as @{thm [source] list.induct}, which 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1484
  is of the form 
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1485
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1486
  @{thm [display] list.induct}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1487
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1488
  we have to first abstract over the meta variables @{text "?P"} and 
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1489
  @{text "?list"}. For this we can use the function 
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1490
  @{ML_ind forall_intr_vars in Drule}. This allows us to implement the
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1491
  following function for atomizing a theorem.
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1492
*}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1493
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1494
ML{*fun atomize_thm thm =
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1495
let
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1496
  val thm' = forall_intr_vars thm
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1497
  val thm'' = ObjectLogic.atomize (cprop_of thm')
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1498
in
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1499
  MetaSimplifier.rewrite_rule [thm''] thm'
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1500
end*}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1501
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1502
text {*
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1503
  This function produces for the theorem @{thm [source] list.induct}
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1504
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1505
  @{ML_response_fake [display, gray]
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1506
  "atomize_thm @{thm list.induct}"
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1507
  "\<forall>P list. P [] \<longrightarrow> (\<forall>a list. P list \<longrightarrow> P (a # list)) \<longrightarrow> P list"}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1508
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1509
  Theorems can also be produced from terms by giving an explicit proof. 
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1510
  One way to achieve this is by using the function @{ML_ind prove in Goal}
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1511
  in the structure @{ML_struct Goal}. For example below we use this function
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1512
  to prove the term @{term "P \<Longrightarrow> P"}.
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1513
  
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1514
  @{ML_response_fake [display,gray]
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1515
  "let
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1516
  val trm = @{term \"P \<Longrightarrow> P::bool\"}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1517
  val tac = K (atac 1)
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1518
in
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1519
  Goal.prove @{context} [\"P\"] [] trm tac
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1520
end"
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1521
  "?P \<Longrightarrow> ?P"}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1522
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1523
  This function takes first a context and second a list of strings. This list
359
be6538c7b41d polished
Christian Urban <urbanc@in.tum.de>
parents: 358
diff changeset
  1524
  specifies which variables should be turned into schematic variables once the term
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1525
  is proved.  The fourth argument is the term to be proved. The fifth is a
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1526
  corresponding proof given in form of a tactic (we explain tactics in
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1527
  Chapter~\ref{chp:tactical}). In the code above, the tactic proves the theorem
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1528
  by assumption. As before this code will produce a theorem, but the theorem
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1529
  is not yet stored in the theorem database. 
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1530
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1531
  Theorems also contain auxiliary data, such as the name of the theorem, its
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1532
  kind, the names for cases and so on. This data is stored in a string-string
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1533
  list and can be retrieved with the function @{ML_ind get_tags in
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1534
  Thm}. Assume you prove the following lemma.
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1535
*}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1536
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1537
lemma foo_data: 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1538
  shows "P \<Longrightarrow> P \<Longrightarrow> P" by assumption
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1539
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1540
text {*
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1541
  The auxiliary data of this lemma can be retrieved using the function 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1542
  @{ML_ind get_tags in Thm}. So far the the auxiliary data of this lemma is 
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1543
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1544
  @{ML_response_fake [display,gray]
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1545
  "Thm.get_tags @{thm foo_data}"
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1546
  "[(\"name\", \"General.foo_data\"), (\"kind\", \"lemma\")]"}
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1547
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1548
  consisting of a name and a kind.  When we store lemmas in the theorem database, 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1549
  we might want to explicitly extend this data by attaching case names to the 
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
  1550
  two premises of the lemma.  This can be done with the function @{ML_ind name in Rule_Cases}
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
  1551
  from the structure @{ML_struct Rule_Cases}.
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1552
*}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1553
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1554
local_setup %gray {*
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1555
  LocalTheory.note Thm.lemmaK
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1556
    ((@{binding "foo_data'"}, []), 
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
  1557
      [(Rule_Cases.name ["foo_case_one", "foo_case_two"] 
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1558
        @{thm foo_data})]) #> snd *}
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1559
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1560
text {*
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1561
  The data of the theorem @{thm [source] foo_data'} is then as follows:
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1562
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1563
  @{ML_response_fake [display,gray]
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1564
  "Thm.get_tags @{thm foo_data'}"
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1565
  "[(\"name\", \"General.foo_data'\"), (\"kind\", \"lemma\"), 
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1566
 (\"case_names\", \"foo_case_one;foo_case_two\")]"}
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1567
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1568
  You can observe the case names of this lemma on the user level when using 
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1569
  the proof methods @{text cases} and @{text induct}. In the proof below
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1570
*}
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1571
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1572
lemma
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1573
  shows "Q \<Longrightarrow> Q \<Longrightarrow> Q"
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1574
proof (cases rule: foo_data')
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1575
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1576
(*<*)oops(*>*)
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1577
text_raw{*
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1578
\begin{tabular}{@ {}l}
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1579
\isacommand{print\_cases}\\
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1580
@{text "> cases:"}\\
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1581
@{text ">   foo_case_one:"}\\
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1582
@{text ">     let \"?case\" = \"?P\""}\\
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1583
@{text ">   foo_case_two:"}\\
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1584
@{text ">     let \"?case\" = \"?P\""}
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1585
\end{tabular}*}
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1586
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1587
text {*
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1588
  we can proceed by analysing the cases @{text "foo_case_one"} and 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1589
  @{text "foo_case_two"}. While if the theorem has no names, then
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1590
  the cases have standard names @{text 1}, @{text 2} and so 
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1591
  on. This can be seen in the proof below.
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1592
*}
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1593
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1594
lemma
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1595
  shows "Q \<Longrightarrow> Q \<Longrightarrow> Q"
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1596
proof (cases rule: foo_data)
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1597
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1598
(*<*)oops(*>*)
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1599
text_raw{*
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1600
\begin{tabular}{@ {}l}
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1601
\isacommand{print\_cases}\\
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1602
@{text "> cases:"}\\
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1603
@{text ">   1:"}\\
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1604
@{text ">     let \"?case\" = \"?P\""}\\
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1605
@{text ">   2:"}\\
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1606
@{text ">     let \"?case\" = \"?P\""}
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1607
\end{tabular}*}
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1608
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1609
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1610
text {*
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1611
  One great feature of Isabelle is its document preparation system, where
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1612
  proved theorems can be quoted in documents referencing directly their
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1613
  formalisation. This helps tremendously to minimise cut-and-paste errors. However,
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1614
  sometimes the verbatim quoting is not what is wanted or what can be shown to
354
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1615
  readers. For such situations Isabelle allows the installation of \emph{\index*{theorem 
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1616
  styles}}. These are, roughly speaking, functions from terms to terms. The input 
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1617
  term stands for the theorem to be presented; the output can be constructed to
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1618
  ones wishes.  Let us, for example, assume we want to quote theorems without
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1619
  leading @{text \<forall>}-quantifiers. For this we can implement the following function 
358
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1620
  that strips off @{text "\<forall>"}s.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1621
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1622
358
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1623
ML %linenosgray{*fun strip_allq (Const (@{const_name "All"}, _) $ Abs body) = 
354
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1624
      Term.dest_abs body |> snd |> strip_allq
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1625
  | strip_allq (Const (@{const_name "Trueprop"}, _) $ t) = 
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1626
      strip_allq t
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1627
  | strip_allq t = t*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1628
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1629
text {*
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1630
  We use in Line 2 the function @{ML_ind dest_abs in Term} for deconstructing abstractions,
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1631
  since this function deals correctly with potential name clashes. This function produces
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1632
  a pair consisting of the variable and the body of the abstraction. We are only interested
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1633
  in the body, which we feed into the recursive call. In Line 3 and 4, we also
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1634
  have to explicitly strip of the outermost @{term Trueprop}-coercion. Now we can 
354
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1635
  install this function as the theorem style named @{text "my_strip_allq"}. 
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1636
*}
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1637
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1638
setup %gray {*
354
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1639
  Term_Style.setup "my_strip_allq" (Scan.succeed (K strip_allq)) 
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1640
*}
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1641
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1642
text {*
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1643
  We can test this theorem style with the following theorem
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1644
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1645
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1646
theorem style_test:
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1647
  shows "\<forall>x y z. (x, x) = (y, z)" sorry
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1648
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1649
text {*
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1650
  Now printing out in a document the theorem @{thm [source] style_test} normally
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1651
  using @{text "@{thm \<dots>}"} produces
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1652
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1653
  \begin{isabelle}
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1654
  @{text "@{thm style_test}"}\\
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1655
  @{text ">"}~@{thm style_test}
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1656
  \end{isabelle}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1657
354
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1658
  as expected. But with the theorem style @{text "@{thm (my_strip_allq) \<dots>}"} 
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1659
  we obtain
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1660
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1661
  \begin{isabelle}
354
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1662
  @{text "@{thm (my_strip_allq) style_test}"}\\
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1663
  @{text ">"}~@{thm (my_strip_allq) style_test}\\
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1664
  \end{isabelle}
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1665
  
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1666
  without the leading quantifiers. We can improve this theorem style by explicitly 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1667
  giving a list of strings that should be used for the replacement of the
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1668
  variables. For this we implement the function which takes a list of strings
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1669
  and uses them as name in the outermost abstractions.
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1670
*}
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1671
358
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1672
ML{*fun rename_allq [] t = t
354
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1673
  | rename_allq (x::xs) (Const (@{const_name "All"}, U) $ Abs (_, T, t)) = 
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1674
      Const (@{const_name "All"}, U) $ Abs (x, T, rename_allq xs t)
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1675
  | rename_allq xs (Const (@{const_name "Trueprop"}, U) $ t) =
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1676
      rename_allq xs t
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1677
  | rename_allq _ t = t*}
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1678
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1679
text {*
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1680
  We can now install a the modified theorem style as follows
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1681
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1682
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1683
setup %gray {* let
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1684
  val parser = Scan.repeat Args.name
354
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1685
  fun action xs = K (rename_allq xs #> strip_allq)
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1686
in
354
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1687
  Term_Style.setup "my_strip_allq2" (parser >> action)
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1688
end *}
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1689
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1690
text {*
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1691
  The parser reads a list of names. In the function @{text action} we first
354
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1692
  call @{ML rename_allq} with the parsed list, then we call @{ML strip_allq}
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1693
  on the resulting term. We can now suggest, for example, two variables for
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1694
  stripping off the first two @{text \<forall>}-quantifiers.
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1695
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1696
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1697
  \begin{isabelle}
354
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1698
  @{text "@{thm (my_strip_allq2 x' x'') style_test}"}\\
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  1699
  @{text ">"}~@{thm (my_strip_allq2 x' x'') style_test}
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1700
  \end{isabelle}
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1701
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1702
  Such theorem styles allow one to print out theorems in documents formatted to
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1703
  ones heart content. Next we explain theorem attributes, which is another
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1704
  mechanism for dealing with theorems.
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1705
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1706
  \begin{readmore}
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1707
  Theorem styles are implemented in @{ML_file "Pure/Thy/term_style.ML"}.
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1708
  \end{readmore}
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1709
*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1710
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1711
section {* Theorem Attributes\label{sec:attributes} *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1712
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1713
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1714
  Theorem attributes are @{text "[symmetric]"}, @{text "[THEN \<dots>]"}, @{text
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1715
  "[simp]"} and so on. Such attributes are \emph{neither} tags \emph{nor} flags
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1716
  annotated to theorems, but functions that do further processing of 
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1717
  theorems. In particular, it is not possible to find out
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1718
  what are all theorems that have a given attribute in common, unless of course
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1719
  the function behind the attribute stores the theorems in a retrievable 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1720
  data structure. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1721
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1722
  If you want to print out all currently known attributes a theorem can have, 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1723
  you can use the Isabelle command
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1724
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1725
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1726
  \isacommand{print\_attributes}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1727
  @{text "> COMP:  direct composition with rules (no lifting)"}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1728
  @{text "> HOL.dest:  declaration of Classical destruction rule"}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1729
  @{text "> HOL.elim:  declaration of Classical elimination rule"}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1730
  @{text "> \<dots>"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1731
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1732
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1733
  The theorem attributes fall roughly into two categories: the first category manipulates
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1734
  theorems (for example @{text "[symmetric]"} and @{text "[THEN \<dots>]"}), and the second
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1735
  stores theorems somewhere as data (for example @{text "[simp]"}, which adds
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1736
  theorems to the current simpset).
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1737
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1738
  To explain how to write your own attribute, let us start with an extremely simple 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1739
  version of the attribute @{text "[symmetric]"}. The purpose of this attribute is
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1740
  to produce the ``symmetric'' version of an equation. The main function behind 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1741
  this attribute is
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1742
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1743
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1744
ML{*val my_symmetric = Thm.rule_attribute (fn _ => fn thm => thm RS @{thm sym})*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1745
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1746
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1747
  where the function @{ML_ind  rule_attribute in Thm} expects a function taking a 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1748
  context (which we ignore in the code above) and a theorem (@{text thm}), and 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1749
  returns another theorem (namely @{text thm} resolved with the theorem 
363
Christian Urban <urbanc@in.tum.de>
parents: 361
diff changeset
  1750
  @{thm [source] sym}: @{thm sym[no_vars]}; the function @{ML_ind RS in Drule} 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1751
  is explained in Section~\ref{sec:simpletacs}). The function 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1752
  @{ML rule_attribute in Thm} then returns  an attribute.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1753
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1754
  Before we can use the attribute, we need to set it up. This can be done
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1755
  using the Isabelle command \isacommand{attribute\_setup} as follows:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1756
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1757
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1758
attribute_setup %gray my_sym = 
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1759
  {* Scan.succeed my_symmetric *} "applying the sym rule"
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1760
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1761
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1762
  Inside the @{text "\<verbopen> \<dots> \<verbclose>"}, we have to specify a parser
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1763
  for the theorem attribute. Since the attribute does not expect any further 
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1764
  arguments (unlike @{text "[THEN \<dots>]"}, for instance), we use the parser @{ML
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1765
  Scan.succeed}. An example for the attribute @{text "[my_sym]"} is the proof
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1766
*} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1767
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1768
lemma test[my_sym]: "2 = Suc (Suc 0)" by simp
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1769
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1770
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1771
  which stores the theorem @{thm test} under the name @{thm [source] test}. You
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1772
  can see this, if you query the lemma: 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1773
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1774
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1775
  \isacommand{thm}~@{text "test"}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1776
  @{text "> "}~@{thm test}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1777
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1778
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1779
  We can also use the attribute when referring to this theorem:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1780
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1781
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1782
  \isacommand{thm}~@{text "test[my_sym]"}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1783
  @{text "> "}~@{thm test[my_sym]}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1784
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1785
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1786
  An alternative for setting up an attribute is the function @{ML_ind  setup in Attrib}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1787
  So instead of using \isacommand{attribute\_setup}, you can also set up the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1788
  attribute as follows:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1789
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1790
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1791
ML{*Attrib.setup @{binding "my_sym"} (Scan.succeed my_symmetric)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1792
  "applying the sym rule" *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1793
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1794
text {*
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1795
  This gives a function from @{ML_type "theory -> theory"}, which
361
Christian Urban <urbanc@in.tum.de>
parents: 360
diff changeset
  1796
  can be used for example with \isacommand{setup} or with
368
b1a458a03a8e new parts in the tactical section
Christian Urban <urbanc@in.tum.de>
parents: 363
diff changeset
  1797
  @{ML "Context.>> o Context.map_theory"}.\footnote{\bf FIXME: explain what happens here.}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1798
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1799
  As an example of a slightly more complicated theorem attribute, we implement 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1800
  our own version of @{text "[THEN \<dots>]"}. This attribute will take a list of theorems
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1801
  as argument and resolve the theorem with this list (one theorem 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1802
  after another). The code for this attribute is
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1803
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1804
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1805
ML{*fun MY_THEN thms = 
368
b1a458a03a8e new parts in the tactical section
Christian Urban <urbanc@in.tum.de>
parents: 363
diff changeset
  1806
  Thm.rule_attribute 
b1a458a03a8e new parts in the tactical section
Christian Urban <urbanc@in.tum.de>
parents: 363
diff changeset
  1807
    (fn _ => fn thm => fold (curry ((op RS) o swap)) thms thm)*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1808
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1809
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1810
  where @{ML swap} swaps the components of a pair. The setup of this theorem
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1811
  attribute uses the parser @{ML thms in Attrib}, which parses a list of
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1812
  theorems. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1813
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1814
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1815
attribute_setup %gray MY_THEN = {* Attrib.thms >> MY_THEN *} 
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1816
  "resolving the list of theorems with the theorem"
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1817
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1818
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1819
  You can, for example, use this theorem attribute to turn an equation into a 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1820
  meta-equation:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1821
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1822
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1823
  \isacommand{thm}~@{text "test[MY_THEN eq_reflection]"}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1824
  @{text "> "}~@{thm test[MY_THEN eq_reflection]}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1825
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1826
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1827
  If you need the symmetric version as a meta-equation, you can write
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1828
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1829
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1830
  \isacommand{thm}~@{text "test[MY_THEN sym eq_reflection]"}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1831
  @{text "> "}~@{thm test[MY_THEN sym eq_reflection]}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1832
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1833
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1834
  It is also possible to combine different theorem attributes, as in:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1835
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1836
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1837
  \isacommand{thm}~@{text "test[my_sym, MY_THEN eq_reflection]"}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1838
  @{text "> "}~@{thm test[my_sym, MY_THEN eq_reflection]}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1839
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1840
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1841
  However, here also a weakness of the concept
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1842
  of theorem attributes shows through: since theorem attributes can be
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1843
  arbitrary functions, they do not commute in general. If you try
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1844
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1845
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1846
  \isacommand{thm}~@{text "test[MY_THEN eq_reflection, my_sym]"}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1847
  @{text "> "}~@{text "exception THM 1 raised: RSN: no unifiers"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1848
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1849
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1850
  you get an exception indicating that the theorem @{thm [source] sym}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1851
  does not resolve with meta-equations. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1852
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1853
  The purpose of @{ML_ind rule_attribute in Thm} is to directly manipulate
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1854
  theorems.  Another usage of theorem attributes is to add and delete theorems
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1855
  from stored data.  For example the theorem attribute @{text "[simp]"} adds
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1856
  or deletes a theorem from the current simpset. For these applications, you
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1857
  can use @{ML_ind declaration_attribute in Thm}. To illustrate this function,
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1858
  let us introduce a theorem list.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1859
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1860
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1861
ML{*structure MyThms = Named_Thms
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1862
  (val name = "attr_thms" 
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1863
   val description = "Theorems for an Attribute") *}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1864
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1865
text {* 
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1866
  We are going to modify this list by adding and deleting theorems.
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1867
  For this we use the two functions @{ML MyThms.add_thm} and
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1868
  @{ML MyThms.del_thm}. You can turn them into attributes 
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1869
  with the code
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1870
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1871
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1872
ML{*val my_add = Thm.declaration_attribute MyThms.add_thm
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1873
val my_del = Thm.declaration_attribute MyThms.del_thm *}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1874
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1875
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1876
  and set up the attributes as follows
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1877
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1878
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1879
attribute_setup %gray my_thms = {* Attrib.add_del my_add my_del *} 
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1880
  "maintaining a list of my_thms" 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1881
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1882
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1883
  The parser @{ML_ind  add_del in Attrib} is a predefined parser for 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1884
  adding and deleting lemmas. Now if you prove the next lemma 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1885
  and attach to it the attribute @{text "[my_thms]"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1886
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1887
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1888
lemma trueI_2[my_thms]: "True" by simp
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1889
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1890
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1891
  then you can see it is added to the initially empty list.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1892
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1893
  @{ML_response_fake [display,gray]
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1894
  "MyThms.get @{context}" 
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1895
  "[\"True\"]"}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1896
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1897
  You can also add theorems using the command \isacommand{declare}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1898
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1899
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1900
declare test[my_thms] trueI_2[my_thms add]
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1901
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1902
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1903
  With this attribute, the @{text "add"} operation is the default and does 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1904
  not need to be explicitly given. These three declarations will cause the 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1905
  theorem list to be updated as:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1906
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1907
  @{ML_response_fake [display,gray]
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1908
  "MyThms.get @{context}"
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1909
  "[\"True\", \"Suc (Suc 0) = 2\"]"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1910
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1911
  The theorem @{thm [source] trueI_2} only appears once, since the 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1912
  function @{ML_ind  add_thm in Thm} tests for duplicates, before extending
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1913
  the list. Deletion from the list works as follows:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1914
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1915
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1916
declare test[my_thms del]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1917
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1918
text {* After this, the theorem list is again: 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1919
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1920
  @{ML_response_fake [display,gray]
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1921
  "MyThms.get @{context}"
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1922
  "[\"True\"]"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1923
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1924
  We used in this example two functions declared as @{ML_ind
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1925
  declaration_attribute in Thm}, but there can be any number of them. We just
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1926
  have to change the parser for reading the arguments accordingly.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1927
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1928
  \footnote{\bf FIXME What are: @{text "theory_attributes"}, @{text "proof_attributes"}?}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1929
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1930
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1931
  FIXME: @{ML_file "Pure/more_thm.ML"}; parsers for attributes is in 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1932
  @{ML_file "Pure/Isar/attrib.ML"}...also explained in the chapter about
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1933
  parsing.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1934
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1935
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1936
358
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1937
section {* Theories\label{sec:theories} (TBD) *}
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1938
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1939
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1940
text {*
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1941
  Theories are the most fundamental building blocks in Isabelle. They 
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1942
  contain definitions, syntax declarations, axioms, proofs etc. If a definition
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1943
  is stated, it must be stored in a theory in order to be usable later
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1944
  on. Similar with proofs: once a proof is finished, the proved theorem
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1945
  needs to be stored in the theorem database of the theory in order to
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1946
  be usable. All relevant data of a theort can be querried as follows.
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1947
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1948
  \begin{isabelle}
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1949
  \isacommand{print\_theory}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1950
  @{text "> names: Pure Code_Generator HOL \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1951
  @{text "> classes: Inf < type \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1952
  @{text "> default sort: type"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1953
  @{text "> syntactic types: #prop \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1954
  @{text "> logical types: 'a \<times> 'b \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1955
  @{text "> type arities: * :: (random, random) random \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1956
  @{text "> logical constants: == :: 'a \<Rightarrow> 'a \<Rightarrow> prop \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1957
  @{text "> abbreviations: \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1958
  @{text "> axioms: \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1959
  @{text "> oracles: \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1960
  @{text "> definitions: \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1961
  @{text "> theorems: \<dots>"}
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1962
  \end{isabelle}
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1963
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1964
  \begin{center}
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1965
  \begin{tikzpicture}
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1966
  \node[top color=white, bottom color=gray!30, draw=black!100, drop shadow] {A};
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1967
  \end{tikzpicture}
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1968
  \end{center}
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1969
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1970
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1971
  In contrast to an ordinary theory, which simply consists of a type
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1972
  signature, as well as tables for constants, axioms and theorems, a local
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1973
  theory contains additional context information, such as locally fixed
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1974
  variables and local assumptions that may be used by the package. The type
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1975
  @{ML_type local_theory} is identical to the type of \emph{proof contexts}
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1976
  @{ML_type "Proof.context"}, although not every proof context constitutes a
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1977
  valid local theory.
361
Christian Urban <urbanc@in.tum.de>
parents: 360
diff changeset
  1978
Christian Urban <urbanc@in.tum.de>
parents: 360
diff changeset
  1979
  @{ML "Context.>> o Context.map_theory"}
358
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1980
*}
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1981
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1982
section {* Setups (TBD) *}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1983
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1984
text {*
361
Christian Urban <urbanc@in.tum.de>
parents: 360
diff changeset
  1985
  @{ML Sign.declare_const}
Christian Urban <urbanc@in.tum.de>
parents: 360
diff changeset
  1986
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1987
  In the previous section we used \isacommand{setup} in order to make
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1988
  a theorem attribute known to Isabelle. What happens behind the scenes
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1989
  is that \isacommand{setup} expects a function of type 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1990
  @{ML_type "theory -> theory"}: the input theory is the current theory and the 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1991
  output the theory where the theory attribute has been stored.
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1992
  
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1993
  This is a fundamental principle in Isabelle. A similar situation occurs 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1994
  for example with declaring constants. The function that declares a 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1995
  constant on the ML-level is @{ML_ind  add_consts_i in Sign}. 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1996
  If you write\footnote{Recall that ML-code  needs to be 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1997
  enclosed in \isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"}.} 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1998
*}  
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1999
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2000
ML{*Sign.add_consts_i [(@{binding "BAR"}, @{typ "nat"}, NoSyn)] @{theory} *}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2001
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2002
text {*
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2003
  for declaring the constant @{text "BAR"} with type @{typ nat} and 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2004
  run the code, then you indeed obtain a theory as result. But if you 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2005
  query the constant on the Isabelle level using the command \isacommand{term}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2006
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2007
  \begin{isabelle}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2008
  \isacommand{term}~@{text [quotes] "BAR"}\\
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2009
  @{text "> \"BAR\" :: \"'a\""}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2010
  \end{isabelle}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2011
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2012
  you do not obtain a constant of type @{typ nat}, but a free variable (printed in 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2013
  blue) of polymorphic type. The problem is that the ML-expression above did 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2014
  not register the declaration with the current theory. This is what the command
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2015
  \isacommand{setup} is for. The constant is properly declared with
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2016
*}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2017
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2018
setup %gray {* Sign.add_consts_i [(@{binding "BAR"}, @{typ "nat"}, NoSyn)] *}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2019
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2020
text {* 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2021
  Now 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2022
  
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2023
  \begin{isabelle}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2024
  \isacommand{term}~@{text [quotes] "BAR"}\\
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2025
  @{text "> \"BAR\" :: \"nat\""}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2026
  \end{isabelle}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2027
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2028
  returns a (black) constant with the type @{typ nat}.
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2029
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2030
  A similar command is \isacommand{local\_setup}, which expects a function
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2031
  of type @{ML_type "local_theory -> local_theory"}. Later on we will also
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2032
  use the commands \isacommand{method\_setup} for installing methods in the
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2033
  current theory and \isacommand{simproc\_setup} for adding new simprocs to
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2034
  the current simpset.
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  2035
*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2036
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  2037
section {* Contexts (TBD) *}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  2038
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  2039
section {* Local Theories (TBD) *}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  2040
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2041
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2042
(*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2043
setup {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2044
 Sign.add_consts_i [(Binding"bar", @{typ "nat"},NoSyn)] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2045
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2046
lemma "bar = (1::nat)"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2047
  oops
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2048
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2049
setup {*   
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2050
  Sign.add_consts_i [("foo", @{typ "nat"},NoSyn)] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2051
 #> PureThy.add_defs false [((@{binding "foo_def"}, 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2052
       Logic.mk_equals (Const ("FirstSteps.foo", @{typ "nat"}), @{term "1::nat"})), [])] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2053
 #> snd
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2054
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2055
*)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2056
(*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2057
lemma "foo = (1::nat)"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2058
  apply(simp add: foo_def)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2059
  done
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2060
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2061
thm foo_def
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2062
*)
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2063
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2064
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2065
section {* Pretty-Printing\label{sec:pretty} *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2066
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2067
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2068
  So far we printed out only plain strings without any formatting except for
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2069
  occasional explicit line breaks using @{text [quotes] "\\n"}. This is
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2070
  sufficient for ``quick-and-dirty'' printouts. For something more
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2071
  sophisticated, Isabelle includes an infrastructure for properly formatting
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2072
  text. Most of its functions do not operate on @{ML_type string}s, but on
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2073
  instances of the pretty type:
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2074
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2075
  @{ML_type_ind [display, gray] "Pretty.T"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2076
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2077
  The function @{ML str in Pretty} transforms a (plain) string into such a pretty 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2078
  type. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2079
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2080
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2081
  "Pretty.str \"test\"" "String (\"test\", 4)"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2082
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2083
  where the result indicates that we transformed a string with length 4. Once
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2084
  you have a pretty type, you can, for example, control where linebreaks may
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2085
  occur in case the text wraps over a line, or with how much indentation a
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2086
  text should be printed.  However, if you want to actually output the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2087
  formatted text, you have to transform the pretty type back into a @{ML_type
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2088
  string}. This can be done with the function @{ML_ind  string_of in Pretty}. In what
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2089
  follows we will use the following wrapper function for printing a pretty
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2090
  type:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2091
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2092
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2093
ML{*fun pprint prt = tracing (Pretty.string_of prt)*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2094
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2095
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2096
  The point of the pretty-printing infrastructure is to give hints about how to
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2097
  layout text and let Isabelle do the actual layout. Let us first explain
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2098
  how you can insert places where a line break can occur. For this assume the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2099
  following function that replicates a string n times:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2100
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2101
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2102
ML{*fun rep n str = implode (replicate n str) *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2103
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2104
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2105
  and suppose we want to print out the string:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2106
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2107
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2108
ML{*val test_str = rep 8 "fooooooooooooooobaaaaaaaaaaaar "*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2109
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2110
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2111
  We deliberately chose a large string so that it spans over more than one line. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2112
  If we print out the string using the usual ``quick-and-dirty'' method, then
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2113
  we obtain the ugly output:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2114
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2115
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2116
"tracing test_str"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2117
"fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar foooooooooo
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2118
ooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaa
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2119
aaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fo
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2120
oooooooooooooobaaaaaaaaaaaar"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2121
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2122
  We obtain the same if we just use the function @{ML pprint}.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2123
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2124
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2125
"pprint (Pretty.str test_str)"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2126
"fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar foooooooooo
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2127
ooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaa
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2128
aaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fo
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2129
oooooooooooooobaaaaaaaaaaaar"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2130
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2131
  However by using pretty types you have the ability to indicate possible
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2132
  linebreaks for example at each whitespace. You can achieve this with the
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2133
  function @{ML_ind breaks in Pretty}, which expects a list of pretty types
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2134
  and inserts a possible line break in between every two elements in this
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2135
  list. To print this list of pretty types as a single string, we concatenate
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2136
  them with the function @{ML_ind blk in Pretty} as follows:
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2137
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2138
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2139
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2140
  val ptrs = map Pretty.str (space_explode \" \" test_str)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2141
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2142
  pprint (Pretty.blk (0, Pretty.breaks ptrs))
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2143
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2144
"fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2145
fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2146
fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2147
fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2148
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2149
  Here the layout of @{ML test_str} is much more pleasing to the 
360
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
  2150
  eye. The @{ML "0"} in @{ML_ind  blk in Pretty} stands for no hanging 
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
  2151
  indentation of the printed string. You can increase the indentation 
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
  2152
  and obtain
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2153
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2154
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2155
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2156
  val ptrs = map Pretty.str (space_explode \" \" test_str)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2157
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2158
  pprint (Pretty.blk (3, Pretty.breaks ptrs))
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2159
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2160
"fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2161
   fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2162
   fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2163
   fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2164
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2165
  where starting from the second line the indent is 3. If you want
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2166
  that every line starts with the same indent, you can use the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2167
  function @{ML_ind  indent in Pretty} as follows:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2168
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2169
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2170
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2171
  val ptrs = map Pretty.str (space_explode \" \" test_str)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2172
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2173
  pprint (Pretty.indent 10 (Pretty.blk (0, Pretty.breaks ptrs)))
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2174
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2175
"          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2176
          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2177
          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2178
          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2179
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2180
  If you want to print out a list of items separated by commas and 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2181
  have the linebreaks handled properly, you can use the function 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2182
  @{ML_ind  commas in Pretty}. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2183
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2184
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2185
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2186
  val ptrs = map (Pretty.str o string_of_int) (99998 upto 100020)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2187
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2188
  pprint (Pretty.blk (0, Pretty.commas ptrs))
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2189
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2190
"99998, 99999, 100000, 100001, 100002, 100003, 100004, 100005, 100006, 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2191
100007, 100008, 100009, 100010, 100011, 100012, 100013, 100014, 100015, 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2192
100016, 100017, 100018, 100019, 100020"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2193
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2194
  where @{ML upto} generates a list of integers. You can print out this
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2195
  list as a ``set'', that means enclosed inside @{text [quotes] "{"} and
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2196
  @{text [quotes] "}"}, and separated by commas using the function
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2197
  @{ML_ind  enum in Pretty}. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2198
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2199
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2200
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2201
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2202
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2203
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2204
  val ptrs = map (Pretty.str o string_of_int) (99998 upto 100020)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2205
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2206
  pprint (Pretty.enum \",\" \"{\" \"}\" ptrs)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2207
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2208
"{99998, 99999, 100000, 100001, 100002, 100003, 100004, 100005, 100006, 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2209
  100007, 100008, 100009, 100010, 100011, 100012, 100013, 100014, 100015, 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2210
  100016, 100017, 100018, 100019, 100020}"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2211
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2212
  As can be seen, this function prints out the ``set'' so that starting 
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2213
  from the second, each new line has an indentation of 2.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2214
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2215
  If you print out something that goes beyond the capabilities of the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2216
  standard functions, you can do relatively easily the formatting
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2217
  yourself. Assume you want to print out a list of items where like in ``English''
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2218
  the last two items are separated by @{text [quotes] "and"}. For this you can
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2219
  write the function
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2220
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2221
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2222
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2223
ML %linenosgray{*fun and_list [] = []
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2224
  | and_list [x] = [x]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2225
  | and_list xs = 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2226
      let 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2227
        val (front, last) = split_last xs
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2228
      in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2229
        (Pretty.commas front) @ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2230
        [Pretty.brk 1, Pretty.str "and", Pretty.brk 1, last]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2231
      end *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2232
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2233
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2234
  where Line 7 prints the beginning of the list and Line
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2235
  8 the last item. We have to use @{ML "Pretty.brk 1"} in order
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2236
  to insert a space (of length 1) before the 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2237
  @{text [quotes] "and"}. This space is also a place where a line break 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2238
  can occur. We do the same after the @{text [quotes] "and"}. This gives you
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2239
  for example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2240
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2241
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2242
"let
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2243
  val ptrs1 = map (Pretty.str o string_of_int) (1 upto 22)
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2244
  val ptrs2 = map (Pretty.str o string_of_int) (10 upto 28)
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2245
in
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2246
  pprint (Pretty.blk (0, and_list ptrs1));
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2247
  pprint (Pretty.blk (0, and_list ptrs2))
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2248
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2249
"1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2250
and 22
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2251
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27 and
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2252
28"}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2253
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2254
  Next we like to pretty-print a term and its type. For this we use the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2255
  function @{text "tell_type"}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2256
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2257
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2258
ML %linenosgray{*fun tell_type ctxt t = 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2259
let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2260
  fun pstr s = Pretty.breaks (map Pretty.str (space_explode " " s))
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2261
  val ptrm = Pretty.quote (Syntax.pretty_term ctxt t)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2262
  val pty  = Pretty.quote (Syntax.pretty_typ ctxt (fastype_of t))
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2263
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2264
  pprint (Pretty.blk (0, 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2265
    (pstr "The term " @ [ptrm] @ pstr " has type " 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2266
      @ [pty, Pretty.str "."])))
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2267
end*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2268
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2269
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2270
  In Line 3 we define a function that inserts possible linebreaks in places
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2271
  where a space is. In Lines 4 and 5 we pretty-print the term and its type
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2272
  using the functions @{ML_ind  pretty_term in Syntax} and @{ML_ind 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2273
  pretty_typ in Syntax}. We also use the function @{ML_ind quote in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2274
  Pretty} in order to enclose the term and type inside quotation marks. In
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2275
  Line 9 we add a period right after the type without the possibility of a
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2276
  line break, because we do not want that a line break occurs there.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2277
  Now you can write
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2278
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2279
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2280
  "tell_type @{context} @{term \"min (Suc 0)\"}"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2281
  "The term \"min (Suc 0)\" has type \"nat \<Rightarrow> nat\"."}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2282
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2283
  To see the proper line breaking, you can try out the function on a bigger term 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2284
  and type. For example:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2285
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2286
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2287
  "tell_type @{context} @{term \"op = (op = (op = (op = (op = op =))))\"}"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2288
  "The term \"op = (op = (op = (op = (op = op =))))\" has type 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2289
\"((((('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool\"."}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2290
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2291
  The function @{ML_ind big_list in Pretty} helps with printing long lists.
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2292
  It is used for example in the command \isacommand{print\_theorems}. An
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2293
  example is as follows.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2294
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2295
  @{ML_response_fake [display,gray]
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2296
  "let
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2297
  val pstrs = map (Pretty.str o string_of_int) (4 upto 10)
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2298
in
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2299
  pprint (Pretty.big_list \"header\" pstrs)
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2300
end"
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2301
  "header
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2302
  4
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2303
  5
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2304
  6
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2305
  7
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2306
  8
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2307
  9
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2308
  10"}
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2309
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2310
  Like @{ML blk in Pretty}, the function @{ML_ind chunks in Pretty} prints out 
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2311
  a list of items, but automatically inserts forced breaks between each item.
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2312
  Compare
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2313
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2314
  @{ML_response_fake [display,gray]
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2315
  "let
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2316
  val a_and_b = [Pretty.str \"a\", Pretty.str \"b\"]
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2317
in
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2318
  pprint (Pretty.blk (0, a_and_b));
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2319
  pprint (Pretty.chunks a_and_b)
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2320
end"
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2321
"ab
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2322
a
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2323
b"}
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2324
  
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  2325
  \footnote{\bf FIXME: What happens with printing big formulae?}
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2326
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2327
  
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2328
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2329
  \begin{readmore}
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2330
  The general infrastructure for pretty-printing is implemented in the file
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2331
  @{ML_file "Pure/General/pretty.ML"}. The file @{ML_file "Pure/Syntax/syntax.ML"}
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2332
  contains pretty-printing functions for terms, types, theorems and so on.
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2333
  
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2334
  @{ML_file "Pure/General/markup.ML"}
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2335
  \end{readmore}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2336
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2337
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2338
(*
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2339
text {*
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2340
  printing into the goal buffer as part of the proof state
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2341
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2342
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2343
text {* writing into the goal buffer *}
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2344
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2345
ML {* fun my_hook interactive state =
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2346
         (interactive ? Proof.goal_message (fn () => Pretty.str  
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2347
"foo")) state
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2348
*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2349
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2350
setup %gray {* Context.theory_map (Specification.add_theorem_hook my_hook) *}
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2351
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2352
lemma "False"
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2353
oops
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2354
*)
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2355
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2356
(*
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2357
ML {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2358
fun setmp_show_all_types f =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2359
   setmp show_all_types
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2360
         (! show_types orelse ! show_sorts orelse ! show_all_types) f;
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2361
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2362
val ctxt = @{context};
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2363
val t1 = @{term "undefined::nat"};
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2364
val t2 = @{term "Suc y"};
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2365
val pty =        Pretty.block (Pretty.breaks
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2366
             [(setmp show_question_marks false o setmp_show_all_types)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2367
                  (Syntax.pretty_term ctxt) t1,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2368
              Pretty.str "=", Syntax.pretty_term ctxt t2]);
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2369
pty |> Pretty.string_of  
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2370
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2371
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2372
text {* the infrastructure of Syntax-pretty-term makes sure it is printed nicely  *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2373
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2374
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2375
ML {* Pretty.mark Markup.hilite (Pretty.str "foo") |> Pretty.string_of  |> tracing *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2376
ML {* (Pretty.str "bar") |> Pretty.string_of |> tracing *}
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2377
*)
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2378
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2379
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2380
section {* Misc (TBD) *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2381
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2382
ML {*Datatype.get_info @{theory} "List.list"*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2383
319
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  2384
text {* 
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  2385
FIXME: association lists:
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  2386
@{ML_file "Pure/General/alist.ML"}
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  2387
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  2388
FIXME: calling the ML-compiler
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  2389
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  2390
*}
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  2391
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  2392
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  2393
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  2394
section {* Managing Name Spaces (TBD) *}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2395
360
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
  2396
ML {* Sign.intern_type @{theory} "list" *}
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
  2397
ML {* Sign.intern_const @{theory} "prod_fun" *}
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
  2398
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  2399
section {* Summary *}
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  2400
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  2401
text {*
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  2402
  \begin{conventions}
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  2403
  \begin{itemize}
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  2404
  \item Start with a proper context and then pass it around 
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  2405
  through all your functions.
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  2406
  \end{itemize}
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  2407
  \end{conventions}
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  2408
*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2409
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2410
end