ProgTutorial/General.thy
author Christian Urban <urbanc@in.tum.de>
Sun, 18 Oct 2009 08:44:39 +0200
changeset 350 364fffa80794
parent 349 9e374cd891e1
child 351 f118240ab44a
permissions -rw-r--r--
polished
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
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
    14
chapter {* Isabelle in More Detail *}
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
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
    19
  everything else is build on top of this trusted core. The fundamental data
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    20
  structures involved in this core are certified terms and certified types, 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    21
  as well as theorems.
319
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    22
*}
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
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
section {* Terms and Types *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
text {*
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    28
  In Isabelle, there are certified terms and uncertified terms (respectively types). 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    29
  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
    30
  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
    31
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  @{ML_response [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
"@{term \"(a::nat) + b = c\"}" 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
"Const (\"op =\", \<dots>) $ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  (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
    36
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    37
  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
    38
  the internal representation corresponding to the datatype @{ML_type_ind "term"}, 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    39
  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
    40
*}  
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
ML_val %linenosgray{*datatype term =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  Const of string * typ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
| Free of string * typ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
| Var of indexname * typ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
| Bound of int 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
| Abs of string * typ * term 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
| $ of term * term *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
text {*
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
    51
  This datatype implements Church-style lambda-terms, where types are
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    52
  explicitly recorded in variables, constants and abstractions.  As can be
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
    53
  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
    54
  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
    55
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
  @{ML_response_fake [display, gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  "@{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
    58
  "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
    59
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  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
    61
  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
    62
  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
    63
  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
    64
  (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
    65
  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
    66
  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
    67
  term-constructor @{ML $}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
  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
    70
  @{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
    71
  \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
    72
  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
    73
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  @{ML_response_fake [display, gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
  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
    77
  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
    78
  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
    79
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  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
    81
  |> tracing
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
"?x3, ?x1.3, x"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  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
    86
  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
    87
  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
    88
  however, observe the distinction. The reason is that only schematic
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
    89
  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
    90
  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
    91
  (see below).
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
  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
    95
  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
    96
  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
    97
  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
    98
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
  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
   101
  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
   102
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
  @{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
   104
  "@{term \"x x\"}"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
  "Type unification failed: Occurs check!"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
  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
   108
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
  @{ML_response_fake [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
"let
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   111
  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
   112
in 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
  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
   114
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
  "x x"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
  with the raw ML-constructors.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
  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
   120
  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
   121
  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
   122
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
  \begin{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
  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
   125
  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
   126
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
  \begin{itemize}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
  \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
   129
  \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
   130
  \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
   131
  \end{itemize}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
  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
   134
  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
   135
  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
   136
  value:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
  @{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
   139
  \end{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
  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
   142
  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
   143
  Consider for example the pairs
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
@{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
   146
"(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
   147
 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
   148
 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
  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
   150
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
  @{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
   152
  "(Const (\"==>\", \<dots>) $ \<dots> $ \<dots>, 
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
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
  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
   156
  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
   157
  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
   158
  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
   159
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
  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
   161
  @{text "@{typ \<dots>}"}. For example:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
  @{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
   164
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
  The corresponding datatype is
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
*}
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
ML_val{*datatype typ =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
  Type  of string * typ list 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
| TFree of string * sort 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
| TVar  of indexname * sort *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
  Like with terms, there is the distinction between free type
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   175
  variables (term-constructor @{ML "TFree"}) and schematic
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
  type variables (term-constructor @{ML "TVar"}). A type constant,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
  like @{typ "int"} or @{typ bool}, are types with an empty list
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
  of argument types. However, it is a bit difficult to show an
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
  example, because Isabelle always pretty-prints types (unlike terms).
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
  Here is a contrived example:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
  @{ML_response [display, gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
  "if Type (\"bool\", []) = @{typ \"bool\"} then true else false"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
  "true"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
  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
   188
  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
   189
  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
   190
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
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
   194
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
  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
   197
  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
   198
  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
   199
  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
   200
  @{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
   201
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
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
   205
let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
  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
   207
in 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
  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
   209
end *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
  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
   213
  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
   214
  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
   215
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
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
   218
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
  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
   221
  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
   222
  the given arguments
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
  @{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
   225
"Const \<dots> $ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
  Abs (\"x\", Type (\"nat\",[]),
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
    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
   228
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
  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
   230
  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
   231
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
  @{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
   233
"Const \<dots> $ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
  Abs (\"x\", \<dots>,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
    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
   236
                (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
   237
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   238
  There are a number of handy functions that are frequently used for
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   239
  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
   240
  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
   241
  term list applied to the term. For example
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   242
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
  val trm = @{term \"P::nat\"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
  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
   248
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
  list_comb (trm, args)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
"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
   252
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   253
  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
   254
  in a term. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
  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
   259
  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
   260
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   261
  lambda x_nat trm
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
  "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
   264
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   265
  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
   266
  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
   267
  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
   268
  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
   269
  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
   270
  as in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   271
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   272
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   273
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   274
  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
   275
  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
   276
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   277
  lambda x_int trm
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   278
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   279
  "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
   280
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   281
  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
   282
  This is a fundamental principle
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   283
  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
   284
  have different type.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   285
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   286
  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
   287
  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
   288
  "(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
   289
  @{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
   290
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   291
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   292
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   293
  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
   294
  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
   295
  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
   296
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   297
  subst_free [sub1, sub2] trm
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   298
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   299
  "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
   300
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   301
  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
   302
  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
   303
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
  @{ML_response_fake [display, gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   305
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   306
  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
   307
  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
   308
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   309
  subst_free [sub] trm
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   310
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   311
  "Free (\"x\", \"nat\")"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   312
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   313
  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
   314
  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
   315
  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
   316
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   317
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   318
ML{*fun strip_alls (Const ("All", _) $ Abs (n, T, t)) =
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   319
                                    strip_alls t |>> cons (Free (n, T))
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   320
  | strip_alls t = ([], t) *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   321
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   322
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   323
  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
   324
  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
   325
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   326
  @{ML_response_fake [display, gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   327
  "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
   328
"([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
   329
  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
   330
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   331
  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
   332
  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
   333
  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
   334
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   335
  @{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
   336
  "let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   337
  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
   338
in 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   339
  subst_bounds (rev vrs, trm) 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   340
  |> string_of_term @{context}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   341
  |> tracing
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   342
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   343
  "x = y"}
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
  Note that in Line 4 we had to reverse the list of variables that @{ML strip_alls}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   346
  returned. The reason is that the head of the list the function @{ML subst_bounds}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   347
  takes is the replacement for @{ML "Bound 0"}, the next element for @{ML "Bound 1"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   348
  and so on. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   349
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   350
  There are also many convenient functions that construct specific HOL-terms
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   351
  in the structure @{ML_struct HOLogic}. For
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   352
  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
   353
  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
   354
  arguments. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   355
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   356
@{ML_response_fake [gray,display]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   357
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   358
  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
   359
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   360
  string_of_term @{context} eq
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   361
  |> tracing
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   362
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   363
  "True = False"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   364
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   365
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   366
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   367
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   368
  There are many functions in @{ML_file "Pure/term.ML"}, @{ML_file
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   369
  "Pure/logic.ML"} and @{ML_file "HOL/Tools/hologic.ML"} that make such manual
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   370
  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
   371
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   372
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   373
  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
   374
  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
   375
  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
   376
  @{text is_true} as follows
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   377
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   378
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   379
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
   380
  | is_true _ = false*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   381
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   382
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   383
  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
   384
  the function 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   385
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   386
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   387
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
   388
  | is_all _ = false*}
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
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   391
  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
   392
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   393
  @{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
   394
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   395
  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
   396
  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
   397
  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
   398
  for this function is
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
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   401
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
   402
  | is_all _ = false*}
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
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   405
  because now
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   406
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   407
@{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
   408
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   409
  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
   410
  second any term).
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   411
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   412
  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
   413
  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
   414
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   415
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   416
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
   417
  | is_nil _ = false *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   418
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   419
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   420
  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
   421
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   422
  @{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
   423
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   424
  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
   425
  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
   426
  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
   427
  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
   428
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   429
  @{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
   430
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   431
  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
   432
  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
   433
  (@{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
   434
  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
   435
  \mbox{@{text "(op *)"}}:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   436
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   437
  @{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
   438
 "(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
   439
 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
   440
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   441
  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
   442
  @{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
   443
  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
   444
  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
   445
  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
   446
  @{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
   447
  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
   448
  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
   449
  be written as follows.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   450
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   451
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   452
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
   453
  | 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
   454
  | is_nil_or_all _ = false *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   455
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   456
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   457
  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
   458
  For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   459
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   460
  @{ML_response [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   461
  "@{type_name \"list\"}" "\"List.list\""}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   462
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   463
  \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
   464
  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
   465
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   466
  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
   467
  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
   468
  @{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
   469
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   470
  @{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
   471
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   472
  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
   473
  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
   474
  strips off all qualifiers.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   475
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   476
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   477
  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
   478
  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
   479
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   480
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   481
  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
   482
  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
   483
  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
   484
  type is as follows:
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
*} 
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
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
   489
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   490
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
   491
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   492
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
   493
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   494
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   495
  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
   496
  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
   497
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   498
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   499
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
   500
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   501
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   502
  A handy function for manipulating terms is @{ML_ind  map_types}: it takes a 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   503
  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
   504
  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
   505
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   506
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   507
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
   508
  (case ty of
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   509
     @{typ nat} => @{typ int}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   510
   | 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
   511
   | _ => ty)*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   512
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   513
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   514
  Here is an example:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   515
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   516
@{ML_response_fake [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   517
"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
   518
"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
   519
           $ 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
   520
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   521
  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
   522
  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
   523
  (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
   524
  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
   525
  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
   526
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   527
  @{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
   528
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   529
  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
   530
  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
   531
  call them as follows
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   532
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   533
  @{ML_response [gray,display]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   534
  "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
   535
  "[(\"'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
   536
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   537
  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
   538
  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
   539
  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
   540
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   541
  \begin{exercise}\label{fun:revsum}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   542
  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
   543
  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
   544
  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
   545
  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
   546
  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
   547
  \end{exercise}
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
  \begin{exercise}\label{fun:makesum}
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   550
  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
   551
  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
   552
  number representing their sum.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   553
  \end{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   554
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   555
  \begin{exercise}\label{ex:debruijn}
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   556
  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
   557
  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
   558
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   559
  \begin{center}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   560
  \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
   561
  {\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
   562
  {\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
   563
                        $\longrightarrow$ {\it rhs n}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   564
  {\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
   565
  \end{tabular}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   566
  \end{center}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   567
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   568
  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
   569
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   570
  \begin{center}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   571
  \begin{tabular}{l}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   572
  (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
   573
  (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
   574
  (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
   575
  \end{tabular}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   576
  \end{center}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   577
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   578
  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
   579
  for constructing the terms for the logical connectives.\footnote{Thanks to Roy
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   580
  Dyckhoff for this exercise.} 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   581
  \end{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   582
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   583
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
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
   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
  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
   589
  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
   590
  information) so that it is always clear what the type of a term is. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   591
  Given a well-typed term, the function @{ML_ind  type_of} returns the 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   592
  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
   593
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   594
  @{ML_response [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   595
  "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
   596
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   597
  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
   598
  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
   599
  @{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
   600
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   601
  @{ML_response_fake [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   602
  "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
   603
  "*** 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
   604
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   605
  Since the complete traversal might sometimes be too costly and
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   606
  not necessary, there is the function @{ML_ind  fastype_of}, which 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   607
  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
   608
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   609
  @{ML_response [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   610
  "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
   611
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   612
  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
   613
  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
   614
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   615
   @{ML_response [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   616
  "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
   617
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   618
  where no error is detected.
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
  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
   621
  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
   622
  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
   623
  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
   624
  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
   625
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   626
  @{ML_response_fake [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   627
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   628
  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
   629
  val o = @{term \"1::nat\"} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   630
  val v = Free (\"x\", dummyT)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   631
in   
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   632
  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
   633
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   634
"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
   635
  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
   636
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   637
  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
   638
  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
   639
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   640
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   641
  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
   642
  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
   643
  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
   644
  @{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
   645
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   646
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   647
  \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
   648
  \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
   649
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   650
  \begin{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   651
  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
   652
  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
   653
  exercise given in Appendix \ref{ch:solutions} when they receive an 
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   654
  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
   655
  \end{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   656
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   657
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   658
section {* Certified Terms and Certified Types *}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   659
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   660
text {*
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   661
  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
   662
  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
   663
  eventually want to see if a term is well-formed, or type-checks, relative to
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   664
  a theory.  Type-checking is done via the function @{ML_ind  cterm_of}, which
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   665
  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
   666
  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
   667
  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
   668
  be constructed via ``official interfaces''.
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   669
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   670
  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
   671
  write:
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   672
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   673
  @{ML_response_fake [display,gray] 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   674
  "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
   675
  "a + b = c"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   676
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   677
  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
   678
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   679
  @{ML_response_fake [display,gray] 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   680
  "@{cterm \"(a::nat) + b = c\"}" 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   681
  "a + b = c"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   682
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   683
  Attempting to obtain the certified term for
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   684
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   685
  @{ML_response_fake_both [display,gray] 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   686
  "@{cterm \"1 + True\"}" 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   687
  "Type unification failed \<dots>"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   688
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   689
  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
   690
  example that type-checks is:
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   691
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   692
@{ML_response_fake [display,gray] 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   693
"let
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   694
  val natT = @{typ \"nat\"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   695
  val zero = @{term \"0::nat\"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   696
in
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   697
  cterm_of @{theory} 
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   698
      (Const (@{const_name plus}, [natT, natT] ---> natT) $ zero $ zero)
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   699
end" "0 + 0"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   700
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   701
  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
   702
  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
   703
  the ML-level as follows:
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   704
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   705
  @{ML_response_fake [display,gray]
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   706
  "ctyp_of @{theory} (@{typ nat} --> @{typ bool})"
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   707
  "nat \<Rightarrow> bool"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   708
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   709
  or with the antiquotation:
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   710
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   711
  @{ML_response_fake [display,gray]
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   712
  "@{ctyp \"nat \<Rightarrow> bool\"}"
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   713
  "nat \<Rightarrow> bool"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   714
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   715
  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
   716
  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
   717
  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
   718
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   719
  @{ML_response_fake [display,gray]
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   720
  "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
   721
  "P 3"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   722
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   723
  Similarly @{ML_ind list_comb in Drule} applies a list of @{ML_type cterm}s.
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   724
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   725
  @{ML_response_fake [display,gray]
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   726
  "let
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   727
  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
   728
  val cargs = [@{cterm \"()\"}, @{cterm \"3::nat\"}]
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   729
in
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   730
  Drule.list_comb (chead, cargs)
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   731
end"
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   732
  "P () 3"}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   733
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   734
  \begin{readmore}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   735
  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
   736
  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
   737
  @{ML_file "Pure/drule.ML"}.
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   738
  \end{readmore}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   739
*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   740
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   741
section {* Theorems *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   742
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   743
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   744
  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
   745
  that can only be built by going through interfaces. As a consequence, every proof 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   746
  in Isabelle is correct by construction. This follows the tradition of the LCF approach
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   747
  \cite{GordonMilnerWadsworth79}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   748
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   749
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   750
  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
   751
  statement:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   752
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   753
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   754
  lemma 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   755
   assumes assm\<^isub>1: "\<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
   756
   and     assm\<^isub>2: "P t"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   757
   shows "Q t" (*<*)oops(*>*) 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   758
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   759
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   760
  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
   761
*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   762
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   763
ML{*val my_thm = 
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   764
let
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   765
  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
   766
  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
   767
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   768
  val Pt_implies_Qt = 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   769
        assume assm1
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   770
        |> 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
   771
  
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   772
  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
   773
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   774
  Qt 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   775
  |> implies_intr assm2
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   776
  |> implies_intr assm1
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   777
end*}
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   778
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   779
text {* 
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   780
  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
   781
  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
   782
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   783
  @{ML_response_fake [display, gray]
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   784
  "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
   785
  "\<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
   786
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   787
  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
   788
  proof.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   789
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   790
  \[
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   791
  \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
   792
    {\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
   793
       {\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
   794
          {\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
   795
                 {\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
   796
                 &
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   797
           \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
   798
          }
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   799
       }
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   800
    }
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   801
  \]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   802
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
   803
  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
   804
  yet stored in Isabelle's theorem database. Consequently, it cannot be 
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   805
  referenced on the user level. One way to store it in the theorem database is
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   806
  by using the function @{ML_ind note in LocalTheory}.
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   807
*}
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   808
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   809
local_setup %gray {*
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   810
  LocalTheory.note Thm.theoremK
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   811
     ((@{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
   812
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   813
text {*
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
   814
  The first argument @{ML_ind theoremK in Thm} is a kind indicator, which
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   815
  classifies the theorem. There are several such kind indicators: for a
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   816
  theorem arising from a definition we should use @{ML_ind definitionK in
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   817
  Thm}, for an axiom @{ML_ind axiomK in Thm}, for ``normal'' theorems the
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   818
  kinds @{ML_ind theoremK in Thm} or @{ML_ind lemmaK in Thm}.  The second
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   819
  argument of @{ML note in LocalTheory} is the name under which we store the
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   820
  theorem or theorems. The third argument can contain a list of (theorem)
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   821
  attributes. We will explain them in detail in
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   822
  Section~\ref{sec:attributes}. Below we just use one such attribute in order add
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   823
  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
   824
*}
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
   825
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
   826
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
   827
  LocalTheory.note Thm.theoremK
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   828
    ((@{binding "my_thm_simp"}, 
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   829
       [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
   830
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
   831
text {*
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   832
  Note that we have to use another name under which the theorem is stored,
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   833
  since Isabelle does not allow us to store another theorem under the same
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   834
  name. The attribute needs to be wrapped inside the function @{ML_ind
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   835
  internal in Attrib}. If we use the function @{ML get_thm_names_from_ss} from
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   836
  the previous chapter, we can check whether the theorem has actually been
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   837
  added.
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   838
340
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
   839
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
   840
  @{ML_response [display,gray]
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
   841
  "let
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
   842
  fun pred s = match_string \"my_thm_simp\" s
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
   843
in
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
   844
  exists pred (get_thm_names_from_ss @{simpset})
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
   845
end"
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
   846
  "true"}
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
   847
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   848
  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
   849
  [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
   850
  \isacommand{thm}-command on the user-level of Isabelle
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   851
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   852
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   853
  \begin{isabelle}
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   854
  \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
   855
   @{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
   856
  \end{isabelle}
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
   857
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   858
  or with the @{text "@{thm \<dots>}"}-antiquotation on the ML-level. As can be seen
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   859
  the theorem does not have any meta-variables that would be present if we proved
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   860
  this theorem on the user-level. We will see later on that usually we have to 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   861
  construct meta-variables in theorems explicitly.
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
   862
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
   863
  \footnote{\bf FIXME say something about @{ML_ind add_thms_dynamic in PureThy}}
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   864
*}
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   865
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   866
text {*
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   867
  There is a multitude of functions that manage or manipulate theorems. For example 
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   868
  we can test theorems for alpha equality. Suppose you proved the following three 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   869
  theorems.
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   870
*}
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   871
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   872
lemma
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   873
  shows thm1: "\<forall>x. P x" 
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   874
  and   thm2: "\<forall>y. P y" 
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   875
  and   thm3: "\<forall>y. Q y" sorry
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   876
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   877
text {*
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   878
  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
   879
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   880
  @{ML_response [display,gray]
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   881
"(Thm.eq_thm (@{thm thm1}, @{thm thm2}),
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   882
 Thm.eq_thm (@{thm thm2}, @{thm thm3}))"
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   883
"(true, false)"}
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   884
340
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
   885
  Many functions destruct theorems into @{ML_type cterm}s. For example
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
   886
  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
   887
  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
   888
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   889
  @{ML_response_fake [display,gray]
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   890
  "let
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   891
  val eq = @{thm True_def}
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   892
in
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   893
  (Thm.lhs_of eq, Thm.rhs_of eq) 
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   894
  |> pairself (string_of_cterm @{context})
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   895
end"
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   896
  "(True, (\<lambda>x. x) = (\<lambda>x. x))"}
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   897
340
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
   898
  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
   899
  Suppose the following two theorems.
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   900
*}
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   901
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   902
lemma  
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   903
  shows foo_test1: "A \<Longrightarrow> B \<Longrightarrow> C" 
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   904
  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
   905
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   906
text {*
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   907
  We can destruct them into premises and conclusions as follows. 
340
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
   908
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   909
 @{ML_response_fake [display,gray]
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   910
"let
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   911
  val ctxt = @{context}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   912
  fun prems_and_concl thm =
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   913
     [\"Premises: \"   ^ (string_of_terms ctxt (Thm.prems_of thm))] @ 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   914
     [\"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
   915
     |> cat_lines
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   916
     |> tracing
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   917
in
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   918
  prems_and_concl @{thm foo_test1};
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   919
  prems_and_concl @{thm foo_test2}
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
   920
end"
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   921
"Premises: ?A, ?B
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   922
Conclusion: ?C
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   923
Premises: 
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   924
Conclusion: ?A \<longrightarrow> ?B \<longrightarrow> ?C"}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   925
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   926
  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
   927
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   928
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   929
  For the functions @{text "assume"}, @{text "forall_elim"} etc 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   930
  see \isccite{sec:thms}. 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
   931
  @{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
   932
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   933
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   934
  The simplifier can be used to unfold definition in theorms. To show
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   935
  this we build the theorem @{term "True \<equiv> True"} (Line 1) and then 
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   936
  unfold the constant according to its definition (Line 2).
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   937
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   938
  @{ML_response_fake [display,gray,linenos]
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   939
  "Thm.reflexive @{cterm \"True\"}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   940
  |> Simplifier.rewrite_rule [@{thm True_def}]
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   941
  |> string_of_thm @{context}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   942
  |> tracing"
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   943
  "(\<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
   944
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   945
  Often it is necessary to transform theorems to and from the object 
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   946
  logic.  For example, the function @{ML_ind rulify in ObjectLogic}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   947
  replaces all @{text "\<longrightarrow>"} and @{text "\<forall>"} into the equivalents of the 
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   948
  meta logic. For example
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   949
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   950
  @{ML_response_fake [display, gray]
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   951
  "ObjectLogic.rulify @{thm foo_test2}"
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   952
  "\<lbrakk>?A; ?B\<rbrakk> \<Longrightarrow> ?C"}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   953
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   954
  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
   955
  @{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
   956
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   957
  @{ML_response_fake [display,gray]
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   958
  "let
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   959
  val thm = @{thm foo_test1}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   960
  val meta_eq = ObjectLogic.atomize (cprop_of thm)
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   961
in
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   962
  MetaSimplifier.rewrite_rule [meta_eq] thm
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   963
end"
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   964
  "?A \<longrightarrow> ?B \<longrightarrow> ?C"}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   965
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   966
  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
   967
  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
   968
  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
   969
  connectives. However, in order to completely transform a theorem
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
   970
  such as @{thm [source] list.induct}, which is of the form 
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   971
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   972
  @{thm [display] list.induct}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   973
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
   974
  we have to first abstract over the meta variables @{text "?P"} and 
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   975
  @{text "?list"}. For this we can use the function 
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   976
  @{ML_ind forall_intr_vars in Drule}. 
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   977
*}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   978
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   979
ML{*fun atomize_thm thm =
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   980
let
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   981
  val thm' = forall_intr_vars thm
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   982
  val thm'' = ObjectLogic.atomize (cprop_of thm')
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   983
in
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   984
  MetaSimplifier.rewrite_rule [thm''] thm'
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   985
end*}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   986
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   987
text {*
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
   988
  For the theorem @{thm [source] list.induct}, this function produces:
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   989
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   990
  @{ML_response_fake [display, gray]
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   991
  "atomize_thm @{thm list.induct}"
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   992
  "\<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
   993
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   994
  Theorems can also be produced from terms by giving an explicit proof. 
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   995
  One way to achive this is by using the function @{ML_ind prove in Goal}. 
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
   996
  For example below we prove the term @{term "P \<Longrightarrow> P"}.
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   997
  
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   998
  @{ML_response_fake [display,gray]
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   999
  "let
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1000
  val trm = @{term \"P \<Longrightarrow> P::bool\"}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1001
  val tac = K (atac 1)
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1002
in
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1003
  Goal.prove @{context} [\"P\"] [] trm tac
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1004
end"
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1005
  "?P \<Longrightarrow> ?P"}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1006
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1007
  This function takes as second argument a list of strings. This list specifies
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1008
  which variables should be turned into meta-variables once the term is proved.
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1009
  The proof is given in form of a tactic as last argument. We explain tactics in 
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1010
  Chapter~\ref{chp:tactical}. In the code above the tactic proved the theorem 
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1011
  by assumption.
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1012
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1013
  Theorems also contain auxiliary data, such their names and kind, but also 
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1014
  names for cases etc. This data is stored in a string-string list and can
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1015
  be retrieved with the function @{ML_ind get_tags in Thm}. Assume the
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1016
  following lemma. 
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1017
*}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1018
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1019
lemma foo_data: "P \<Longrightarrow> P \<Longrightarrow> P" by assumption
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1020
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1021
text {*
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1022
  The auxiliary data of this lemma is as follows. 
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1023
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1024
  @{ML_response_fake [display,gray]
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1025
  "Thm.get_tags @{thm foo_data}"
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1026
  "[(\"name\", \"General.foo_data\"), (\"kind\", \"lemma\")]"}
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1027
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1028
  When we store lemmas in the theorem database, we can explicitly extend the data 
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1029
  associated with this lemma by attaching case names.  
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1030
*}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1031
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1032
local_setup %gray {*
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1033
  LocalTheory.note Thm.lemmaK
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1034
    ((@{binding "foo_data'"}, []), 
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1035
      [(RuleCases.name ["foo_case1", "foo_case2"] 
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1036
        @{thm foo_data})]) #> snd *}
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1037
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1038
text {*
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1039
  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
  1040
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1041
  @{ML_response_fake [display,gray]
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1042
  "Thm.get_tags @{thm foo_data'}"
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1043
  "[(\"name\", \"General.foo_data'\"), (\"kind\", \"lemma\"), 
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1044
 (\"case_names\", \"foo_case1;foo_case2\")]"}
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1045
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1046
  You notice the difference when using the proof methods @{ML_text cases} 
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1047
  or @{ML_text induct}. In the proof below
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1048
*}
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1049
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1050
lemma
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1051
  "Q \<Longrightarrow> Q \<Longrightarrow> Q"
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1052
proof (cases rule: foo_data')
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1053
print_cases
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1054
(*<*)oops(*>*)
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1055
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1056
text {*
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1057
  we can proceed by analysing the cases @{ML_text "foo_case1"} and 
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1058
  @{ML_text "foo_case2"}. While if the theorem has no names, then
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1059
  the cases have standard names @{ML_text 1}, @{ML_text 2} and so 
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1060
  on. This can be seen in the proof below.
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1061
*}
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1062
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1063
lemma
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1064
  "Q \<Longrightarrow> Q \<Longrightarrow> Q"
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1065
proof (cases rule: foo_data)
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1066
print_cases
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1067
(*<*)oops(*>*)
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1068
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1069
text {*
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1070
  TBD below
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1071
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1072
  One great feature of Isabelle is its document preparation system where
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1073
  proved theorems can be quoted in the text directly from the formalisation. 
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1074
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1075
  (FIXME: example for how to add theorem styles)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1076
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1077
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1078
ML {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1079
fun strip_assums_all (params, Const("all",_) $ Abs(a, T, t)) =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1080
      strip_assums_all ((a, T)::params, t)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1081
  | strip_assums_all (params, B) = (params, B)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1082
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1083
fun style_parm_premise i ctxt t =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1084
  let val prems = Logic.strip_imp_prems t in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1085
    if i <= length prems
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1086
    then let val (params,t) = strip_assums_all([], nth prems (i - 1))
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1087
         in subst_bounds(map Free params, t) end
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1088
    else error ("Not enough premises for prem" ^ string_of_int i ^
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1089
      " in propositon: " ^ string_of_term ctxt t)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1090
  end;
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1091
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1092
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1093
ML {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1094
strip_assums_all ([], @{term "\<And>x y. A x y"})
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1095
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1096
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1097
(*
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1098
setup %gray {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1099
  TermStyle.add_style "no_all_prem1" (style_parm_premise 1) #>
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1100
  TermStyle.add_style "no_all_prem2" (style_parm_premise 2)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1101
*}
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1102
*)
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1103
lemma 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1104
  shows "A \<Longrightarrow> B"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1105
  and   "C \<Longrightarrow> D"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1106
oops
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1107
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1108
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1109
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
  1110
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1111
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1112
  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
  1113
  "[simp]"} and so on. Such attributes are \emph{neither} tags \emph{nor} flags
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1114
  annotated to theorems, but functions that do further processing once a
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1115
  theorem is proved. In particular, it is not possible to find out
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1116
  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
  1117
  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
  1118
  data structure. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1119
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1120
  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
  1121
  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
  1122
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1123
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1124
  \isacommand{print\_attributes}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1125
  @{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
  1126
  @{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
  1127
  @{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
  1128
  @{text "> \<dots>"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1129
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1130
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1131
  The theorem attributes fall roughly into two categories: the first category manipulates
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1132
  the proved theorem (for example @{text "[symmetric]"} and @{text "[THEN \<dots>]"}), and the second
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1133
  stores the proved theorem somewhere as data (for example @{text "[simp]"}, which adds
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1134
  the theorem to the current simpset).
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1135
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1136
  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
  1137
  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
  1138
  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
  1139
  this attribute is
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1140
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1141
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1142
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
  1143
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1144
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1145
  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
  1146
  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
  1147
  returns another theorem (namely @{text thm} resolved with the theorem 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1148
  @{thm [source] sym}: @{thm sym[no_vars]}; the function @{ML_ind "RS"} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1149
  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
  1150
  @{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
  1151
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1152
  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
  1153
  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
  1154
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1155
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1156
attribute_setup %gray 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
  1157
  "applying the sym rule"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1158
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1159
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1160
  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
  1161
  for the theorem attribute. Since the attribute does not expect any further 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1162
  arguments (unlike @{text "[THEN \<dots>]"}, for example), we use the parser @{ML
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1163
  Scan.succeed}. Later on we will also consider attributes taking further
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1164
  arguments. An example for the attribute @{text "[my_sym]"} is the proof
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1165
*} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1166
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1167
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
  1168
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1169
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1170
  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
  1171
  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
  1172
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1173
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1174
  \isacommand{thm}~@{text "test"}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1175
  @{text "> "}~@{thm test}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1176
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1177
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1178
  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
  1179
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1180
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1181
  \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
  1182
  @{text "> "}~@{thm test[my_sym]}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1183
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1184
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1185
  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
  1186
  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
  1187
  attribute as follows:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1188
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1189
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1190
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
  1191
  "applying the sym rule" *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1192
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1193
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1194
  This gives a function from @{ML_type "Context.theory -> Context.theory"}, which
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1195
  can be used for example with \isacommand{setup}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1196
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1197
  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
  1198
  our own version of @{text "[THEN \<dots>]"}. This attribute will take a list of theorems
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1199
  as argument and resolve the proved theorem with this list (one theorem 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1200
  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
  1201
*}
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
ML{*fun MY_THEN thms = 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1204
  Thm.rule_attribute (fn _ => fn thm => foldl ((op RS) o swap) thm thms)*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1205
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1206
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1207
  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
  1208
  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
  1209
  theorems. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1210
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1211
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1212
attribute_setup %gray MY_THEN = {* Attrib.thms >> MY_THEN *} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1213
  "resolving the list of theorems with the proved theorem"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1214
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1215
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1216
  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
  1217
  meta-equation:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1218
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1219
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1220
  \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
  1221
  @{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
  1222
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1223
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1224
  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
  1225
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1226
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1227
  \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
  1228
  @{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
  1229
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1230
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1231
  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
  1232
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1233
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1234
  \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
  1235
  @{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
  1236
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1237
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1238
  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
  1239
  of theorem attributes shows through: since theorem attributes can be
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1240
  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
  1241
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1242
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1243
  \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
  1244
  @{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
  1245
  \end{isabelle}
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
  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
  1248
  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
  1249
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1250
  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
  1251
  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
  1252
  from stored data.  For example the theorem attribute @{text "[simp]"} adds
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1253
  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
  1254
  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
  1255
  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
  1256
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1257
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1258
ML{*structure MyThms = Named_Thms
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1259
  (val name = "attr_thms" 
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1260
   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
  1261
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1262
text {* 
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1263
  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
  1264
  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
  1265
  @{ML MyThms.del_thm}. You can turn them into attributes 
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1266
  with the code
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1267
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1268
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1269
ML{*val my_add = Thm.declaration_attribute MyThms.add_thm
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1270
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
  1271
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1272
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1273
  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
  1274
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1275
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1276
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
  1277
  "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
  1278
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1279
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1280
  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
  1281
  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
  1282
  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
  1283
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1284
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1285
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
  1286
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1287
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1288
  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
  1289
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1290
  @{ML_response_fake [display,gray]
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1291
  "MyThms.get @{context}" 
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1292
  "[\"True\"]"}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1293
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1294
  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
  1295
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1296
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1297
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
  1298
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1299
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1300
  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
  1301
  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
  1302
  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
  1303
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1304
  @{ML_response_fake [display,gray]
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1305
  "MyThms.get @{context}"
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1306
  "[\"True\", \"Suc (Suc 0) = 2\"]"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1307
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1308
  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
  1309
  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
  1310
  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
  1311
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1312
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1313
declare test[my_thms del]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1314
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1315
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
  1316
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1317
  @{ML_response_fake [display,gray]
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1318
  "MyThms.get @{context}"
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1319
  "[\"True\"]"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1320
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1321
  We used in this example two functions declared as @{ML_ind
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1322
  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
  1323
  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
  1324
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1325
  \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
  1326
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1327
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1328
  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
  1329
  @{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
  1330
  parsing.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1331
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1332
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1333
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1334
section {* Setups (TBD) *}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1335
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1336
text {*
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1337
  In the previous section we used \isacommand{setup} in order to make
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1338
  a theorem attribute known to Isabelle. What happens behind the scenes
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1339
  is that \isacommand{setup} expects a function of type 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1340
  @{ML_type "theory -> theory"}: the input theory is the current theory and the 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1341
  output the theory where the theory attribute has been stored.
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1342
  
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1343
  This is a fundamental principle in Isabelle. A similar situation occurs 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1344
  for example with declaring constants. The function that declares a 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1345
  constant on the ML-level is @{ML_ind  add_consts_i in Sign}. 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1346
  If you write\footnote{Recall that ML-code  needs to be 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1347
  enclosed in \isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"}.} 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1348
*}  
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1349
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1350
ML{*Sign.add_consts_i [(@{binding "BAR"}, @{typ "nat"}, NoSyn)] @{theory} *}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1351
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1352
text {*
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1353
  for declaring the constant @{text "BAR"} with type @{typ nat} and 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1354
  run the code, then you indeed obtain a theory as result. But if you 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1355
  query the constant on the Isabelle level using the command \isacommand{term}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1356
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1357
  \begin{isabelle}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1358
  \isacommand{term}~@{text [quotes] "BAR"}\\
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1359
  @{text "> \"BAR\" :: \"'a\""}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1360
  \end{isabelle}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1361
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1362
  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
  1363
  blue) of polymorphic type. The problem is that the ML-expression above did 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1364
  not register the declaration with the current theory. This is what the command
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1365
  \isacommand{setup} is for. The constant is properly declared with
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1366
*}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1367
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1368
setup %gray {* Sign.add_consts_i [(@{binding "BAR"}, @{typ "nat"}, NoSyn)] *}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1369
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1370
text {* 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1371
  Now 
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1372
  
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1373
  \begin{isabelle}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1374
  \isacommand{term}~@{text [quotes] "BAR"}\\
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1375
  @{text "> \"BAR\" :: \"nat\""}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1376
  \end{isabelle}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1377
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1378
  returns a (black) constant with the type @{typ nat}.
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1379
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1380
  A similar command is \isacommand{local\_setup}, which expects a function
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1381
  of type @{ML_type "local_theory -> local_theory"}. Later on we will also
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1382
  use the commands \isacommand{method\_setup} for installing methods in the
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1383
  current theory and \isacommand{simproc\_setup} for adding new simprocs to
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1384
  the current simpset.
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1385
*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1386
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
  1387
section {* Theories\label{sec:theories} (TBD) *}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1388
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1389
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1390
  There are theories, proof contexts and local theories (in this order, if you
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1391
  want to order them). 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1392
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1393
  In contrast to an ordinary theory, which simply consists of a type
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1394
  signature, as well as tables for constants, axioms and theorems, a local
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1395
  theory contains additional context information, such as locally fixed
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1396
  variables and local assumptions that may be used by the package. The type
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1397
  @{ML_type local_theory} is identical to the type of \emph{proof contexts}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1398
  @{ML_type "Proof.context"}, although not every proof context constitutes a
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1399
  valid local theory.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1400
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1401
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1402
section {* Contexts (TBD) *}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1403
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1404
section {* Local Theories (TBD) *}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1405
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1406
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1407
(*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1408
setup {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1409
 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
  1410
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1411
lemma "bar = (1::nat)"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1412
  oops
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1413
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1414
setup {*   
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1415
  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
  1416
 #> 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
  1417
       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
  1418
 #> snd
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1419
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1420
*)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1421
(*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1422
lemma "foo = (1::nat)"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1423
  apply(simp add: foo_def)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1424
  done
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1425
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1426
thm foo_def
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1427
*)
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1428
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1429
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1430
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
  1431
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1432
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1433
  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
  1434
  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
  1435
  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
  1436
  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
  1437
  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
  1438
  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
  1439
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1440
  @{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
  1441
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1442
  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
  1443
  type. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1444
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1445
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1446
  "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
  1447
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1448
  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
  1449
  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
  1450
  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
  1451
  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
  1452
  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
  1453
  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
  1454
  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
  1455
  type:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1456
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1457
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1458
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
  1459
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1460
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1461
  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
  1462
  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
  1463
  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
  1464
  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
  1465
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1466
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1467
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
  1468
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1469
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1470
  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
  1471
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1472
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1473
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
  1474
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1475
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1476
  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
  1477
  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
  1478
  we obtain the ugly output:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1479
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1480
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1481
"tracing test_str"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1482
"fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar foooooooooo
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1483
ooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaa
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1484
aaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fo
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1485
oooooooooooooobaaaaaaaaaaaar"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1486
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1487
  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
  1488
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1489
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1490
"pprint (Pretty.str test_str)"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1491
"fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar foooooooooo
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1492
ooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaa
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1493
aaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fo
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1494
oooooooooooooobaaaaaaaaaaaar"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1495
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1496
  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
  1497
  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
  1498
  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
  1499
  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
  1500
  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
  1501
  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
  1502
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1503
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1504
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1505
  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
  1506
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1507
  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
  1508
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1509
"fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1510
fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1511
fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1512
fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1513
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1514
  Here the layout of @{ML test_str} is much more pleasing to the 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1515
  eye. The @{ML "0"} in @{ML_ind  blk in Pretty} stands for no indentation
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1516
  of the printed string. You can increase the indentation and obtain
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1517
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1518
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1519
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1520
  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
  1521
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1522
  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
  1523
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1524
"fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1525
   fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1526
   fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1527
   fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1528
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1529
  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
  1530
  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
  1531
  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
  1532
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1533
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1534
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1535
  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
  1536
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1537
  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
  1538
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1539
"          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1540
          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1541
          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1542
          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1543
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1544
  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
  1545
  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
  1546
  @{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
  1547
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1548
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1549
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1550
  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
  1551
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1552
  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
  1553
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1554
"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
  1555
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
  1556
100016, 100017, 100018, 100019, 100020"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1557
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1558
  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
  1559
  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
  1560
  @{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
  1561
  @{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
  1562
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1563
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1564
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1565
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1566
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1567
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1568
  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
  1569
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1570
  pprint (Pretty.enum \",\" \"{\" \"}\" ptrs)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1571
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1572
"{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
  1573
  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
  1574
  100016, 100017, 100018, 100019, 100020}"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1575
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1576
  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
  1577
  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
  1578
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1579
  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
  1580
  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
  1581
  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
  1582
  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
  1583
  write the function
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1584
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1585
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1586
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1587
ML %linenosgray{*fun and_list [] = []
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1588
  | and_list [x] = [x]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1589
  | and_list xs = 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1590
      let 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1591
        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
  1592
      in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1593
        (Pretty.commas front) @ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1594
        [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
  1595
      end *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1596
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1597
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1598
  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
  1599
  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
  1600
  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
  1601
  @{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
  1602
  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
  1603
  for example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1604
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1605
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1606
"let
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1607
  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
  1608
  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
  1609
in
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1610
  pprint (Pretty.blk (0, and_list ptrs1));
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1611
  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
  1612
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1613
"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
  1614
and 22
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1615
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
  1616
28"}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1617
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1618
  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
  1619
  function @{text "tell_type"}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1620
*}
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
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
  1623
let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1624
  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
  1625
  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
  1626
  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
  1627
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1628
  pprint (Pretty.blk (0, 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1629
    (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
  1630
      @ [pty, Pretty.str "."])))
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1631
end*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1632
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1633
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1634
  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
  1635
  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
  1636
  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
  1637
  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
  1638
  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
  1639
  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
  1640
  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
  1641
  Now you can write
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1642
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1643
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1644
  "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
  1645
  "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
  1646
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1647
  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
  1648
  and type. For example:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1649
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1650
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1651
  "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
  1652
  "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
  1653
\"((((('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
  1654
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1655
  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
  1656
  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
  1657
  example is as follows.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1658
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1659
  @{ML_response_fake [display,gray]
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1660
  "let
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1661
  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
  1662
in
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1663
  pprint (Pretty.big_list \"header\" pstrs)
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1664
end"
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1665
  "header
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1666
  4
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1667
  5
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1668
  6
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1669
  7
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1670
  8
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1671
  9
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1672
  10"}
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1673
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1674
  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
  1675
  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
  1676
  Compare
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1677
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1678
  @{ML_response_fake [display,gray]
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1679
  "let
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1680
  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
  1681
in
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1682
  pprint (Pretty.blk (0, a_and_b));
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1683
  pprint (Pretty.chunks a_and_b)
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1684
end"
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1685
"ab
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1686
a
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1687
b"}
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1688
  
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1689
  \footnote{\bf What happens with printing big formulae?}
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1690
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1691
  
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1692
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1693
  \begin{readmore}
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1694
  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
  1695
  @{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
  1696
  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
  1697
  
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1698
  @{ML_file "Pure/General/markup.ML"}
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1699
  \end{readmore}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1700
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1701
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1702
(*
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1703
text {*
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1704
  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
  1705
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1706
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1707
text {* writing into the goal buffer *}
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1708
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1709
ML {* fun my_hook interactive state =
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1710
         (interactive ? Proof.goal_message (fn () => Pretty.str  
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1711
"foo")) state
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1712
*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1713
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1714
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
  1715
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1716
lemma "False"
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1717
oops
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1718
*)
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1719
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  1720
(*
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1721
ML {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1722
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
  1723
   setmp show_all_types
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1724
         (! 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
  1725
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1726
val ctxt = @{context};
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1727
val t1 = @{term "undefined::nat"};
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1728
val t2 = @{term "Suc y"};
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1729
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
  1730
             [(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
  1731
                  (Syntax.pretty_term ctxt) t1,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1732
              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
  1733
pty |> Pretty.string_of  
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1734
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1735
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1736
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
  1737
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1738
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1739
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
  1740
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
  1741
*)
318
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
section {* Misc (TBD) *}
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
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
  1747
319
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  1748
text {* 
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  1749
FIXME: association lists:
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  1750
@{ML_file "Pure/General/alist.ML"}
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  1751
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  1752
FIXME: calling the ML-compiler
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  1753
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  1754
*}
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  1755
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  1756
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
  1757
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1758
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
  1759
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1760
section {* Summary *}
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1761
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1762
text {*
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1763
  \begin{conventions}
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1764
  \begin{itemize}
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1765
  \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
  1766
  through all your functions.
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1767
  \end{itemize}
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1768
  \end{conventions}
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1769
*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1770
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1771
end