ProgTutorial/General.thy
author Christian Urban <urbanc@in.tum.de>
Fri, 21 Aug 2009 11:42:14 +0200
changeset 318 efb5fff99c96
child 319 6bce4acf7f2a
permissions -rw-r--r--
split up the first-steps section into two chapters
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
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
chapter {* General Infrastructure *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
section {* Terms and Types *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  One way to construct Isabelle terms, is by using the antiquotation 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  \mbox{@{text "@{term \<dots>}"}}. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
  @{ML_response [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
"@{term \"(a::nat) + b = c\"}" 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
"Const (\"op =\", \<dots>) $ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  (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
    17
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  will show the term @{term "(a::nat) + b = c"}, but printed using the internal
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  representation corresponding to the datatype @{ML_type  "term"} defined as follows: 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
*}  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
ML_val %linenosgray{*datatype term =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  Const of string * typ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
| Free of string * typ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
| Var of indexname * typ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
| Bound of int 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
| Abs of string * typ * term 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
| $ of term * term *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  This datatype implements lambda-terms typed in Church-style.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  As can be seen in Line 5, terms use the usual de Bruijn index mechanism
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  for representing bound variables.  For
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  example in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  @{ML_response_fake [display, gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  "@{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
    38
  "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
    39
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  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
    41
  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
    42
  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
    43
  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
    44
  (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
    45
  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
    46
  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
    47
  term-constructor @{ML $}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  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
    50
  @{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
    51
  \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
    52
  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
    53
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  @{ML_response_fake [display, gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
  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
    57
  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
    58
  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
    59
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  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
    61
  |> tracing
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
"?x3, ?x1.3, x"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  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
    66
  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
    67
  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
    68
  however, observe the distinction. The reason is that only schematic
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
  varaibles can be instantiated with terms when a theorem is applied. A
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  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
    71
  (see below).
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  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
    75
  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
    76
  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
    77
  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
    78
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  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
    81
  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
    82
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
  @{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
    84
  "@{term \"x x\"}"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  "Type unification failed: Occurs check!"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
  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
    88
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  @{ML_response_fake [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
  val omega = Free (\"x\", @{typ nat}) $ Free (\"x\", @{typ nat})
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
in 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
  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
    94
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
  "x x"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  with the raw ML-constructors.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
  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
   100
  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
   101
  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
   102
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
  \begin{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
  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
   105
  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
   106
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
  \begin{itemize}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
  \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
   109
  \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
   110
  \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
   111
  \end{itemize}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
  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
   114
  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
   115
  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
   116
  value:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
  @{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
   119
  \end{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
  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
   122
  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
   123
  Consider for example the pairs
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
@{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
   126
"(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
   127
 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
   128
 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
  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
   130
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
  @{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
   132
  "(Const (\"==>\", \<dots>) $ \<dots> $ \<dots>, 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
 Const (\"==>\", \<dots>) $ \<dots> $ \<dots>)"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
  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
   136
  The purpose of the @{text "Trueprop"}-coercion is to embed formulae of
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
  an object logic, for example HOL, into the meta-logic of Isabelle. It
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
  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
   139
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
  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
   141
  @{text "@{typ \<dots>}"}. For example:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
  @{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
   144
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
  The corresponding datatype is
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
ML_val{*datatype typ =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
  Type  of string * typ list 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
| TFree of string * sort 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
| TVar  of indexname * sort *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
  Like with terms, there is the distinction between free type
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
  variables (term-constructor @{ML "TFree"} and schematic
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
  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
   157
  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
   158
  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
   159
  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
   160
  Here is a contrived example:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
  @{ML_response [display, gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
  "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
   164
  "true"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
  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
   168
  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
   169
  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
   170
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
*}
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
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
   174
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
  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
   177
  only construct fixed terms (remember they are ``linked'' at compile-time).
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
  However, you often need to construct terms dynamically. For example, a
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
  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
   180
  @{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
   181
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
ML{*fun make_imp P Q =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
  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
   187
in 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
  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
   189
end *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
  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
   193
  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
   194
  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
   195
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
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
   198
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
  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
   201
  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
   202
  the given arguments
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_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
   205
"Const \<dots> $ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
  Abs (\"x\", Type (\"nat\",[]),
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
    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
   208
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
  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
   210
  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
   211
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
  @{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
   213
"Const \<dots> $ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
  Abs (\"x\", \<dots>,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
    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
   216
                (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
   217
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
  There are a number of handy functions that are frequently used for 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
  constructing terms. One is the function @{ML_ind  list_comb}, which takes a term
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
  and a list of terms as arguments, and produces as output the term
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
  list applied to the term. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
  val trm = @{term \"P::nat\"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
  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
   227
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
  list_comb (trm, args)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
"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
   231
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
  Another handy function is @{ML_ind  lambda}, which abstracts a variable 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
  in a term. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
  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
   238
  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
   239
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
  lambda x_nat trm
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
  "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
   243
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
  In this example, @{ML lambda} produces a de Bruijn index (i.e.~@{ML "Bound 0"}), 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
  and an abstraction. It also records the type of the abstracted
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
  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
   247
  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
   248
  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
   249
  as in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
  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
   254
  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
   255
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
  lambda x_int trm
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
  "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
   259
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
  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
   261
  This is a fundamental principle
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
  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
   263
  have different type.
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
  There is also the function @{ML_ind  subst_free} with which terms can be
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
  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
   267
  "(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
   268
  @{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
   269
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   271
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   272
  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
   273
  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
   274
  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
   275
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   276
  subst_free [sub1, sub2] trm
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   277
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   278
  "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
   279
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   280
  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
   281
  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
   282
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   283
  @{ML_response_fake [display, gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   284
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   285
  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
   286
  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
   287
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   288
  subst_free [sub] trm
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   289
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   290
  "Free (\"x\", \"nat\")"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   291
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   292
  Similarly the function @{ML_ind  subst_bounds}, replaces lose bound 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   293
  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
   294
  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
   295
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   296
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   297
ML{*fun strip_alls (Const ("All", _) $ Abs (n, T, t)) =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   298
         strip_alls t |>> cons (Free (n, T))
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   299
  | strip_alls t = ([], t) *}
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
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   302
  The function returns a pair consisting of the stripped off variables and
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   303
  the body of the universal quantifications. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   305
  @{ML_response_fake [display, gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   306
  "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
   307
"([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
   308
  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
   309
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   310
  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
   311
  the function @{ML subst_bounds}, you can replace these lose @{ML_ind 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   312
  Bound}s with the stripped off variables.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   313
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   314
  @{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
   315
  "let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   316
  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
   317
in 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   318
  subst_bounds (rev vrs, trm) 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   319
  |> string_of_term @{context}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   320
  |> tracing
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   321
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   322
  "x = y"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   323
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   324
  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
   325
  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
   326
  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
   327
  and so on. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   328
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   329
  There are many convenient functions that construct specific HOL-terms. For
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   330
  example @{ML_ind  mk_eq in HOLogic} constructs an equality out of two terms.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   331
  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
   332
  arguments. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   333
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   334
@{ML_response_fake [gray,display]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   335
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   336
  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
   337
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   338
  string_of_term @{context} eq
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   339
  |> tracing
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   340
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   341
  "True = False"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   342
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   343
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   344
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   345
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   346
  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
   347
  "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
   348
  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
   349
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   350
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   351
  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
   352
  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
   353
  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
   354
  @{text is_true} as follows
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
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   357
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
   358
  | is_true _ = false*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   359
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   360
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   361
  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
   362
  the function 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   363
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   364
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   365
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
   366
  | is_all _ = false*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   367
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   368
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   369
  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
   370
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   371
  @{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
   372
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   373
  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
   374
  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
   375
  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
   376
  for this function is
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_all (Const ("All", _) $ _) = true
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   380
  | is_all _ = 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
  because now
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   384
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   385
@{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
   386
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   387
  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
   388
  second any term).
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   389
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   390
  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
   391
  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
   392
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   393
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   394
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
   395
  | is_nil _ = false *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   396
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   397
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   398
  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
   399
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   400
  @{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
   401
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   402
  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
   403
  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
   404
  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
   405
  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
   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] "@{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
   408
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   409
  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
   410
  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
   411
  (@{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
   412
  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
   413
  \mbox{@{text "(op *)"}}:
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
  @{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
   416
 "(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
   417
 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
   418
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   419
  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
   420
  @{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
   421
  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
   422
  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
   423
  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
   424
  @{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
   425
  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
   426
  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
   427
  be written as follows.
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
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   430
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
   431
  | 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
   432
  | is_nil_or_all _ = false *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   433
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   434
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   435
  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
   436
  For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   437
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   438
  @{ML_response [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   439
  "@{type_name \"list\"}" "\"List.list\""}
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
  (FIXME: Explain the following better.)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   442
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   443
  Occasionally you have to calculate what the ``base'' name of a given
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   444
  constant is. For this you can use the function @{ML_ind  "Sign.extern_const"} or
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   445
  @{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
   446
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   447
  @{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
   448
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   449
  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
   450
  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
   451
  strips off all qualifiers.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   452
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   453
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   454
  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
   455
  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
   456
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   457
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   458
  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
   459
  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
   460
  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
   461
  type is as follows:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   462
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   463
*} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   464
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   465
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
   466
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   467
text {* This can be equally written with the combinator @{ML_ind  "-->"} as: *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   468
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   469
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
   470
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   471
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   472
  If you want to construct a function type with more than one argument
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   473
  type, then you can use @{ML_ind  "--->"}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   474
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   475
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   476
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
   477
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   478
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   479
  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
   480
  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
   481
  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
   482
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   483
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   484
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
   485
  (case ty of
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   486
     @{typ nat} => @{typ int}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   487
   | 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
   488
   | _ => ty)*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   489
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   490
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   491
  Here is an example:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   492
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   493
@{ML_response_fake [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   494
"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
   495
"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
   496
           $ 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
   497
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   498
  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
   499
  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
   500
  (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
   501
  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
   502
  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
   503
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   504
  @{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
   505
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   506
  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
   507
  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
   508
  call them as follows
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   509
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   510
  @{ML_response [gray,display]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   511
  "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
   512
  "[(\"'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
   513
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   514
  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
   515
  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
   516
  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
   517
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   518
  \begin{exercise}\label{fun:revsum}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   519
  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
   520
  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
   521
  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
   522
  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
   523
  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
   524
  \end{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   525
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   526
  \begin{exercise}\label{fun:makesum}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   527
  Write a function which takes two terms representing natural numbers
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   528
  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
   529
  number representing their sum.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   530
  \end{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   531
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   532
  \begin{exercise}\footnote{Personal communication of
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   533
  de Bruijn to Dyckhoff.}\label{ex:debruijn}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   534
  Implement the function, which we below name deBruijn, that depends on a natural
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   535
  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
   536
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   537
  \begin{center}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   538
  \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
   539
  {\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
   540
  {\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
   541
                        $\longrightarrow$ {\it rhs n}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   542
  {\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
   543
  \end{tabular}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   544
  \end{center}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   545
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   546
  For n=3 this function returns the term
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   547
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   548
  \begin{center}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   549
  \begin{tabular}{l}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   550
  (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
   551
  (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
   552
  (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
   553
  \end{tabular}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   554
  \end{center}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   555
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   556
  Make sure you use the functions defined 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
   557
  for constructing the terms for the logical connectives. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   558
  \end{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   559
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   560
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   561
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   562
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
   563
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   564
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   565
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   566
  You can freely construct and manipulate @{ML_type "term"}s and @{ML_type
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   567
  typ}es, since they are just arbitrary unchecked trees. However, you
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   568
  eventually want to see if a term is well-formed, or type-checks, relative to
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   569
  a theory.  Type-checking is done via the function @{ML_ind  cterm_of}, which
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   570
  converts a @{ML_type  term} into a @{ML_type  cterm}, a \emph{certified}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   571
  term. Unlike @{ML_type term}s, which are just trees, @{ML_type "cterm"}s are
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   572
  abstract objects that are guaranteed to be type-correct, and they can only
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   573
  be constructed via ``official interfaces''.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   574
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   575
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   576
  Type-checking is always relative to a theory context. For now we use
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   577
  the @{ML "@{theory}"} antiquotation to get hold of the current theory.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   578
  For example you can write:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   579
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   580
  @{ML_response_fake [display,gray] "cterm_of @{theory} @{term \"(a::nat) + b = c\"}" "a + b = c"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   581
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   582
  This can also be written with an antiquotation:
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
  @{ML_response_fake [display,gray] "@{cterm \"(a::nat) + b = c\"}" "a + b = c"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   585
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   586
  Attempting to obtain the certified term for
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   587
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   588
  @{ML_response_fake_both [display,gray] "@{cterm \"1 + True\"}" "Type unification failed \<dots>"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   589
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   590
  yields an error (since the term is not typable). A slightly more elaborate
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   591
  example that type-checks is:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   592
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   593
@{ML_response_fake [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   594
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   595
  val natT = @{typ \"nat\"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   596
  val zero = @{term \"0::nat\"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   597
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   598
  cterm_of @{theory} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   599
      (Const (@{const_name plus}, natT --> natT --> natT) $ zero $ zero)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   600
end" "0 + 0"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   601
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   602
  In Isabelle not just terms need to be certified, but also types. For example, 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   603
  you obtain the certified type for the Isabelle type @{typ "nat \<Rightarrow> bool"} on 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   604
  the ML-level as follows:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   605
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   606
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   607
  "ctyp_of @{theory} (@{typ nat} --> @{typ bool})"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   608
  "nat \<Rightarrow> bool"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   609
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   610
  or with the antiquotation:
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
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   613
  "@{ctyp \"nat \<Rightarrow> bool\"}"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   614
  "nat \<Rightarrow> bool"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   615
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   616
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   617
  For functions related to @{ML_type cterm}s and @{ML_type ctyp}s see 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   618
  the file @{ML_file "Pure/thm.ML"}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   619
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   620
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   621
  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
   622
  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
   623
  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
   624
  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
   625
  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
   626
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   627
  @{ML_response [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   628
  "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
   629
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   630
  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
   631
  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
   632
  @{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
   633
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   634
  @{ML_response_fake [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   635
  "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
   636
  "*** 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
   637
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   638
  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
   639
  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
   640
  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
   641
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   642
  @{ML_response [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   643
  "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
   644
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   645
  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
   646
  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
   647
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   648
   @{ML_response [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   649
  "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
   650
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   651
  where no error is detected.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   652
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   653
  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
   654
  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
   655
  information is redundant. A short-cut is to use the ``place-holder'' 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   656
  type @{ML_ind  dummyT} and then let type-inference figure out the 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   657
  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
   658
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   659
  @{ML_response_fake [display,gray] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   660
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   661
  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
   662
  val o = @{term \"1::nat\"} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   663
  val v = Free (\"x\", dummyT)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   664
in   
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   665
  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
   666
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   667
"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
   668
  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
   669
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   670
  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
   671
  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
   672
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   673
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   674
  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
   675
  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
   676
  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
   677
  @{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
   678
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   679
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   680
  (FIXME: say something about sorts)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   681
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   682
  \begin{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   683
  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
   684
  result that type-checks. See what happens to the  solutions of this 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   685
  exercise given in \ref{ch:solutions} when they receive an ill-typed term
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   686
  as input.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   687
  \end{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   688
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   689
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   690
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   691
section {* Theorems *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   692
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   693
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   694
  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
   695
  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
   696
  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
   697
  \cite{GordonMilnerWadsworth79}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   698
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   699
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   700
  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
   701
  statement:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   702
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   703
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   704
  lemma 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   705
   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
   706
   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
   707
   shows "Q t" (*<*)oops(*>*) 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   708
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   709
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   710
  The corresponding ML-code is as follows:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   711
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   712
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   713
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   714
  val assm1 = @{cprop \"\<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
   715
  val assm2 = @{cprop \"(P::nat\<Rightarrow>bool) t\"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   716
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   717
  val Pt_implies_Qt = 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   718
        assume assm1
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   719
        |> forall_elim @{cterm \"t::nat\"};
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   720
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   721
  val Qt = implies_elim Pt_implies_Qt (assume assm2);
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   722
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   723
  Qt 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   724
  |> implies_intr assm2
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   725
  |> implies_intr assm1
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   726
end" "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   727
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   728
  This code-snippet constructs the following proof:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   729
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   730
  \[
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   731
  \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
   732
    {\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
   733
       {\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
   734
          {\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
   735
                 {\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
   736
                 &
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   737
           \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
   738
          }
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   739
       }
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
  \]
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
  However, while we obtained a theorem as result, this theorem is not
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   744
  yet stored in Isabelle's theorem database. So it cannot be referenced later
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   745
  on. How to store theorems will be explained in Section~\ref{sec:storing}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   746
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   747
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   748
  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
   749
  see \isccite{sec:thms}. The basic functions for theorems are defined in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   750
  @{ML_file "Pure/thm.ML"}. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   751
  \end{readmore}
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
  (FIXME: handy functions working on theorems, like @{ML_ind  rulify in ObjectLogic} and so on) 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   754
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   755
  (FIXME: @{ML_ind prove in Goal})
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   756
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   757
  (FIXME: how to add case-names to goal states - maybe in the 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   758
  next section)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   759
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   760
  (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
   761
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   762
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   763
ML {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   764
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
   765
      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
   766
  | 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
   767
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   768
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
   769
  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
   770
    if i <= length prems
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   771
    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
   772
         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
   773
    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
   774
      " 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
   775
  end;
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   776
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   777
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   778
ML {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   779
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
   780
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   781
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   782
setup %gray {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   783
  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
   784
  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
   785
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   786
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   787
lemma 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   788
  shows "A \<Longrightarrow> B"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   789
  and   "C \<Longrightarrow> D"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   790
oops
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   791
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   792
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   793
section {* Setups (TBD) *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   794
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   795
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   796
  In the previous section we used \isacommand{setup} in order to make
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   797
  a theorem attribute known to Isabelle. What happens behind the scenes
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   798
  is that \isacommand{setup} expects a function of type 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   799
  @{ML_type "theory -> theory"}: the input theory is the current theory and the 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   800
  output the theory where the theory attribute has been stored.
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
  This is a fundamental principle in Isabelle. A similar situation occurs 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   803
  for example with declaring constants. The function that declares a 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   804
  constant on the ML-level is @{ML_ind  add_consts_i in Sign}. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   805
  If you write\footnote{Recall that ML-code  needs to be 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   806
  enclosed in \isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"}.} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   807
*}  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   808
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   809
ML{*Sign.add_consts_i [(@{binding "BAR"}, @{typ "nat"}, NoSyn)] @{theory} *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   810
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   811
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   812
  for declaring the constant @{text "BAR"} with type @{typ nat} and 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   813
  run the code, then you indeed obtain a theory as result. But if you 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   814
  query the constant on the Isabelle level using the command \isacommand{term}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   815
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   816
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   817
  \isacommand{term}~@{text [quotes] "BAR"}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   818
  @{text "> \"BAR\" :: \"'a\""}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   819
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   820
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   821
  you do not obtain a constant of type @{typ nat}, but a free variable (printed in 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   822
  blue) of polymorphic type. The problem is that the ML-expression above did 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   823
  not register the declaration with the current theory. This is what the command
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   824
  \isacommand{setup} is for. The constant is properly declared with
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   825
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   826
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   827
setup %gray {* 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
   828
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   829
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   830
  Now 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   831
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   832
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   833
  \isacommand{term}~@{text [quotes] "BAR"}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   834
  @{text "> \"BAR\" :: \"nat\""}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   835
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   836
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   837
  returns a (black) constant with the type @{typ nat}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   838
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   839
  A similar command is \isacommand{local\_setup}, which expects a function
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   840
  of type @{ML_type "local_theory -> local_theory"}. Later on we will also
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   841
  use the commands \isacommand{method\_setup} for installing methods in the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   842
  current theory and \isacommand{simproc\_setup} for adding new simprocs to
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   843
  the current simpset.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   844
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   845
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   846
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
   847
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   848
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   849
  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
   850
  "[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
   851
  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
   852
  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
   853
  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
   854
  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
   855
  data structure. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   856
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   857
  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
   858
  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
   859
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   860
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   861
  \isacommand{print\_attributes}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   862
  @{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
   863
  @{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
   864
  @{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
   865
  @{text "> \<dots>"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   866
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   867
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   868
  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
   869
  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
   870
  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
   871
  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
   872
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   873
  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
   874
  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
   875
  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
   876
  this attribute is
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   877
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   878
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   879
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
   880
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   881
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   882
  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
   883
  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
   884
  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
   885
  @{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
   886
  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
   887
  @{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
   888
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   889
  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
   890
  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
   891
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   892
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   893
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
   894
  "applying the sym rule"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   895
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   896
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   897
  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
   898
  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
   899
  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
   900
  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
   901
  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
   902
*} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   903
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   904
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
   905
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   906
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   907
  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
   908
  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
   909
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   910
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   911
  \isacommand{thm}~@{text "test"}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   912
  @{text "> "}~@{thm test}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   913
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   914
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   915
  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
   916
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   917
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   918
  \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
   919
  @{text "> "}~@{thm test[my_sym]}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   920
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   921
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   922
  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
   923
  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
   924
  attribute as follows:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   925
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   926
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   927
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
   928
  "applying the sym rule" *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   929
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   930
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   931
  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
   932
  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
   933
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   934
  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
   935
  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
   936
  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
   937
  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
   938
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   939
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   940
ML{*fun MY_THEN thms = 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   941
  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
   942
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   943
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   944
  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
   945
  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
   946
  theorems. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   947
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   948
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   949
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
   950
  "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
   951
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   952
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   953
  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
   954
  meta-equation:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   955
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   956
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   957
  \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
   958
  @{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
   959
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   960
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   961
  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
   962
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   963
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   964
  \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
   965
  @{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
   966
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   967
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   968
  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
   969
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   970
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   971
  \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
   972
  @{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
   973
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   974
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   975
  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
   976
  of theorem attributes shows through: since theorem attributes can be
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   977
  arbitrary functions, they do not in general commute. If you try
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   978
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   979
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   980
  \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
   981
  @{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
   982
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   983
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   984
  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
   985
  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
   986
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   987
  The purpose of @{ML_ind  rule_attribute in Thm} is to directly manipulate theorems.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   988
  Another usage of theorem attributes is to add and delete theorems from stored data.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   989
  For example the theorem attribute @{text "[simp]"} adds or deletes a theorem from the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   990
  current simpset. For these applications, you can use @{ML_ind  declaration_attribute in Thm}. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   991
  To illustrate this function, let us introduce a reference containing a list
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   992
  of theorems.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   993
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   994
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   995
ML{*val my_thms = ref ([] : thm list)*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   996
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   997
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   998
  The purpose of this reference is to store a list of theorems.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   999
  We are going to modify it by adding and deleting theorems.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1000
  However, a word of warning: such references must not 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1001
  be used in any code that is meant to be more than just for testing purposes! 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1002
  Here it is only used to illustrate matters. We will show later how to store 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1003
  data properly without using references.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1004
 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1005
  We need to provide two functions that add and delete theorems from this list. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1006
  For this we use the two functions:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1007
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1008
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1009
ML{*fun my_thm_add thm ctxt =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1010
  (my_thms := Thm.add_thm thm (!my_thms); ctxt)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1011
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1012
fun my_thm_del thm ctxt =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1013
  (my_thms := Thm.del_thm thm (!my_thms); ctxt)*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1014
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1015
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1016
  These functions take a theorem and a context and, for what we are explaining
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1017
  here it is sufficient that they just return the context unchanged. They change
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1018
  however the reference @{ML my_thms}, whereby the function 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1019
  @{ML_ind  add_thm in Thm} adds a theorem if it is not already included in 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1020
  the list, and @{ML_ind  del_thm in Thm} deletes one (both functions use the 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1021
  predicate @{ML_ind  eq_thm_prop in Thm}, which compares theorems according to 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1022
  their proved propositions modulo alpha-equivalence).
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1023
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1024
  You can turn functions @{ML my_thm_add} and @{ML my_thm_del} into 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1025
  attributes with the code
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1026
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1027
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1028
ML{*val my_add = Thm.declaration_attribute my_thm_add
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1029
val my_del = Thm.declaration_attribute my_thm_del *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1030
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1031
text {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1032
  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
  1033
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1034
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1035
attribute_setup %gray my_thms = {* Attrib.add_del my_add my_del *} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1036
  "maintaining a list of my_thms - rough test only!" 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1037
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1038
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1039
  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
  1040
  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
  1041
  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
  1042
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1043
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1044
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
  1045
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1046
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1047
  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
  1048
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1049
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1050
  "!my_thms" "[\"True\"]"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1051
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1052
  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
  1053
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1054
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1055
declare test[my_thms] trueI_2[my_thms add] 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1056
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1057
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1058
  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
  1059
  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
  1060
  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
  1061
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1062
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1063
  "!my_thms"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1064
  "[\"True\", \"Suc (Suc 0) = 2\"]"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1065
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1066
  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
  1067
  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
  1068
  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
  1069
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1070
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1071
declare test[my_thms del]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1072
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1073
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
  1074
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1075
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1076
  "!my_thms"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1077
  "[\"True\"]"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1078
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1079
  We used in this example two functions declared as @{ML_ind  declaration_attribute in Thm}, 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1080
  but there can be any number of them. We just have to change the parser for reading
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1081
  the arguments accordingly. 
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
  However, as said at the beginning of this example, using references for storing theorems is
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1084
  \emph{not} the received way of doing such things. The received way is to 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1085
  start a ``data slot'', below called @{text MyThmsData}, generated by the functor 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1086
  @{text GenericDataFun}:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1087
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1088
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1089
ML{*structure MyThmsData = GenericDataFun
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1090
 (type T = thm list
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1091
  val empty = []
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1092
  val extend = I
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1093
  fun merge _ = Thm.merge_thms) *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1094
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1095
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1096
  The type @{text "T"} of this data slot is @{ML_type "thm list"}.\footnote{FIXME: give a pointer
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1097
  to where data slots are explained properly.}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1098
  To use this data slot, you only have to change @{ML my_thm_add} and
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1099
  @{ML my_thm_del} to:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1100
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1101
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1102
ML{*val my_thm_add = MyThmsData.map o Thm.add_thm
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1103
val my_thm_del = MyThmsData.map o Thm.del_thm*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1104
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1105
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1106
  where @{ML MyThmsData.map} updates the data appropriately. The
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1107
  corresponding theorem attributes are
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
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1110
ML{*val my_add = Thm.declaration_attribute my_thm_add
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1111
val my_del = Thm.declaration_attribute my_thm_del *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1112
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1113
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1114
  and the setup is as follows
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1115
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1116
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1117
attribute_setup %gray my_thms2 = {* Attrib.add_del my_add my_del *} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1118
  "properly maintaining a list of my_thms"
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
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1121
  Initially, the data slot is empty 
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
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1124
  "MyThmsData.get (Context.Proof @{context})"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1125
  "[]"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1126
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1127
  but if you prove
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1128
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1129
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1130
lemma three[my_thms2]: "3 = Suc (Suc (Suc 0))" by simp
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1131
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1132
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1133
  then the lemma is recorded. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1134
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1135
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1136
  "MyThmsData.get (Context.Proof @{context})"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1137
  "[\"3 = Suc (Suc (Suc 0))\"]"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1138
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1139
  With theorem attribute @{text my_thms2} you can also nicely see why it 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1140
  is important to 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1141
  store data in a ``data slot'' and \emph{not} in a reference. Backtrack
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1142
  to the point just before the lemma @{thm [source] three} was proved and 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1143
  check the the content of @{ML_struct MyThmsData}: it should be empty. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1144
  The addition has been properly retracted. Now consider the proof:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1145
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1146
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1147
lemma four[my_thms]: "4 = Suc (Suc (Suc (Suc 0)))" by simp
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1148
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1149
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1150
  Checking the content of @{ML my_thms} gives
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
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1153
  "!my_thms"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1154
  "[\"4 = Suc (Suc (Suc (Suc 0)))\", \"True\"]"}
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
  as expected, but if you backtrack before the lemma @{thm [source] four}, the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1157
  content of @{ML my_thms} is unchanged. The backtracking mechanism
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1158
  of Isabelle is completely oblivious about what to do with references, but
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1159
  properly treats ``data slots''!
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1160
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1161
  Since storing theorems in a list is such a common task, there is the special
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1162
  functor @{ML_functor Named_Thms}, which does most of the work for you. To obtain
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1163
  a named theorem list, you just declare
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1164
*}
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
ML{*structure FooRules = Named_Thms
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1167
 (val name = "foo" 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1168
  val description = "Rules for foo") *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1169
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1170
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1171
  and set up the @{ML_struct FooRules} with the command
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
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1174
setup %gray {* FooRules.setup *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1175
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1176
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1177
  This code declares a data slot where the theorems are stored,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1178
  an attribute @{text foo} (with the @{text add} and @{text del} options
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1179
  for adding and deleting theorems) and an internal ML interface to retrieve and 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1180
  modify the theorems.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1181
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1182
  Furthermore, the facts are made available on the user-level under the dynamic 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1183
  fact name @{text foo}. For example you can declare three lemmas to be of the kind
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1184
  @{text foo} by:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1185
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1186
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1187
lemma rule1[foo]: "A" sorry
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1188
lemma rule2[foo]: "B" sorry
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1189
lemma rule3[foo]: "C" sorry
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1190
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1191
text {* and undeclare the first one by: *}
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
declare rule1[foo del]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1194
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1195
text {* and query the remaining ones with:
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
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1198
  \isacommand{thm}~@{text "foo"}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1199
  @{text "> ?C"}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1200
  @{text "> ?B"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1201
  \end{isabelle}
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
  On the ML-level the rules marked with @{text "foo"} can be retrieved
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1204
  using the function @{ML FooRules.get}:
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
  @{ML_response_fake [display,gray] "FooRules.get @{context}" "[\"?C\",\"?B\"]"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1207
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1208
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1209
  For more information see @{ML_file "Pure/Tools/named_thms.ML"}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1210
  \end{readmore}
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
  (FIXME What are: @{text "theory_attributes"}, @{text "proof_attributes"}?)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1213
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
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1216
  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
  1217
  @{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
  1218
  parsing.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1219
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1220
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1221
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1222
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
section {* Theories, Contexts and Local Theories (TBD) *}
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
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1227
  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
  1228
  want to order them). 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1229
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1230
  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
  1231
  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
  1232
  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
  1233
  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
  1234
  @{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
  1235
  @{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
  1236
  valid local theory.
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
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1239
(*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1240
ML{*signature UNIVERSAL_TYPE =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1241
sig
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1242
  type t
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1243
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1244
  val embed: unit -> ('a -> t) * (t -> 'a option)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1245
end*}
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
ML{*structure U:> UNIVERSAL_TYPE =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1248
   struct
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1249
      type t = exn
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1250
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1251
      fun 'a embed () =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1252
         let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1253
            exception E of 'a
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1254
            fun project (e: t): 'a option =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1255
               case e of
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1256
                  E a => SOME a
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1257
                | _ => NONE
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1258
         in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1259
            (E, project)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1260
         end
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1261
   end*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1262
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1263
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1264
  The idea is that type t is the universal type and that each call to embed
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1265
  returns a new pair of functions (inject, project), where inject embeds a
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1266
  value into the universal type and project extracts the value from the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1267
  universal type. A pair (inject, project) returned by embed works together in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1268
  that project u will return SOME v if and only if u was created by inject
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1269
  v. If u was created by a different function inject', then project returns
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1270
  NONE.
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
  in library.ML
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1273
*}
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
ML_val{*structure Object = struct type T = exn end; *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1276
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1277
ML{*functor Test (U: UNIVERSAL_TYPE): sig end =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1278
   struct
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1279
      val (intIn: int -> U.t, intOut) = U.embed ()
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1280
      val r: U.t ref = ref (intIn 13)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1281
      val s1 =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1282
         case intOut (!r) of
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1283
            NONE => "NONE"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1284
          | SOME i => Int.toString i
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1285
      val (realIn: real -> U.t, realOut) = U.embed ()
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1286
      val () = r := realIn 13.0
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1287
      val s2 =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1288
         case intOut (!r) of
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1289
            NONE => "NONE"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1290
          | SOME i => Int.toString i
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1291
      val s3 =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1292
         case realOut (!r) of
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1293
            NONE => "NONE"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1294
          | SOME x => Real.toString x
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1295
      val () = tracing (concat [s1, " ", s2, " ", s3, "\n"])
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1296
   end*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1297
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1298
ML_val{*structure t = Test(U) *} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1299
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1300
ML_val{*structure Datatab = TableFun(type key = int val ord = int_ord);*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1301
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1302
ML {* LocalTheory.restore *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1303
ML {* LocalTheory.set_group *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1304
*)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1305
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1306
section {* Storing Theorems\label{sec:storing} (TBD) *}
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
text {* @{ML_ind  add_thms_dynamic in PureThy} *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1309
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1310
local_setup %gray {* 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1311
  LocalTheory.note Thm.theoremK 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1312
    ((@{binding "allI_alt"}, []), [@{thm allI}]) #> snd *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1313
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
(* FIXME: some code below *)
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
(*<*)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1318
(*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1319
setup {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1320
 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
  1321
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1322
*)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1323
lemma "bar = (1::nat)"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1324
  oops
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1325
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
setup {*   
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1328
  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
  1329
 #> 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
  1330
       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
  1331
 #> snd
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
*)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1334
(*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1335
lemma "foo = (1::nat)"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1336
  apply(simp add: foo_def)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1337
  done
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1338
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1339
thm foo_def
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1340
*)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1341
(*>*)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1342
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1343
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
  1344
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1345
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1346
  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
  1347
  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
  1348
  sufficient for ``quick-and-dirty'' printouts. For something more
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1349
  sophisticated, Isabelle includes an infrastructure for properly formatting text.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1350
  This infrastructure is loosely based on a paper by Oppen~\cite{Oppen80}. Most of
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1351
  its functions do not operate on @{ML_type string}s, but on instances of the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1352
  type:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1353
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1354
  @{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
  1355
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1356
  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
  1357
  type. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1358
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1359
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1360
  "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
  1361
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1362
  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
  1363
  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
  1364
  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
  1365
  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
  1366
  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
  1367
  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
  1368
  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
  1369
  type:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1370
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1371
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1372
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
  1373
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1374
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1375
  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
  1376
  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
  1377
  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
  1378
  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
  1379
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1380
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1381
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
  1382
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1383
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1384
  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
  1385
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1386
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1387
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
  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
  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
  1391
  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
  1392
  we obtain the ugly output:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1393
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1394
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1395
"tracing test_str"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1396
"fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar foooooooooo
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1397
ooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaa
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1398
aaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fo
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1399
oooooooooooooobaaaaaaaaaaaar"}
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
  We obtain the same if we just use
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1402
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1403
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1404
"pprint (Pretty.str test_str)"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1405
"fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar foooooooooo
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1406
ooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaa
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1407
aaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fo
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1408
oooooooooooooobaaaaaaaaaaaar"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1409
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1410
  However by using pretty types you have the ability to indicate a possible
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1411
  line break for example at each space. You can achieve this with the function
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1412
  @{ML_ind  breaks in Pretty}, which expects a list of pretty types and inserts a
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1413
  possible line break in between every two elements in this list. To print
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1414
  this list of pretty types as a single string, we concatenate them 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1415
  with the function @{ML_ind  blk in Pretty} as follows:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1416
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1417
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1418
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1419
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1420
  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
  1421
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1422
  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
  1423
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1424
"fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1425
fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1426
fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1427
fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1428
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1429
  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
  1430
  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
  1431
  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
  1432
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1433
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1434
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1435
  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
  1436
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1437
  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
  1438
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1439
"fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1440
   fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1441
   fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1442
   fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1443
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1444
  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
  1445
  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
  1446
  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
  1447
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1448
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1449
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1450
  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
  1451
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1452
  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
  1453
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1454
"          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1455
          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1456
          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1457
          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1458
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1459
  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
  1460
  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
  1461
  @{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
  1462
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1463
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1464
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1465
  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
  1466
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1467
  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
  1468
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1469
"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
  1470
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
  1471
100016, 100017, 100018, 100019, 100020"}
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
  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
  1474
  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
  1475
  @{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
  1476
  @{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
  1477
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1478
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1479
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1480
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1481
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1482
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1483
  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
  1484
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1485
  pprint (Pretty.enum \",\" \"{\" \"}\" ptrs)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1486
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1487
"{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
  1488
  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
  1489
  100016, 100017, 100018, 100019, 100020}"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1490
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1491
  As can be seen, this function prints out the ``set'' so that starting 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1492
  from the second, each new line as an indentation of 2.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1493
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1494
  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
  1495
  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
  1496
  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
  1497
  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
  1498
  write the function
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1499
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1500
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1501
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1502
ML %linenosgray{*fun and_list [] = []
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1503
  | and_list [x] = [x]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1504
  | and_list xs = 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1505
      let 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1506
        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
  1507
      in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1508
        (Pretty.commas front) @ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1509
        [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
  1510
      end *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1511
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1512
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1513
  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
  1514
  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
  1515
  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
  1516
  @{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
  1517
  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
  1518
  for example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1519
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1520
@{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1521
"let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1522
  val ptrs = map (Pretty.str o string_of_int) (1 upto 22)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1523
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1524
  pprint (Pretty.blk (0, and_list ptrs))
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1525
end"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1526
"1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1527
and 22"}
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
  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
  1530
  function @{text "tell_type"}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1531
*}
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 %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
  1534
let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1535
  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
  1536
  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
  1537
  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
  1538
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1539
  pprint (Pretty.blk (0, 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1540
    (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
  1541
      @ [pty, Pretty.str "."])))
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1542
end*}
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
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1545
  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
  1546
  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
  1547
  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
  1548
  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
  1549
  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
  1550
  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
  1551
  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
  1552
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1553
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1554
  Now you can write
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1555
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1556
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1557
  "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
  1558
  "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
  1559
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1560
  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
  1561
  and type. 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
  @{ML_response_fake [display,gray]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1564
  "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
  1565
  "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
  1566
\"((((('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
  1567
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1568
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1569
  FIXME: TBD below
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1570
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1571
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1572
ML {* pprint (Pretty.big_list "header"  (map (Pretty.str o string_of_int) (4 upto 10))) *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1573
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1574
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1575
chunks inserts forced breaks (unlike blk where you have to do this yourself)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1576
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1577
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1578
ML {* (Pretty.chunks [Pretty.str "a", Pretty.str "b"], 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1579
       Pretty.blk (0, [Pretty.str "a", Pretty.str "b"])) *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1580
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1581
ML {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1582
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
  1583
   setmp show_all_types
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1584
         (! 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
  1585
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1586
val ctxt = @{context};
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1587
val t1 = @{term "undefined::nat"};
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1588
val t2 = @{term "Suc y"};
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1589
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
  1590
             [(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
  1591
                  (Syntax.pretty_term ctxt) t1,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1592
              Pretty.str "=", Syntax.pretty_term ctxt t2]);
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1593
pty |> Pretty.string_of |> priority
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1594
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1595
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1596
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
  1597
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1598
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1599
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
  1600
ML {* (Pretty.str "bar") |> Pretty.string_of |> tracing *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1601
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1602
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1603
ML {* Pretty.mark Markup.subgoal (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
  1604
ML {* (Pretty.str "bar") |> Pretty.string_of |> tracing *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1605
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1606
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1607
  Still to be done:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1608
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1609
  What happens with big formulae?
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1610
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1611
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1612
  The general infrastructure for pretty-printing is implemented in the file
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1613
  @{ML_file "Pure/General/pretty.ML"}. The file @{ML_file "Pure/Syntax/syntax.ML"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1614
  contains pretty-printing functions for terms, types, theorems and so on.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1615
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1616
  @{ML_file "Pure/General/markup.ML"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1617
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1618
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1619
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1620
text {*
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1621
  printing into the goal buffer as part of the proof state
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1622
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1623
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1624
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1625
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
  1626
ML {* (Pretty.str "bar") |> Pretty.string_of |> tracing *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1627
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1628
text {* writing into the goal buffer *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1629
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1630
ML {* fun my_hook interactive state =
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1631
         (interactive ? Proof.goal_message (fn () => Pretty.str  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1632
"foo")) state
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1633
*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1634
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1635
setup %gray {* Context.theory_map (Specification.add_theorem_hook my_hook) *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1636
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1637
lemma "False"
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1638
oops
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1639
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1640
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1641
section {* Misc (TBD) *}
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 {*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
  1644
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1645
section {* Name Space Issues (TBD) *}
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
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1648
end