doc-src/Logics/CTT.tex
author wenzelm
Mon, 02 May 2011 22:31:46 +0200
changeset 43508 381fdcab0f36
parent 12679 8ed660138f83
child 43890 99985426c0bb
permissions -rw-r--r--
eliminated old CVS Ids;
lcp@104
     1
\chapter{Constructive Type Theory}
lcp@314
     2
\index{Constructive Type Theory|(}
lcp@314
     3
paulson@7159
     4
\underscoreoff %this file contains _ in rule names
paulson@7159
     5
lcp@104
     6
Martin-L\"of's Constructive Type Theory \cite{martinlof84,nordstrom90} can
lcp@104
     7
be viewed at many different levels.  It is a formal system that embodies
lcp@104
     8
the principles of intuitionistic mathematics; it embodies the
lcp@104
     9
interpretation of propositions as types; it is a vehicle for deriving
lcp@314
    10
programs from proofs.  
lcp@314
    11
lcp@314
    12
Thompson's book~\cite{thompson91} gives a readable and thorough account of
lcp@314
    13
Type Theory.  Nuprl is an elaborate implementation~\cite{constable86}.
lcp@314
    14
{\sc alf} is a more recent tool that allows proof terms to be edited
lcp@314
    15
directly~\cite{alf}.
lcp@104
    16
lcp@104
    17
Isabelle's original formulation of Type Theory was a kind of sequent
lcp@104
    18
calculus, following Martin-L\"of~\cite{martinlof84}.  It included rules for
lcp@104
    19
building the context, namely variable bindings with their types.  A typical
lcp@104
    20
judgement was
lcp@104
    21
\[   a(x@1,\ldots,x@n)\in A(x@1,\ldots,x@n) \; 
lcp@104
    22
    [ x@1\in A@1, x@2\in A@2(x@1), \ldots, x@n\in A@n(x@1,\ldots,x@{n-1}) ]
lcp@104
    23
\]
lcp@104
    24
This sequent calculus was not satisfactory because assumptions like
lcp@104
    25
`suppose $A$ is a type' or `suppose $B(x)$ is a type for all $x$ in $A$'
lcp@104
    26
could not be formalized.  
lcp@104
    27
lcp@314
    28
The theory~\thydx{CTT} implements Constructive Type Theory, using
lcp@104
    29
natural deduction.  The judgement above is expressed using $\Forall$ and
lcp@104
    30
$\Imp$:
lcp@104
    31
\[ \begin{array}{r@{}l}
lcp@104
    32
     \Forall x@1\ldots x@n. &
lcp@111
    33
          \List{x@1\in A@1; 
lcp@111
    34
                x@2\in A@2(x@1); \cdots \; 
lcp@111
    35
                x@n\in A@n(x@1,\ldots,x@{n-1})} \Imp \\
lcp@104
    36
     &  \qquad\qquad a(x@1,\ldots,x@n)\in A(x@1,\ldots,x@n) 
lcp@104
    37
    \end{array}
lcp@104
    38
\]
lcp@104
    39
Assumptions can use all the judgement forms, for instance to express that
lcp@104
    40
$B$ is a family of types over~$A$:
lcp@104
    41
\[ \Forall x . x\in A \Imp B(x)\;{\rm type} \]
wenzelm@9695
    42
To justify the CTT formulation it is probably best to appeal directly to the
wenzelm@9695
    43
semantic explanations of the rules~\cite{martinlof84}, rather than to the
wenzelm@9695
    44
rules themselves.  The order of assumptions no longer matters, unlike in
wenzelm@9695
    45
standard Type Theory.  Contexts, which are typical of many modern type
wenzelm@9695
    46
theories, are difficult to represent in Isabelle.  In particular, it is
wenzelm@9695
    47
difficult to enforce that all the variables in a context are distinct.
wenzelm@9695
    48
\index{assumptions!in CTT}
lcp@104
    49
wenzelm@9695
    50
The theory does not use polymorphism.  Terms in CTT have type~\tydx{i}, the
wenzelm@9695
    51
type of individuals.  Types in CTT have type~\tydx{t}.
lcp@104
    52
lcp@314
    53
\begin{figure} \tabcolsep=1em  %wider spacing in tables
lcp@314
    54
\begin{center}
lcp@314
    55
\begin{tabular}{rrr} 
lcp@314
    56
  \it name      & \it meta-type         & \it description \\ 
lcp@314
    57
  \cdx{Type}    & $t \to prop$          & judgement form \\
lcp@314
    58
  \cdx{Eqtype}  & $[t,t]\to prop$       & judgement form\\
lcp@314
    59
  \cdx{Elem}    & $[i, t]\to prop$      & judgement form\\
lcp@314
    60
  \cdx{Eqelem}  & $[i, i, t]\to prop$   & judgement form\\
lcp@314
    61
  \cdx{Reduce}  & $[i, i]\to prop$      & extra judgement form\\[2ex]
lcp@314
    62
lcp@314
    63
  \cdx{N}       &     $t$               & natural numbers type\\
lcp@314
    64
  \cdx{0}       &     $i$               & constructor\\
lcp@314
    65
  \cdx{succ}    & $i\to i$              & constructor\\
lcp@314
    66
  \cdx{rec}     & $[i,i,[i,i]\to i]\to i$       & eliminator\\[2ex]
lcp@314
    67
  \cdx{Prod}    & $[t,i\to t]\to t$     & general product type\\
lcp@314
    68
  \cdx{lambda}  & $(i\to i)\to i$       & constructor\\[2ex]
lcp@314
    69
  \cdx{Sum}     & $[t, i\to t]\to t$    & general sum type\\
lcp@314
    70
  \cdx{pair}    & $[i,i]\to i$          & constructor\\
lcp@314
    71
  \cdx{split}   & $[i,[i,i]\to i]\to i$ & eliminator\\
lcp@314
    72
  \cdx{fst} \cdx{snd} & $i\to i$        & projections\\[2ex]
lcp@314
    73
  \cdx{inl} \cdx{inr} & $i\to i$        & constructors for $+$\\
lcp@314
    74
  \cdx{when}    & $[i,i\to i, i\to i]\to i$    & eliminator for $+$\\[2ex]
lcp@314
    75
  \cdx{Eq}      & $[t,i,i]\to t$        & equality type\\
lcp@314
    76
  \cdx{eq}      & $i$                   & constructor\\[2ex]
lcp@314
    77
  \cdx{F}       & $t$                   & empty type\\
lcp@314
    78
  \cdx{contr}   & $i\to i$              & eliminator\\[2ex]
lcp@314
    79
  \cdx{T}       & $t$                   & singleton type\\
lcp@314
    80
  \cdx{tt}      & $i$                   & constructor
lcp@314
    81
\end{tabular}
lcp@314
    82
\end{center}
wenzelm@9695
    83
\caption{The constants of CTT} \label{ctt-constants}
lcp@314
    84
\end{figure}
lcp@314
    85
lcp@314
    86
wenzelm@9695
    87
CTT supports all of Type Theory apart from list types, well-ordering types,
wenzelm@9695
    88
and universes.  Universes could be introduced {\em\`a la Tarski}, adding new
wenzelm@9695
    89
constants as names for types.  The formulation {\em\`a la Russell}, where
wenzelm@9695
    90
types denote themselves, is only possible if we identify the meta-types~{\tt
wenzelm@9695
    91
  i} and~{\tt t}.  Most published formulations of well-ordering types have
wenzelm@9695
    92
difficulties involving extensionality of functions; I suggest that you use
wenzelm@9695
    93
some other method for defining recursive types.  List types are easy to
wenzelm@9695
    94
introduce by declaring new rules.
lcp@104
    95
wenzelm@9695
    96
CTT uses the 1982 version of Type Theory, with extensional equality.  The
wenzelm@9695
    97
computation $a=b\in A$ and the equality $c\in Eq(A,a,b)$ are interchangeable.
wenzelm@9695
    98
Its rewriting tactics prove theorems of the form $a=b\in A$.  It could be
wenzelm@9695
    99
modified to have intensional equality, but rewriting tactics would have to
wenzelm@9695
   100
prove theorems of the form $c\in Eq(A,a,b)$ and the computation rules might
wenzelm@9695
   101
require a separate simplifier.
lcp@104
   102
lcp@104
   103
lcp@104
   104
\begin{figure} \tabcolsep=1em  %wider spacing in tables
wenzelm@9695
   105
\index{lambda abs@$\lambda$-abstractions!in CTT}
lcp@104
   106
\begin{center}
lcp@104
   107
\begin{tabular}{llrrr} 
lcp@314
   108
  \it symbol &\it name     &\it meta-type & \it priority & \it description \\
lcp@314
   109
  \sdx{lam} & \cdx{lambda}  & $(i\To o)\To i$ & 10 & $\lambda$-abstraction
lcp@104
   110
\end{tabular}
lcp@104
   111
\end{center}
lcp@104
   112
\subcaption{Binders} 
lcp@104
   113
lcp@104
   114
\begin{center}
wenzelm@9695
   115
\index{*"` symbol}\index{function applications!in CTT}
lcp@314
   116
\index{*"+ symbol}
lcp@104
   117
\begin{tabular}{rrrr} 
lcp@314
   118
  \it symbol & \it meta-type    & \it priority & \it description \\ 
lcp@111
   119
  \tt `         & $[i,i]\to i$  & Left 55       & function application\\
lcp@111
   120
  \tt +         & $[t,t]\to t$  & Right 30      & sum of two types
lcp@104
   121
\end{tabular}
lcp@104
   122
\end{center}
lcp@104
   123
\subcaption{Infixes}
lcp@104
   124
lcp@314
   125
\index{*"* symbol}
lcp@314
   126
\index{*"-"-"> symbol}
lcp@104
   127
\begin{center} \tt\frenchspacing
lcp@104
   128
\begin{tabular}{rrr} 
lcp@111
   129
  \it external                  & \it internal  & \it standard notation \\ 
paulson@5151
   130
  \sdx{PROD} $x$:$A$ . $B[x]$   &  Prod($A$, $\lambda x. B[x]$) &
lcp@111
   131
        \rm product $\prod@{x\in A}B[x]$ \\
paulson@5151
   132
  \sdx{SUM} $x$:$A$ . $B[x]$    & Sum($A$, $\lambda x. B[x]$) &
lcp@111
   133
        \rm sum $\sum@{x\in A}B[x]$ \\
paulson@5151
   134
  $A$ --> $B$     &  Prod($A$, $\lambda x. B$) &
lcp@111
   135
        \rm function space $A\to B$ \\
paulson@5151
   136
  $A$ * $B$       &  Sum($A$, $\lambda x. B$)  &
lcp@111
   137
        \rm binary product $A\times B$
lcp@104
   138
\end{tabular}
lcp@104
   139
\end{center}
lcp@104
   140
\subcaption{Translations} 
lcp@104
   141
lcp@314
   142
\index{*"= symbol}
lcp@104
   143
\begin{center}
lcp@104
   144
\dquotes
lcp@104
   145
\[ \begin{array}{rcl}
lcp@111
   146
prop    & = &  type " type"       \\
lcp@111
   147
        & | & type " = " type     \\
lcp@111
   148
        & | & term " : " type        \\
lcp@111
   149
        & | & term " = " term " : " type 
lcp@104
   150
\\[2ex]
lcp@111
   151
type    & = & \hbox{expression of type~$t$} \\
lcp@111
   152
        & | & "PROD~" id " : " type " . " type  \\
lcp@111
   153
        & | & "SUM~~" id " : " type " . " type 
lcp@104
   154
\\[2ex]
lcp@111
   155
term    & = & \hbox{expression of type~$i$} \\
lcp@111
   156
        & | & "lam " id~id^* " . " term   \\
lcp@111
   157
        & | & "< " term " , " term " >"
lcp@104
   158
\end{array} 
lcp@104
   159
\]
lcp@104
   160
\end{center}
lcp@104
   161
\subcaption{Grammar}
wenzelm@9695
   162
\caption{Syntax of CTT} \label{ctt-syntax}
lcp@104
   163
\end{figure}
lcp@104
   164
lcp@104
   165
%%%%\section{Generic Packages}  typedsimp.ML????????????????
lcp@104
   166
lcp@104
   167
lcp@104
   168
\section{Syntax}
lcp@284
   169
The constants are shown in Fig.\ts\ref{ctt-constants}.  The infixes include
wenzelm@9695
   170
the function application operator (sometimes called `apply'), and the 2-place
wenzelm@9695
   171
type operators.  Note that meta-level abstraction and application, $\lambda
wenzelm@9695
   172
x.b$ and $f(a)$, differ from object-level abstraction and application,
wenzelm@9695
   173
\hbox{\tt lam $x$. $b$} and $b{\tt`}a$.  A CTT function~$f$ is simply an
wenzelm@9695
   174
individual as far as Isabelle is concerned: its Isabelle type is~$i$, not say
wenzelm@9695
   175
$i\To i$.
lcp@104
   176
wenzelm@9695
   177
The notation for~CTT (Fig.\ts\ref{ctt-syntax}) is based on that of Nordstr\"om
wenzelm@9695
   178
et al.~\cite{nordstrom90}.  The empty type is called $F$ and the one-element
wenzelm@9695
   179
type is $T$; other finite types are built as $T+T+T$, etc.
lcp@314
   180
lcp@314
   181
\index{*SUM symbol}\index{*PROD symbol}
paulson@6072
   182
Quantification is expressed by sums $\sum@{x\in A}B[x]$ and
lcp@314
   183
products $\prod@{x\in A}B[x]$.  Instead of {\tt Sum($A$,$B$)} and {\tt
paulson@6072
   184
  Prod($A$,$B$)} we may write \hbox{\tt SUM $x$:$A$.\ $B[x]$} and \hbox{\tt
paulson@6072
   185
  PROD $x$:$A$.\ $B[x]$}.  For example, we may write
lcp@104
   186
\begin{ttbox}
lcp@284
   187
SUM y:B. PROD x:A. C(x,y)   {\rm for}   Sum(B, \%y. Prod(A, \%x. C(x,y)))
lcp@104
   188
\end{ttbox}
lcp@104
   189
The special cases as \hbox{\tt$A$*$B$} and \hbox{\tt$A$-->$B$} abbreviate
lcp@104
   190
general sums and products over a constant family.\footnote{Unlike normal
lcp@104
   191
infix operators, {\tt*} and {\tt-->} merely define abbreviations; there are
lcp@104
   192
no constants~{\tt op~*} and~\hbox{\tt op~-->}.}  Isabelle accepts these
lcp@104
   193
abbreviations in parsing and uses them whenever possible for printing.
lcp@104
   194
lcp@104
   195
lcp@104
   196
\begin{figure} 
lcp@104
   197
\begin{ttbox}
lcp@314
   198
\tdx{refl_type}         A type ==> A = A
lcp@314
   199
\tdx{refl_elem}         a : A ==> a = a : A
lcp@104
   200
lcp@314
   201
\tdx{sym_type}          A = B ==> B = A
lcp@314
   202
\tdx{sym_elem}          a = b : A ==> b = a : A
lcp@104
   203
lcp@314
   204
\tdx{trans_type}        [| A = B;  B = C |] ==> A = C
lcp@314
   205
\tdx{trans_elem}        [| a = b : A;  b = c : A |] ==> a = c : A
lcp@104
   206
lcp@314
   207
\tdx{equal_types}       [| a : A;  A = B |] ==> a : B
lcp@314
   208
\tdx{equal_typesL}      [| a = b : A;  A = B |] ==> a = b : B
lcp@104
   209
lcp@314
   210
\tdx{subst_type}        [| a : A;  !!z. z:A ==> B(z) type |] ==> B(a) type
lcp@314
   211
\tdx{subst_typeL}       [| a = c : A;  !!z. z:A ==> B(z) = D(z) 
lcp@104
   212
                  |] ==> B(a) = D(c)
lcp@104
   213
lcp@314
   214
\tdx{subst_elem}        [| a : A;  !!z. z:A ==> b(z):B(z) |] ==> b(a):B(a)
lcp@314
   215
\tdx{subst_elemL}       [| a = c : A;  !!z. z:A ==> b(z) = d(z) : B(z) 
lcp@104
   216
                  |] ==> b(a) = d(c) : B(a)
lcp@104
   217
lcp@314
   218
\tdx{refl_red}          Reduce(a,a)
lcp@314
   219
\tdx{red_if_equal}      a = b : A ==> Reduce(a,b)
lcp@314
   220
\tdx{trans_red}         [| a = b : A;  Reduce(b,c) |] ==> a = c : A
lcp@104
   221
\end{ttbox}
lcp@104
   222
\caption{General equality rules} \label{ctt-equality}
lcp@104
   223
\end{figure}
lcp@104
   224
lcp@104
   225
lcp@104
   226
\begin{figure} 
lcp@104
   227
\begin{ttbox}
lcp@314
   228
\tdx{NF}        N type
lcp@104
   229
lcp@314
   230
\tdx{NI0}       0 : N
lcp@314
   231
\tdx{NI_succ}   a : N ==> succ(a) : N
lcp@314
   232
\tdx{NI_succL}  a = b : N ==> succ(a) = succ(b) : N
lcp@104
   233
lcp@314
   234
\tdx{NE}        [| p: N;  a: C(0);  
lcp@104
   235
             !!u v. [| u: N; v: C(u) |] ==> b(u,v): C(succ(u)) 
paulson@5151
   236
          |] ==> rec(p, a, \%u v. b(u,v)) : C(p)
lcp@104
   237
lcp@314
   238
\tdx{NEL}       [| p = q : N;  a = c : C(0);  
lcp@104
   239
             !!u v. [| u: N; v: C(u) |] ==> b(u,v)=d(u,v): C(succ(u))
paulson@5151
   240
          |] ==> rec(p, a, \%u v. b(u,v)) = rec(q,c,d) : C(p)
lcp@104
   241
lcp@314
   242
\tdx{NC0}       [| a: C(0);  
lcp@104
   243
             !!u v. [| u: N; v: C(u) |] ==> b(u,v): C(succ(u))
paulson@5151
   244
          |] ==> rec(0, a, \%u v. b(u,v)) = a : C(0)
lcp@104
   245
lcp@314
   246
\tdx{NC_succ}   [| p: N;  a: C(0);  
lcp@104
   247
             !!u v. [| u: N; v: C(u) |] ==> b(u,v): C(succ(u)) 
paulson@5151
   248
          |] ==> rec(succ(p), a, \%u v. b(u,v)) =
paulson@5151
   249
                 b(p, rec(p, a, \%u v. b(u,v))) : C(succ(p))
lcp@104
   250
lcp@314
   251
\tdx{zero_ne_succ}      [| a: N;  0 = succ(a) : N |] ==> 0: F
lcp@104
   252
\end{ttbox}
lcp@104
   253
\caption{Rules for type~$N$} \label{ctt-N}
lcp@104
   254
\end{figure}
lcp@104
   255
lcp@104
   256
lcp@104
   257
\begin{figure} 
lcp@104
   258
\begin{ttbox}
paulson@5151
   259
\tdx{ProdF}     [| A type; !!x. x:A ==> B(x) type |] ==> PROD x:A. B(x) type
lcp@314
   260
\tdx{ProdFL}    [| A = C;  !!x. x:A ==> B(x) = D(x) |] ==> 
paulson@5151
   261
          PROD x:A. B(x) = PROD x:C. D(x)
lcp@104
   262
lcp@314
   263
\tdx{ProdI}     [| A type;  !!x. x:A ==> b(x):B(x)
paulson@5151
   264
          |] ==> lam x. b(x) : PROD x:A. B(x)
lcp@314
   265
\tdx{ProdIL}    [| A type;  !!x. x:A ==> b(x) = c(x) : B(x)
paulson@5151
   266
          |] ==> lam x. b(x) = lam x. c(x) : PROD x:A. B(x)
lcp@104
   267
paulson@5151
   268
\tdx{ProdE}     [| p : PROD x:A. B(x);  a : A |] ==> p`a : B(a)
paulson@5151
   269
\tdx{ProdEL}    [| p=q: PROD x:A. B(x);  a=b : A |] ==> p`a = q`b : B(a)
lcp@104
   270
lcp@314
   271
\tdx{ProdC}     [| a : A;  !!x. x:A ==> b(x) : B(x)
paulson@5151
   272
          |] ==> (lam x. b(x)) ` a = b(a) : B(a)
lcp@104
   273
paulson@5151
   274
\tdx{ProdC2}    p : PROD x:A. B(x) ==> (lam x. p`x) = p : PROD x:A. B(x)
lcp@104
   275
\end{ttbox}
lcp@314
   276
\caption{Rules for the product type $\prod\sb{x\in A}B[x]$} \label{ctt-prod}
lcp@104
   277
\end{figure}
lcp@104
   278
lcp@104
   279
lcp@104
   280
\begin{figure} 
lcp@104
   281
\begin{ttbox}
paulson@5151
   282
\tdx{SumF}      [| A type;  !!x. x:A ==> B(x) type |] ==> SUM x:A. B(x) type
lcp@314
   283
\tdx{SumFL}     [| A = C;  !!x. x:A ==> B(x) = D(x) 
paulson@5151
   284
          |] ==> SUM x:A. B(x) = SUM x:C. D(x)
lcp@104
   285
paulson@5151
   286
\tdx{SumI}      [| a : A;  b : B(a) |] ==> <a,b> : SUM x:A. B(x)
paulson@5151
   287
\tdx{SumIL}     [| a=c:A;  b=d:B(a) |] ==> <a,b> = <c,d> : SUM x:A. B(x)
lcp@104
   288
paulson@5151
   289
\tdx{SumE}      [| p: SUM x:A. B(x);  
lcp@104
   290
             !!x y. [| x:A; y:B(x) |] ==> c(x,y): C(<x,y>) 
paulson@5151
   291
          |] ==> split(p, \%x y. c(x,y)) : C(p)
lcp@104
   292
paulson@5151
   293
\tdx{SumEL}     [| p=q : SUM x:A. B(x); 
lcp@104
   294
             !!x y. [| x:A; y:B(x) |] ==> c(x,y)=d(x,y): C(<x,y>)
paulson@5151
   295
          |] ==> split(p, \%x y. c(x,y)) = split(q, \%x y. d(x,y)) : C(p)
lcp@104
   296
lcp@314
   297
\tdx{SumC}      [| a: A;  b: B(a);
lcp@104
   298
             !!x y. [| x:A; y:B(x) |] ==> c(x,y): C(<x,y>)
paulson@5151
   299
          |] ==> split(<a,b>, \%x y. c(x,y)) = c(a,b) : C(<a,b>)
lcp@104
   300
paulson@5151
   301
\tdx{fst_def}   fst(a) == split(a, \%x y. x)
paulson@5151
   302
\tdx{snd_def}   snd(a) == split(a, \%x y. y)
lcp@104
   303
\end{ttbox}
lcp@314
   304
\caption{Rules for the sum type $\sum\sb{x\in A}B[x]$} \label{ctt-sum}
lcp@104
   305
\end{figure}
lcp@104
   306
lcp@104
   307
lcp@104
   308
\begin{figure} 
lcp@104
   309
\begin{ttbox}
lcp@314
   310
\tdx{PlusF}       [| A type;  B type |] ==> A+B type
lcp@314
   311
\tdx{PlusFL}      [| A = C;  B = D |] ==> A+B = C+D
lcp@104
   312
lcp@314
   313
\tdx{PlusI_inl}   [| a : A;  B type |] ==> inl(a) : A+B
lcp@314
   314
\tdx{PlusI_inlL}  [| a = c : A;  B type |] ==> inl(a) = inl(c) : A+B
lcp@104
   315
lcp@314
   316
\tdx{PlusI_inr}   [| A type;  b : B |] ==> inr(b) : A+B
lcp@314
   317
\tdx{PlusI_inrL}  [| A type;  b = d : B |] ==> inr(b) = inr(d) : A+B
lcp@104
   318
lcp@314
   319
\tdx{PlusE}     [| p: A+B;
lcp@104
   320
             !!x. x:A ==> c(x): C(inl(x));  
lcp@104
   321
             !!y. y:B ==> d(y): C(inr(y))
paulson@5151
   322
          |] ==> when(p, \%x. c(x), \%y. d(y)) : C(p)
lcp@104
   323
lcp@314
   324
\tdx{PlusEL}    [| p = q : A+B;
lcp@104
   325
             !!x. x: A ==> c(x) = e(x) : C(inl(x));   
lcp@104
   326
             !!y. y: B ==> d(y) = f(y) : C(inr(y))
paulson@5151
   327
          |] ==> when(p, \%x. c(x), \%y. d(y)) = 
paulson@5151
   328
                 when(q, \%x. e(x), \%y. f(y)) : C(p)
lcp@104
   329
lcp@314
   330
\tdx{PlusC_inl} [| a: A;
lcp@104
   331
             !!x. x:A ==> c(x): C(inl(x));  
lcp@104
   332
             !!y. y:B ==> d(y): C(inr(y))
paulson@5151
   333
          |] ==> when(inl(a), \%x. c(x), \%y. d(y)) = c(a) : C(inl(a))
lcp@104
   334
lcp@314
   335
\tdx{PlusC_inr} [| b: B;
lcp@104
   336
             !!x. x:A ==> c(x): C(inl(x));  
lcp@104
   337
             !!y. y:B ==> d(y): C(inr(y))
paulson@5151
   338
          |] ==> when(inr(b), \%x. c(x), \%y. d(y)) = d(b) : C(inr(b))
lcp@104
   339
\end{ttbox}
lcp@104
   340
\caption{Rules for the binary sum type $A+B$} \label{ctt-plus}
lcp@104
   341
\end{figure}
lcp@104
   342
lcp@104
   343
lcp@104
   344
\begin{figure} 
lcp@104
   345
\begin{ttbox}
lcp@314
   346
\tdx{FF}        F type
lcp@314
   347
\tdx{FE}        [| p: F;  C type |] ==> contr(p) : C
lcp@314
   348
\tdx{FEL}       [| p = q : F;  C type |] ==> contr(p) = contr(q) : C
lcp@314
   349
lcp@314
   350
\tdx{TF}        T type
lcp@314
   351
\tdx{TI}        tt : T
lcp@314
   352
\tdx{TE}        [| p : T;  c : C(tt) |] ==> c : C(p)
lcp@314
   353
\tdx{TEL}       [| p = q : T;  c = d : C(tt) |] ==> c = d : C(p)
lcp@314
   354
\tdx{TC}        p : T ==> p = tt : T)
lcp@104
   355
\end{ttbox}
lcp@104
   356
lcp@314
   357
\caption{Rules for types $F$ and $T$} \label{ctt-ft}
lcp@104
   358
\end{figure}
lcp@104
   359
lcp@104
   360
lcp@104
   361
\begin{figure} 
lcp@104
   362
\begin{ttbox}
lcp@314
   363
\tdx{EqF}       [| A type;  a : A;  b : A |] ==> Eq(A,a,b) type
lcp@314
   364
\tdx{EqFL}      [| A=B;  a=c: A;  b=d : A |] ==> Eq(A,a,b) = Eq(B,c,d)
lcp@314
   365
\tdx{EqI}       a = b : A ==> eq : Eq(A,a,b)
lcp@314
   366
\tdx{EqE}       p : Eq(A,a,b) ==> a = b : A
lcp@314
   367
\tdx{EqC}       p : Eq(A,a,b) ==> p = eq : Eq(A,a,b)
lcp@314
   368
\end{ttbox}
lcp@314
   369
\caption{Rules for the equality type $Eq(A,a,b)$} \label{ctt-eq}
lcp@314
   370
\end{figure}
lcp@104
   371
lcp@314
   372
lcp@314
   373
\begin{figure} 
lcp@314
   374
\begin{ttbox}
lcp@314
   375
\tdx{replace_type}    [| B = A;  a : A |] ==> a : B
lcp@314
   376
\tdx{subst_eqtyparg}  [| a=c : A;  !!z. z:A ==> B(z) type |] ==> B(a)=B(c)
lcp@314
   377
lcp@314
   378
\tdx{subst_prodE}     [| p: Prod(A,B);  a: A;  !!z. z: B(a) ==> c(z): C(z)
lcp@104
   379
                |] ==> c(p`a): C(p`a)
lcp@104
   380
lcp@314
   381
\tdx{SumIL2}    [| c=a : A;  d=b : B(a) |] ==> <c,d> = <a,b> : Sum(A,B)
lcp@104
   382
lcp@314
   383
\tdx{SumE_fst}  p : Sum(A,B) ==> fst(p) : A
lcp@104
   384
lcp@314
   385
\tdx{SumE_snd}  [| p: Sum(A,B);  A type;  !!x. x:A ==> B(x) type
lcp@104
   386
          |] ==> snd(p) : B(fst(p))
lcp@104
   387
\end{ttbox}
lcp@104
   388
wenzelm@9695
   389
\caption{Derived rules for CTT} \label{ctt-derived}
lcp@104
   390
\end{figure}
lcp@104
   391
lcp@104
   392
lcp@104
   393
\section{Rules of inference}
lcp@104
   394
The rules obey the following naming conventions.  Type formation rules have
lcp@104
   395
the suffix~{\tt F}\@.  Introduction rules have the suffix~{\tt I}\@.
lcp@104
   396
Elimination rules have the suffix~{\tt E}\@.  Computation rules, which
lcp@104
   397
describe the reduction of eliminators, have the suffix~{\tt C}\@.  The
lcp@104
   398
equality versions of the rules (which permit reductions on subterms) are
lcp@333
   399
called {\bf long} rules; their names have the suffix~{\tt L}\@.
lcp@333
   400
Introduction and computation rules are often further suffixed with
lcp@104
   401
constructor names.
lcp@104
   402
lcp@314
   403
Figure~\ref{ctt-equality} presents the equality rules.  Most of them are
lcp@314
   404
straightforward: reflexivity, symmetry, transitivity and substitution.  The
lcp@314
   405
judgement \cdx{Reduce} does not belong to Type Theory proper; it has
lcp@314
   406
been added to implement rewriting.  The judgement ${\tt Reduce}(a,b)$ holds
lcp@314
   407
when $a=b:A$ holds.  It also holds when $a$ and $b$ are syntactically
lcp@314
   408
identical, even if they are ill-typed, because rule {\tt refl_red} does
lcp@314
   409
not verify that $a$ belongs to $A$.  
lcp@104
   410
lcp@314
   411
The {\tt Reduce} rules do not give rise to new theorems about the standard
lcp@314
   412
judgements.  The only rule with {\tt Reduce} in a premise is
lcp@314
   413
{\tt trans_red}, whose other premise ensures that $a$ and~$b$ (and thus
lcp@314
   414
$c$) are well-typed.
lcp@104
   415
lcp@314
   416
Figure~\ref{ctt-N} presents the rules for~$N$, the type of natural numbers.
lcp@314
   417
They include \tdx{zero_ne_succ}, which asserts $0\not=n+1$.  This is
lcp@314
   418
the fourth Peano axiom and cannot be derived without universes \cite[page
lcp@314
   419
91]{martinlof84}.  
lcp@314
   420
lcp@314
   421
The constant \cdx{rec} constructs proof terms when mathematical
lcp@314
   422
induction, rule~\tdx{NE}, is applied.  It can also express primitive
lcp@314
   423
recursion.  Since \cdx{rec} can be applied to higher-order functions,
lcp@314
   424
it can even express Ackermann's function, which is not primitive recursive
lcp@314
   425
\cite[page~104]{thompson91}.
lcp@314
   426
lcp@314
   427
Figure~\ref{ctt-prod} shows the rules for general product types, which
lcp@314
   428
include function types as a special case.  The rules correspond to the
lcp@314
   429
predicate calculus rules for universal quantifiers and implication.  They
lcp@314
   430
also permit reasoning about functions, with the rules of a typed
lcp@314
   431
$\lambda$-calculus.
lcp@314
   432
lcp@314
   433
Figure~\ref{ctt-sum} shows the rules for general sum types, which
lcp@314
   434
include binary product types as a special case.  The rules correspond to the
lcp@314
   435
predicate calculus rules for existential quantifiers and conjunction.  They
lcp@314
   436
also permit reasoning about ordered pairs, with the projections
lcp@314
   437
\cdx{fst} and~\cdx{snd}.
lcp@314
   438
lcp@314
   439
Figure~\ref{ctt-plus} shows the rules for binary sum types.  They
lcp@314
   440
correspond to the predicate calculus rules for disjunction.  They also
lcp@314
   441
permit reasoning about disjoint sums, with the injections \cdx{inl}
lcp@314
   442
and~\cdx{inr} and case analysis operator~\cdx{when}.
lcp@314
   443
lcp@314
   444
Figure~\ref{ctt-ft} shows the rules for the empty and unit types, $F$
lcp@314
   445
and~$T$.  They correspond to the predicate calculus rules for absurdity and
lcp@314
   446
truth.
lcp@314
   447
lcp@314
   448
Figure~\ref{ctt-eq} shows the rules for equality types.  If $a=b\in A$ is
lcp@314
   449
provable then \cdx{eq} is a canonical element of the type $Eq(A,a,b)$,
lcp@314
   450
and vice versa.  These rules define extensional equality; the most recent
lcp@314
   451
versions of Type Theory use intensional equality~\cite{nordstrom90}.
lcp@314
   452
lcp@314
   453
Figure~\ref{ctt-derived} presents the derived rules.  The rule
lcp@314
   454
\tdx{subst_prodE} is derived from {\tt prodE}, and is easier to use
lcp@314
   455
in backwards proof.  The rules \tdx{SumE_fst} and \tdx{SumE_snd}
lcp@314
   456
express the typing of~\cdx{fst} and~\cdx{snd}; together, they are
lcp@314
   457
roughly equivalent to~{\tt SumE} with the advantage of creating no
lcp@314
   458
parameters.  Section~\ref{ctt-choice} below demonstrates these rules in a
lcp@314
   459
proof of the Axiom of Choice.
lcp@104
   460
lcp@104
   461
All the rules are given in $\eta$-expanded form.  For instance, every
paulson@5151
   462
occurrence of $\lambda u\,v. b(u,v)$ could be abbreviated to~$b$ in the
lcp@314
   463
rules for~$N$.  The expanded form permits Isabelle to preserve bound
lcp@314
   464
variable names during backward proof.  Names of bound variables in the
lcp@314
   465
conclusion (here, $u$ and~$v$) are matched with corresponding bound
lcp@314
   466
variables in the premises.
lcp@104
   467
lcp@104
   468
lcp@104
   469
\section{Rule lists}
lcp@104
   470
The Type Theory tactics provide rewriting, type inference, and logical
lcp@104
   471
reasoning.  Many proof procedures work by repeatedly resolving certain Type
wenzelm@9695
   472
Theory rules against a proof state.  CTT defines lists --- each with
lcp@104
   473
type
lcp@104
   474
\hbox{\tt thm list} --- of related rules. 
lcp@314
   475
\begin{ttdescription}
lcp@104
   476
\item[\ttindexbold{form_rls}] 
lcp@104
   477
contains formation rules for the types $N$, $\Pi$, $\Sigma$, $+$, $Eq$,
lcp@104
   478
$F$, and $T$.
lcp@104
   479
lcp@104
   480
\item[\ttindexbold{formL_rls}] 
lcp@104
   481
contains long formation rules for $\Pi$, $\Sigma$, $+$, and $Eq$.  (For
lcp@314
   482
other types use \tdx{refl_type}.)
lcp@104
   483
lcp@104
   484
\item[\ttindexbold{intr_rls}] 
lcp@104
   485
contains introduction rules for the types $N$, $\Pi$, $\Sigma$, $+$, and
lcp@104
   486
$T$.
lcp@104
   487
lcp@104
   488
\item[\ttindexbold{intrL_rls}] 
lcp@104
   489
contains long introduction rules for $N$, $\Pi$, $\Sigma$, and $+$.  (For
lcp@314
   490
$T$ use \tdx{refl_elem}.)
lcp@104
   491
lcp@104
   492
\item[\ttindexbold{elim_rls}] 
lcp@104
   493
contains elimination rules for the types $N$, $\Pi$, $\Sigma$, $+$, and
lcp@104
   494
$F$.  The rules for $Eq$ and $T$ are omitted because they involve no
lcp@104
   495
eliminator.
lcp@104
   496
lcp@104
   497
\item[\ttindexbold{elimL_rls}] 
lcp@104
   498
contains long elimination rules for $N$, $\Pi$, $\Sigma$, $+$, and $F$.
lcp@104
   499
lcp@104
   500
\item[\ttindexbold{comp_rls}] 
lcp@104
   501
contains computation rules for the types $N$, $\Pi$, $\Sigma$, and $+$.
lcp@104
   502
Those for $Eq$ and $T$ involve no eliminator.
lcp@104
   503
lcp@104
   504
\item[\ttindexbold{basic_defs}] 
lcp@314
   505
contains the definitions of {\tt fst} and {\tt snd}.  
lcp@314
   506
\end{ttdescription}
lcp@104
   507
lcp@104
   508
lcp@104
   509
\section{Tactics for subgoal reordering}
lcp@104
   510
\begin{ttbox}
lcp@104
   511
test_assume_tac : int -> tactic
lcp@104
   512
typechk_tac     : thm list -> tactic
lcp@104
   513
equal_tac       : thm list -> tactic
lcp@104
   514
intr_tac        : thm list -> tactic
lcp@104
   515
\end{ttbox}
wenzelm@9695
   516
Blind application of CTT rules seldom leads to a proof.  The elimination
lcp@104
   517
rules, especially, create subgoals containing new unknowns.  These subgoals
lcp@333
   518
unify with anything, creating a huge search space.  The standard tactic
wenzelm@9695
   519
\ttindex{filt_resolve_tac}
lcp@314
   520
(see \iflabelundefined{filt_resolve_tac}{the {\em Reference Manual\/}}%
lcp@314
   521
        {\S\ref{filt_resolve_tac}})
lcp@314
   522
%
wenzelm@9695
   523
fails for goals that are too flexible; so does the CTT tactic {\tt
lcp@314
   524
  test_assume_tac}.  Used with the tactical \ttindex{REPEAT_FIRST} they
lcp@314
   525
achieve a simple kind of subgoal reordering: the less flexible subgoals are
lcp@314
   526
attempted first.  Do some single step proofs, or study the examples below,
lcp@314
   527
to see why this is necessary.
lcp@314
   528
\begin{ttdescription}
lcp@104
   529
\item[\ttindexbold{test_assume_tac} $i$] 
lcp@314
   530
uses {\tt assume_tac} to solve the subgoal by assumption, but only if
lcp@104
   531
subgoal~$i$ has the form $a\in A$ and the head of $a$ is not an unknown.
lcp@104
   532
Otherwise, it fails.
lcp@104
   533
lcp@104
   534
\item[\ttindexbold{typechk_tac} $thms$] 
lcp@104
   535
uses $thms$ with formation, introduction, and elimination rules to check
lcp@104
   536
the typing of constructions.  It is designed to solve goals of the form
lcp@975
   537
$a\in \Var{A}$, where $a$ is rigid and $\Var{A}$ is flexible; thus it
lcp@975
   538
performs type inference.  The tactic can also solve goals of
lcp@104
   539
the form $A\;\rm type$.
lcp@104
   540
lcp@104
   541
\item[\ttindexbold{equal_tac} $thms$]
lcp@104
   542
uses $thms$ with the long introduction and elimination rules to solve goals
lcp@104
   543
of the form $a=b\in A$, where $a$ is rigid.  It is intended for deriving
lcp@104
   544
the long rules for defined constants such as the arithmetic operators.  The
paulson@6170
   545
tactic can also perform type-checking.
lcp@104
   546
lcp@104
   547
\item[\ttindexbold{intr_tac} $thms$]
lcp@104
   548
uses $thms$ with the introduction rules to break down a type.  It is
lcp@104
   549
designed for goals like $\Var{a}\in A$ where $\Var{a}$ is flexible and $A$
lcp@104
   550
rigid.  These typically arise when trying to prove a proposition~$A$,
lcp@104
   551
expressed as a type.
lcp@314
   552
\end{ttdescription}
lcp@104
   553
lcp@104
   554
lcp@104
   555
lcp@104
   556
\section{Rewriting tactics}
lcp@104
   557
\begin{ttbox}
lcp@104
   558
rew_tac     : thm list -> tactic
lcp@104
   559
hyp_rew_tac : thm list -> tactic
lcp@104
   560
\end{ttbox}
lcp@104
   561
Object-level simplification is accomplished through proof, using the {\tt
lcp@314
   562
  CTT} equality rules and the built-in rewriting functor
lcp@314
   563
{\tt TSimpFun}.%
lcp@314
   564
\footnote{This should not be confused with Isabelle's main simplifier; {\tt
wenzelm@9695
   565
    TSimpFun} is only useful for CTT and similar logics with type inference
wenzelm@9695
   566
  rules.  At present it is undocumented.}
lcp@314
   567
%
lcp@314
   568
The rewrites include the computation rules and other equations.  The long
lcp@314
   569
versions of the other rules permit rewriting of subterms and subtypes.
lcp@314
   570
Also used are transitivity and the extra judgement form \cdx{Reduce}.
lcp@104
   571
Meta-level simplification handles only definitional equality.
lcp@314
   572
\begin{ttdescription}
lcp@104
   573
\item[\ttindexbold{rew_tac} $thms$]
lcp@104
   574
applies $thms$ and the computation rules as left-to-right rewrites.  It
lcp@104
   575
solves the goal $a=b\in A$ by rewriting $a$ to $b$.  If $b$ is an unknown
lcp@104
   576
then it is assigned the rewritten form of~$a$.  All subgoals are rewritten.
lcp@104
   577
lcp@104
   578
\item[\ttindexbold{hyp_rew_tac} $thms$]
lcp@104
   579
is like {\tt rew_tac}, but includes as rewrites any equations present in
lcp@104
   580
the assumptions.
lcp@314
   581
\end{ttdescription}
lcp@104
   582
lcp@104
   583
lcp@104
   584
\section{Tactics for logical reasoning}
wenzelm@9695
   585
Interpreting propositions as types lets CTT express statements of
wenzelm@9695
   586
intuitionistic logic.  However, Constructive Type Theory is not just another
wenzelm@9695
   587
syntax for first-order logic.  There are fundamental differences.
lcp@104
   588
wenzelm@9695
   589
\index{assumptions!in CTT}
lcp@314
   590
Can assumptions be deleted after use?  Not every occurrence of a type
lcp@314
   591
represents a proposition, and Type Theory assumptions declare variables.
lcp@104
   592
In first-order logic, $\disj$-elimination with the assumption $P\disj Q$
lcp@104
   593
creates one subgoal assuming $P$ and another assuming $Q$, and $P\disj Q$
lcp@314
   594
can be deleted safely.  In Type Theory, $+$-elimination with the assumption
lcp@314
   595
$z\in A+B$ creates one subgoal assuming $x\in A$ and another assuming $y\in
lcp@314
   596
B$ (for arbitrary $x$ and $y$).  Deleting $z\in A+B$ when other assumptions
lcp@314
   597
refer to $z$ may render the subgoal unprovable: arguably,
lcp@314
   598
meaningless.
lcp@314
   599
wenzelm@9695
   600
Isabelle provides several tactics for predicate calculus reasoning in CTT:
lcp@104
   601
\begin{ttbox}
lcp@104
   602
mp_tac       : int -> tactic
lcp@104
   603
add_mp_tac   : int -> tactic
lcp@104
   604
safestep_tac : thm list -> int -> tactic
lcp@104
   605
safe_tac     : thm list -> int -> tactic
lcp@104
   606
step_tac     : thm list -> int -> tactic
lcp@104
   607
pc_tac       : thm list -> int -> tactic
lcp@104
   608
\end{ttbox}
lcp@104
   609
These are loosely based on the intuitionistic proof procedures
lcp@314
   610
of~\thydx{FOL}.  For the reasons discussed above, a rule that is safe for
paulson@6170
   611
propositional reasoning may be unsafe for type-checking; thus, some of the
lcp@314
   612
`safe' tactics are misnamed.
lcp@314
   613
\begin{ttdescription}
lcp@104
   614
\item[\ttindexbold{mp_tac} $i$] 
lcp@104
   615
searches in subgoal~$i$ for assumptions of the form $f\in\Pi(A,B)$ and
lcp@104
   616
$a\in A$, where~$A$ may be found by unification.  It replaces
lcp@104
   617
$f\in\Pi(A,B)$ by $z\in B(a)$, where~$z$ is a new parameter.  The tactic
lcp@104
   618
can produce multiple outcomes for each suitable pair of assumptions.  In
lcp@104
   619
short, {\tt mp_tac} performs Modus Ponens among the assumptions.
lcp@104
   620
lcp@104
   621
\item[\ttindexbold{add_mp_tac} $i$]
lcp@314
   622
is like {\tt mp_tac}~$i$ but retains the assumption $f\in\Pi(A,B)$.  It
lcp@314
   623
avoids information loss but obviously loops if repeated.
lcp@104
   624
lcp@104
   625
\item[\ttindexbold{safestep_tac} $thms$ $i$]
lcp@104
   626
attacks subgoal~$i$ using formation rules and certain other `safe' rules
lcp@333
   627
(\tdx{FE}, \tdx{ProdI}, \tdx{SumE}, \tdx{PlusE}), calling
lcp@104
   628
{\tt mp_tac} when appropriate.  It also uses~$thms$,
lcp@104
   629
which are typically premises of the rule being derived.
lcp@104
   630
lcp@314
   631
\item[\ttindexbold{safe_tac} $thms$ $i$] attempts to solve subgoal~$i$ by
lcp@314
   632
  means of backtracking, using {\tt safestep_tac}.
lcp@104
   633
lcp@104
   634
\item[\ttindexbold{step_tac} $thms$ $i$]
lcp@104
   635
tries to reduce subgoal~$i$ using {\tt safestep_tac}, then tries unsafe
lcp@104
   636
rules.  It may produce multiple outcomes.
lcp@104
   637
lcp@104
   638
\item[\ttindexbold{pc_tac} $thms$ $i$]
lcp@104
   639
tries to solve subgoal~$i$ by backtracking, using {\tt step_tac}.
lcp@314
   640
\end{ttdescription}
lcp@104
   641
lcp@104
   642
lcp@104
   643
lcp@104
   644
\begin{figure} 
lcp@314
   645
\index{#+@{\tt\#+} symbol}
lcp@314
   646
\index{*"- symbol}
paulson@12679
   647
\index{"|"-@{{\tt"|"-"|} symbol}}
lcp@314
   648
\index{#*@{\tt\#*} symbol}
lcp@314
   649
\index{*div symbol}
lcp@314
   650
\index{*mod symbol}
lcp@314
   651
\begin{constants}
lcp@314
   652
  \it symbol  & \it meta-type & \it priority & \it description \\ 
lcp@314
   653
  \tt \#*       & $[i,i]\To i$  &  Left 70      & multiplication \\
lcp@314
   654
  \tt div       & $[i,i]\To i$  &  Left 70      & division\\
lcp@314
   655
  \tt mod       & $[i,i]\To i$  &  Left 70      & modulus\\
lcp@314
   656
  \tt \#+       & $[i,i]\To i$  &  Left 65      & addition\\
lcp@314
   657
  \tt -         & $[i,i]\To i$  &  Left 65      & subtraction\\
lcp@314
   658
  \verb'|-|'    & $[i,i]\To i$  &  Left 65      & absolute difference
lcp@314
   659
\end{constants}
lcp@104
   660
lcp@104
   661
\begin{ttbox}
paulson@5151
   662
\tdx{add_def}           a#+b  == rec(a, b, \%u v. succ(v))  
paulson@5151
   663
\tdx{diff_def}          a-b   == rec(b, a, \%u v. rec(v, 0, \%x y. x))  
lcp@314
   664
\tdx{absdiff_def}       a|-|b == (a-b) #+ (b-a)  
lcp@314
   665
\tdx{mult_def}          a#*b  == rec(a, 0, \%u v. b #+ v)  
lcp@104
   666
lcp@314
   667
\tdx{mod_def}           a mod b ==
paulson@5151
   668
                  rec(a, 0, \%u v. rec(succ(v) |-| b, 0, \%x y. succ(v)))
lcp@314
   669
lcp@314
   670
\tdx{div_def}           a div b ==
paulson@5151
   671
                  rec(a, 0, \%u v. rec(succ(u) mod b, succ(v), \%x y. v))
lcp@314
   672
lcp@314
   673
\tdx{add_typing}        [| a:N;  b:N |] ==> a #+ b : N
lcp@314
   674
\tdx{addC0}             b:N ==> 0 #+ b = b : N
lcp@314
   675
\tdx{addC_succ}         [| a:N;  b:N |] ==> succ(a) #+ b = succ(a #+ b) : N
lcp@314
   676
lcp@314
   677
\tdx{add_assoc}         [| a:N;  b:N;  c:N |] ==> 
lcp@104
   678
                  (a #+ b) #+ c = a #+ (b #+ c) : N
lcp@104
   679
lcp@314
   680
\tdx{add_commute}       [| a:N;  b:N |] ==> a #+ b = b #+ a : N
lcp@104
   681
lcp@314
   682
\tdx{mult_typing}       [| a:N;  b:N |] ==> a #* b : N
lcp@314
   683
\tdx{multC0}            b:N ==> 0 #* b = 0 : N
lcp@314
   684
\tdx{multC_succ}        [| a:N;  b:N |] ==> succ(a) #* b = b #+ (a#*b) : N
lcp@314
   685
\tdx{mult_commute}      [| a:N;  b:N |] ==> a #* b = b #* a : N
lcp@104
   686
lcp@314
   687
\tdx{add_mult_dist}     [| a:N;  b:N;  c:N |] ==> 
lcp@104
   688
                  (a #+ b) #* c = (a #* c) #+ (b #* c) : N
lcp@104
   689
lcp@314
   690
\tdx{mult_assoc}        [| a:N;  b:N;  c:N |] ==> 
lcp@104
   691
                  (a #* b) #* c = a #* (b #* c) : N
lcp@104
   692
lcp@314
   693
\tdx{diff_typing}       [| a:N;  b:N |] ==> a - b : N
lcp@314
   694
\tdx{diffC0}            a:N ==> a - 0 = a : N
lcp@314
   695
\tdx{diff_0_eq_0}       b:N ==> 0 - b = 0 : N
lcp@314
   696
\tdx{diff_succ_succ}    [| a:N;  b:N |] ==> succ(a) - succ(b) = a - b : N
lcp@314
   697
\tdx{diff_self_eq_0}    a:N ==> a - a = 0 : N
lcp@314
   698
\tdx{add_inverse_diff}  [| a:N;  b:N;  b-a=0 : N |] ==> b #+ (a-b) = a : N
wenzelm@3136
   699
\end{ttbox}
berghofe@3096
   700
\caption{The theory of arithmetic} \label{ctt_arith}
lcp@104
   701
\end{figure}
lcp@104
   702
lcp@104
   703
lcp@104
   704
\section{A theory of arithmetic}
lcp@314
   705
\thydx{Arith} is a theory of elementary arithmetic.  It proves the
lcp@104
   706
properties of addition, multiplication, subtraction, division, and
lcp@104
   707
remainder, culminating in the theorem
lcp@104
   708
\[ a \bmod b + (a/b)\times b = a. \]
berghofe@3096
   709
Figure~\ref{ctt_arith} presents the definitions and some of the key
lcp@314
   710
theorems, including commutative, distributive, and associative laws.
lcp@104
   711
lcp@111
   712
The operators~\verb'#+', \verb'-', \verb'|-|', \verb'#*', \verb'mod'
lcp@111
   713
and~\verb'div' stand for sum, difference, absolute difference, product,
lcp@104
   714
remainder and quotient, respectively.  Since Type Theory has only primitive
lcp@104
   715
recursion, some of their definitions may be obscure.  
lcp@104
   716
lcp@104
   717
The difference~$a-b$ is computed by taking $b$ predecessors of~$a$, where
paulson@5151
   718
the predecessor function is $\lambda v. {\tt rec}(v, 0, \lambda x\,y. x)$.
lcp@104
   719
lcp@111
   720
The remainder $a\bmod b$ counts up to~$a$ in a cyclic fashion, using 0
lcp@111
   721
as the successor of~$b-1$.  Absolute difference is used to test the
lcp@111
   722
equality $succ(v)=b$.
lcp@104
   723
lcp@111
   724
The quotient $a/b$ is computed by adding one for every number $x$
lcp@111
   725
such that $0\leq x \leq a$ and $x\bmod b = 0$.
lcp@104
   726
lcp@104
   727
lcp@104
   728
lcp@104
   729
\section{The examples directory}
wenzelm@9695
   730
This directory contains examples and experimental proofs in CTT.
lcp@314
   731
\begin{ttdescription}
lcp@314
   732
\item[CTT/ex/typechk.ML]
paulson@6170
   733
contains simple examples of type-checking and type deduction.
lcp@104
   734
lcp@314
   735
\item[CTT/ex/elim.ML]
lcp@104
   736
contains some examples from Martin-L\"of~\cite{martinlof84}, proved using 
lcp@104
   737
{\tt pc_tac}.
lcp@104
   738
lcp@314
   739
\item[CTT/ex/equal.ML]
lcp@104
   740
contains simple examples of rewriting.
lcp@104
   741
lcp@314
   742
\item[CTT/ex/synth.ML]
lcp@104
   743
demonstrates the use of unknowns with some trivial examples of program
lcp@104
   744
synthesis. 
lcp@314
   745
\end{ttdescription}
lcp@104
   746
lcp@104
   747
lcp@104
   748
\section{Example: type inference}
lcp@104
   749
Type inference involves proving a goal of the form $a\in\Var{A}$, where $a$
lcp@104
   750
is a term and $\Var{A}$ is an unknown standing for its type.  The type,
lcp@104
   751
initially
lcp@104
   752
unknown, takes shape in the course of the proof.  Our example is the
lcp@104
   753
predecessor function on the natural numbers.
lcp@104
   754
\begin{ttbox}
paulson@5151
   755
Goal "lam n. rec(n, 0, \%x y. x) : ?A";
lcp@104
   756
{\out Level 0}
lcp@284
   757
{\out lam n. rec(n,0,\%x y. x) : ?A}
lcp@284
   758
{\out  1. lam n. rec(n,0,\%x y. x) : ?A}
lcp@104
   759
\end{ttbox}
lcp@104
   760
Since the term is a Constructive Type Theory $\lambda$-abstraction (not to
lcp@104
   761
be confused with a meta-level abstraction), we apply the rule
lcp@314
   762
\tdx{ProdI}, for $\Pi$-introduction.  This instantiates~$\Var{A}$ to a
lcp@104
   763
product type of unknown domain and range.
lcp@104
   764
\begin{ttbox}
lcp@104
   765
by (resolve_tac [ProdI] 1);
lcp@104
   766
{\out Level 1}
lcp@284
   767
{\out lam n. rec(n,0,\%x y. x) : PROD x:?A1. ?B1(x)}
lcp@104
   768
{\out  1. ?A1 type}
lcp@284
   769
{\out  2. !!n. n : ?A1 ==> rec(n,0,\%x y. x) : ?B1(n)}
lcp@104
   770
\end{ttbox}
lcp@284
   771
Subgoal~1 is too flexible.  It can be solved by instantiating~$\Var{A@1}$
lcp@284
   772
to any type, but most instantiations will invalidate subgoal~2.  We
lcp@284
   773
therefore tackle the latter subgoal.  It asks the type of a term beginning
lcp@314
   774
with {\tt rec}, which can be found by $N$-elimination.%
lcp@314
   775
\index{*NE theorem}
lcp@104
   776
\begin{ttbox}
lcp@104
   777
by (eresolve_tac [NE] 2);
lcp@104
   778
{\out Level 2}
lcp@284
   779
{\out lam n. rec(n,0,\%x y. x) : PROD x:N. ?C2(x,x)}
lcp@104
   780
{\out  1. N type}
lcp@104
   781
{\out  2. !!n. 0 : ?C2(n,0)}
lcp@104
   782
{\out  3. !!n x y. [| x : N; y : ?C2(n,x) |] ==> x : ?C2(n,succ(x))}
lcp@104
   783
\end{ttbox}
lcp@284
   784
Subgoal~1 is no longer flexible: we now know~$\Var{A@1}$ is the type of
lcp@284
   785
natural numbers.  However, let us continue proving nontrivial subgoals.
lcp@314
   786
Subgoal~2 asks, what is the type of~0?\index{*NIO theorem}
lcp@104
   787
\begin{ttbox}
lcp@104
   788
by (resolve_tac [NI0] 2);
lcp@104
   789
{\out Level 3}
lcp@284
   790
{\out lam n. rec(n,0,\%x y. x) : N --> N}
lcp@104
   791
{\out  1. N type}
lcp@104
   792
{\out  2. !!n x y. [| x : N; y : N |] ==> x : N}
lcp@104
   793
\end{ttbox}
lcp@284
   794
The type~$\Var{A}$ is now fully determined.  It is the product type
lcp@314
   795
$\prod@{x\in N}N$, which is shown as the function type $N\to N$ because
lcp@284
   796
there is no dependence on~$x$.  But we must prove all the subgoals to show
lcp@284
   797
that the original term is validly typed.  Subgoal~2 is provable by
lcp@314
   798
assumption and the remaining subgoal falls by $N$-formation.%
lcp@314
   799
\index{*NF theorem}
lcp@104
   800
\begin{ttbox}
lcp@104
   801
by (assume_tac 2);
lcp@104
   802
{\out Level 4}
lcp@284
   803
{\out lam n. rec(n,0,\%x y. x) : N --> N}
lcp@104
   804
{\out  1. N type}
lcp@284
   805
\ttbreak
lcp@104
   806
by (resolve_tac [NF] 1);
lcp@104
   807
{\out Level 5}
lcp@284
   808
{\out lam n. rec(n,0,\%x y. x) : N --> N}
lcp@104
   809
{\out No subgoals!}
lcp@104
   810
\end{ttbox}
lcp@104
   811
Calling \ttindex{typechk_tac} can prove this theorem in one step.
lcp@104
   812
lcp@284
   813
Even if the original term is ill-typed, one can infer a type for it, but
lcp@284
   814
unprovable subgoals will be left.  As an exercise, try to prove the
lcp@284
   815
following invalid goal:
lcp@284
   816
\begin{ttbox}
paulson@5151
   817
Goal "lam n. rec(n, 0, \%x y. tt) : ?A";
lcp@284
   818
\end{ttbox}
lcp@284
   819
lcp@284
   820
lcp@104
   821
lcp@104
   822
\section{An example of logical reasoning}
lcp@104
   823
Logical reasoning in Type Theory involves proving a goal of the form
lcp@314
   824
$\Var{a}\in A$, where type $A$ expresses a proposition and $\Var{a}$ stands
lcp@314
   825
for its proof term, a value of type $A$.  The proof term is initially
lcp@314
   826
unknown and takes shape during the proof.  
lcp@314
   827
lcp@314
   828
Our example expresses a theorem about quantifiers in a sorted logic:
lcp@104
   829
\[ \infer{(\ex{x\in A}P(x)) \disj (\ex{x\in A}Q(x))}
lcp@104
   830
         {\ex{x\in A}P(x)\disj Q(x)} 
lcp@104
   831
\]
lcp@314
   832
By the propositions-as-types principle, this is encoded
lcp@314
   833
using~$\Sigma$ and~$+$ types.  A special case of it expresses a
lcp@314
   834
distributive law of Type Theory: 
lcp@104
   835
\[ \infer{(A\times B) + (A\times C)}{A\times(B+C)} \]
lcp@104
   836
Generalizing this from $\times$ to $\Sigma$, and making the typing
lcp@314
   837
conditions explicit, yields the rule we must derive:
lcp@104
   838
\[ \infer{\Var{a} \in (\sum@{x\in A} B(x)) + (\sum@{x\in A} C(x))}
lcp@104
   839
         {\hbox{$A$ type} &
lcp@104
   840
          \infer*{\hbox{$B(x)$ type}}{[x\in A]}  &
lcp@104
   841
          \infer*{\hbox{$C(x)$ type}}{[x\in A]}  &
lcp@104
   842
          p\in \sum@{x\in A} B(x)+C(x)} 
lcp@104
   843
\]
lcp@314
   844
To begin, we bind the rule's premises --- returned by the~{\tt goal}
lcp@314
   845
command --- to the {\ML} variable~{\tt prems}.
lcp@104
   846
\begin{ttbox}
paulson@5151
   847
val prems = Goal
lcp@104
   848
    "[| A type;                       \ttback
lcp@104
   849
\ttback       !!x. x:A ==> B(x) type;       \ttback
lcp@104
   850
\ttback       !!x. x:A ==> C(x) type;       \ttback
lcp@104
   851
\ttback       p: SUM x:A. B(x) + C(x)       \ttback
lcp@104
   852
\ttback    |] ==>  ?a : (SUM x:A. B(x)) + (SUM x:A. C(x))";
lcp@104
   853
{\out Level 0}
lcp@104
   854
{\out ?a : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@104
   855
{\out  1. ?a : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@114
   856
\ttbreak
lcp@111
   857
{\out val prems = ["A type  [A type]",}
lcp@111
   858
{\out              "?x : A ==> B(?x) type  [!!x. x : A ==> B(x) type]",}
lcp@111
   859
{\out              "?x : A ==> C(?x) type  [!!x. x : A ==> C(x) type]",}
lcp@111
   860
{\out              "p : SUM x:A. B(x) + C(x)  [p : SUM x:A. B(x) + C(x)]"]}
lcp@111
   861
{\out             : thm list}
lcp@104
   862
\end{ttbox}
lcp@314
   863
The last premise involves the sum type~$\Sigma$.  Since it is a premise
lcp@314
   864
rather than the assumption of a goal, it cannot be found by {\tt
lcp@314
   865
  eresolve_tac}.  We could insert it (and the other atomic premise) by
lcp@314
   866
calling
lcp@314
   867
\begin{ttbox}
lcp@314
   868
cut_facts_tac prems 1;
lcp@314
   869
\end{ttbox}
lcp@314
   870
A forward proof step is more straightforward here.  Let us resolve the
lcp@314
   871
$\Sigma$-elimination rule with the premises using~\ttindex{RL}.  This
lcp@314
   872
inference yields one result, which we supply to {\tt
lcp@314
   873
  resolve_tac}.\index{*SumE theorem}
lcp@104
   874
\begin{ttbox}
lcp@104
   875
by (resolve_tac (prems RL [SumE]) 1);
lcp@104
   876
{\out Level 1}
lcp@104
   877
{\out split(p,?c1) : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@104
   878
{\out  1. !!x y.}
lcp@104
   879
{\out        [| x : A; y : B(x) + C(x) |] ==>}
lcp@104
   880
{\out        ?c1(x,y) : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@104
   881
\end{ttbox}
lcp@284
   882
The subgoal has two new parameters, $x$ and~$y$.  In the main goal,
lcp@314
   883
$\Var{a}$ has been instantiated with a \cdx{split} term.  The
lcp@284
   884
assumption $y\in B(x) + C(x)$ is eliminated next, causing a case split and
lcp@314
   885
creating the parameter~$xa$.  This inference also inserts~\cdx{when}
lcp@314
   886
into the main goal.\index{*PlusE theorem}
lcp@104
   887
\begin{ttbox}
lcp@104
   888
by (eresolve_tac [PlusE] 1);
lcp@104
   889
{\out Level 2}
lcp@284
   890
{\out split(p,\%x y. when(y,?c2(x,y),?d2(x,y)))}
lcp@104
   891
{\out : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@104
   892
{\out  1. !!x y xa.}
lcp@104
   893
{\out        [| x : A; xa : B(x) |] ==>}
lcp@104
   894
{\out        ?c2(x,y,xa) : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@114
   895
\ttbreak
lcp@104
   896
{\out  2. !!x y ya.}
lcp@104
   897
{\out        [| x : A; ya : C(x) |] ==>}
lcp@104
   898
{\out        ?d2(x,y,ya) : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@104
   899
\end{ttbox}
lcp@104
   900
To complete the proof object for the main goal, we need to instantiate the
lcp@104
   901
terms $\Var{c@2}(x,y,xa)$ and $\Var{d@2}(x,y,xa)$.  We attack subgoal~1 by
lcp@314
   902
a~$+$-introduction rule; since the goal assumes $xa\in B(x)$, we take the left
lcp@314
   903
injection~(\cdx{inl}).
lcp@314
   904
\index{*PlusI_inl theorem}
lcp@104
   905
\begin{ttbox}
lcp@104
   906
by (resolve_tac [PlusI_inl] 1);
lcp@104
   907
{\out Level 3}
lcp@284
   908
{\out split(p,\%x y. when(y,\%xa. inl(?a3(x,y,xa)),?d2(x,y)))}
lcp@104
   909
{\out : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@104
   910
{\out  1. !!x y xa. [| x : A; xa : B(x) |] ==> ?a3(x,y,xa) : SUM x:A. B(x)}
lcp@104
   911
{\out  2. !!x y xa. [| x : A; xa : B(x) |] ==> SUM x:A. C(x) type}
lcp@114
   912
\ttbreak
lcp@104
   913
{\out  3. !!x y ya.}
lcp@104
   914
{\out        [| x : A; ya : C(x) |] ==>}
lcp@104
   915
{\out        ?d2(x,y,ya) : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@104
   916
\end{ttbox}
lcp@314
   917
A new subgoal~2 has appeared, to verify that $\sum@{x\in A}C(x)$ is a type.
lcp@314
   918
Continuing to work on subgoal~1, we apply the $\Sigma$-introduction rule.
lcp@314
   919
This instantiates the term $\Var{a@3}(x,y,xa)$; the main goal now contains
lcp@314
   920
an ordered pair, whose components are two new unknowns.%
lcp@314
   921
\index{*SumI theorem}
lcp@104
   922
\begin{ttbox}
lcp@104
   923
by (resolve_tac [SumI] 1);
lcp@104
   924
{\out Level 4}
lcp@284
   925
{\out split(p,\%x y. when(y,\%xa. inl(<?a4(x,y,xa),?b4(x,y,xa)>),?d2(x,y)))}
lcp@104
   926
{\out : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@104
   927
{\out  1. !!x y xa. [| x : A; xa : B(x) |] ==> ?a4(x,y,xa) : A}
lcp@104
   928
{\out  2. !!x y xa. [| x : A; xa : B(x) |] ==> ?b4(x,y,xa) : B(?a4(x,y,xa))}
lcp@104
   929
{\out  3. !!x y xa. [| x : A; xa : B(x) |] ==> SUM x:A. C(x) type}
lcp@104
   930
{\out  4. !!x y ya.}
lcp@104
   931
{\out        [| x : A; ya : C(x) |] ==>}
lcp@104
   932
{\out        ?d2(x,y,ya) : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@104
   933
\end{ttbox}
lcp@104
   934
The two new subgoals both hold by assumption.  Observe how the unknowns
lcp@104
   935
$\Var{a@4}$ and $\Var{b@4}$ are instantiated throughout the proof state.
lcp@104
   936
\begin{ttbox}
lcp@104
   937
by (assume_tac 1);
lcp@104
   938
{\out Level 5}
lcp@284
   939
{\out split(p,\%x y. when(y,\%xa. inl(<x,?b4(x,y,xa)>),?d2(x,y)))}
lcp@104
   940
{\out : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@284
   941
\ttbreak
lcp@104
   942
{\out  1. !!x y xa. [| x : A; xa : B(x) |] ==> ?b4(x,y,xa) : B(x)}
lcp@104
   943
{\out  2. !!x y xa. [| x : A; xa : B(x) |] ==> SUM x:A. C(x) type}
lcp@104
   944
{\out  3. !!x y ya.}
lcp@104
   945
{\out        [| x : A; ya : C(x) |] ==>}
lcp@104
   946
{\out        ?d2(x,y,ya) : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@284
   947
\ttbreak
lcp@104
   948
by (assume_tac 1);
lcp@104
   949
{\out Level 6}
lcp@284
   950
{\out split(p,\%x y. when(y,\%xa. inl(<x,xa>),?d2(x,y)))}
lcp@104
   951
{\out : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@104
   952
{\out  1. !!x y xa. [| x : A; xa : B(x) |] ==> SUM x:A. C(x) type}
lcp@104
   953
{\out  2. !!x y ya.}
lcp@104
   954
{\out        [| x : A; ya : C(x) |] ==>}
lcp@104
   955
{\out        ?d2(x,y,ya) : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@104
   956
\end{ttbox}
lcp@314
   957
Subgoal~1 is an example of a well-formedness subgoal~\cite{constable86}.
lcp@314
   958
Such subgoals are usually trivial; this one yields to
lcp@314
   959
\ttindex{typechk_tac}, given the current list of premises.
lcp@104
   960
\begin{ttbox}
lcp@104
   961
by (typechk_tac prems);
lcp@104
   962
{\out Level 7}
lcp@284
   963
{\out split(p,\%x y. when(y,\%xa. inl(<x,xa>),?d2(x,y)))}
lcp@104
   964
{\out : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@104
   965
{\out  1. !!x y ya.}
lcp@104
   966
{\out        [| x : A; ya : C(x) |] ==>}
lcp@104
   967
{\out        ?d2(x,y,ya) : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@104
   968
\end{ttbox}
lcp@314
   969
This subgoal is the other case from the $+$-elimination above, and can be
lcp@314
   970
proved similarly.  Quicker is to apply \ttindex{pc_tac}.  The main goal
lcp@314
   971
finally gets a fully instantiated proof object.
lcp@104
   972
\begin{ttbox}
lcp@104
   973
by (pc_tac prems 1);
lcp@104
   974
{\out Level 8}
lcp@284
   975
{\out split(p,\%x y. when(y,\%xa. inl(<x,xa>),\%y. inr(<x,y>)))}
lcp@104
   976
{\out : (SUM x:A. B(x)) + (SUM x:A. C(x))}
lcp@104
   977
{\out No subgoals!}
lcp@104
   978
\end{ttbox}
lcp@104
   979
Calling \ttindex{pc_tac} after the first $\Sigma$-elimination above also
lcp@104
   980
proves this theorem.
lcp@104
   981
lcp@104
   982
lcp@104
   983
\section{Example: deriving a currying functional}
lcp@104
   984
In simply-typed languages such as {\ML}, a currying functional has the type 
lcp@104
   985
\[ (A\times B \to C) \to (A\to (B\to C)). \]
lcp@314
   986
Let us generalize this to the dependent types~$\Sigma$ and~$\Pi$.  
lcp@284
   987
The functional takes a function~$f$ that maps $z:\Sigma(A,B)$
lcp@284
   988
to~$C(z)$; the resulting function maps $x\in A$ and $y\in B(x)$ to
lcp@284
   989
$C(\langle x,y\rangle)$.
lcp@284
   990
lcp@284
   991
Formally, there are three typing premises.  $A$ is a type; $B$ is an
lcp@284
   992
$A$-indexed family of types; $C$ is a family of types indexed by
lcp@284
   993
$\Sigma(A,B)$.  The goal is expressed using \hbox{\tt PROD f} to ensure
lcp@284
   994
that the parameter corresponding to the functional's argument is really
lcp@284
   995
called~$f$; Isabelle echoes the type using \verb|-->| because there is no
lcp@284
   996
explicit dependence upon~$f$.
lcp@104
   997
\begin{ttbox}
paulson@5151
   998
val prems = Goal
lcp@284
   999
    "[| A type; !!x. x:A ==> B(x) type;                           \ttback
lcp@284
  1000
\ttback               !!z. z: (SUM x:A. B(x)) ==> C(z) type             \ttback
lcp@284
  1001
\ttback    |] ==> ?a : PROD f: (PROD z : (SUM x:A . B(x)) . C(z)).      \ttback
lcp@284
  1002
\ttback                     (PROD x:A . PROD y:B(x) . C(<x,y>))";
lcp@284
  1003
\ttbreak
lcp@104
  1004
{\out Level 0}
lcp@284
  1005
{\out ?a : (PROD z:SUM x:A. B(x). C(z)) -->}
lcp@284
  1006
{\out      (PROD x:A. PROD y:B(x). C(<x,y>))}
lcp@104
  1007
{\out  1. ?a : (PROD z:SUM x:A. B(x). C(z)) -->}
lcp@104
  1008
{\out          (PROD x:A. PROD y:B(x). C(<x,y>))}
lcp@114
  1009
\ttbreak
lcp@111
  1010
{\out val prems = ["A type  [A type]",}
lcp@111
  1011
{\out              "?x : A ==> B(?x) type  [!!x. x : A ==> B(x) type]",}
lcp@111
  1012
{\out              "?z : SUM x:A. B(x) ==> C(?z) type}
lcp@111
  1013
{\out               [!!z. z : SUM x:A. B(x) ==> C(z) type]"] : thm list}
lcp@104
  1014
\end{ttbox}
lcp@284
  1015
This is a chance to demonstrate \ttindex{intr_tac}.  Here, the tactic
lcp@314
  1016
repeatedly applies $\Pi$-introduction and proves the rather
lcp@284
  1017
tiresome typing conditions.  
lcp@284
  1018
lcp@284
  1019
Note that $\Var{a}$ becomes instantiated to three nested
lcp@284
  1020
$\lambda$-abstractions.  It would be easier to read if the bound variable
lcp@284
  1021
names agreed with the parameters in the subgoal.  Isabelle attempts to give
lcp@284
  1022
parameters the same names as corresponding bound variables in the goal, but
lcp@284
  1023
this does not always work.  In any event, the goal is logically correct.
lcp@104
  1024
\begin{ttbox}
lcp@104
  1025
by (intr_tac prems);
lcp@104
  1026
{\out Level 1}
lcp@104
  1027
{\out lam x xa xb. ?b7(x,xa,xb)}
lcp@104
  1028
{\out : (PROD z:SUM x:A. B(x). C(z)) --> (PROD x:A. PROD y:B(x). C(<x,y>))}
lcp@284
  1029
{\out  1. !!f x y.}
lcp@284
  1030
{\out        [| f : PROD z:SUM x:A. B(x). C(z); x : A; y : B(x) |] ==>}
lcp@284
  1031
{\out        ?b7(f,x,y) : C(<x,y>)}
lcp@104
  1032
\end{ttbox}
lcp@284
  1033
Using $\Pi$-elimination, we solve subgoal~1 by applying the function~$f$.
lcp@314
  1034
\index{*ProdE theorem}
lcp@104
  1035
\begin{ttbox}
lcp@104
  1036
by (eresolve_tac [ProdE] 1);
lcp@104
  1037
{\out Level 2}
lcp@104
  1038
{\out lam x xa xb. x ` <xa,xb>}
lcp@104
  1039
{\out : (PROD z:SUM x:A. B(x). C(z)) --> (PROD x:A. PROD y:B(x). C(<x,y>))}
lcp@284
  1040
{\out  1. !!f x y. [| x : A; y : B(x) |] ==> <x,y> : SUM x:A. B(x)}
lcp@104
  1041
\end{ttbox}
lcp@314
  1042
Finally, we verify that the argument's type is suitable for the function
lcp@314
  1043
application.  This is straightforward using introduction rules.
lcp@104
  1044
\index{*intr_tac}
lcp@104
  1045
\begin{ttbox}
lcp@104
  1046
by (intr_tac prems);
lcp@104
  1047
{\out Level 3}
lcp@104
  1048
{\out lam x xa xb. x ` <xa,xb>}
lcp@104
  1049
{\out : (PROD z:SUM x:A. B(x). C(z)) --> (PROD x:A. PROD y:B(x). C(<x,y>))}
lcp@104
  1050
{\out No subgoals!}
lcp@104
  1051
\end{ttbox}
lcp@104
  1052
Calling~\ttindex{pc_tac} would have proved this theorem in one step; it can
lcp@104
  1053
also prove an example by Martin-L\"of, related to $\disj$-elimination
lcp@104
  1054
\cite[page~58]{martinlof84}.
lcp@104
  1055
lcp@104
  1056
lcp@104
  1057
\section{Example: proving the Axiom of Choice} \label{ctt-choice}
lcp@104
  1058
Suppose we have a function $h\in \prod@{x\in A}\sum@{y\in B(x)} C(x,y)$,
lcp@104
  1059
which takes $x\in A$ to some $y\in B(x)$ paired with some $z\in C(x,y)$.
lcp@104
  1060
Interpreting propositions as types, this asserts that for all $x\in A$
lcp@104
  1061
there exists $y\in B(x)$ such that $C(x,y)$.  The Axiom of Choice asserts
lcp@104
  1062
that we can construct a function $f\in \prod@{x\in A}B(x)$ such that
lcp@104
  1063
$C(x,f{\tt`}x)$ for all $x\in A$, where the latter property is witnessed by a
lcp@104
  1064
function $g\in \prod@{x\in A}C(x,f{\tt`}x)$.
lcp@104
  1065
lcp@104
  1066
In principle, the Axiom of Choice is simple to derive in Constructive Type
lcp@333
  1067
Theory.  The following definitions work:
lcp@104
  1068
\begin{eqnarray*}
lcp@104
  1069
    f & \equiv & {\tt fst} \circ h \\
lcp@104
  1070
    g & \equiv & {\tt snd} \circ h
lcp@104
  1071
\end{eqnarray*}
lcp@314
  1072
But a completely formal proof is hard to find.  The rules can be applied in
lcp@314
  1073
countless ways, yielding many higher-order unifiers.  The proof can get
lcp@314
  1074
bogged down in the details.  But with a careful selection of derived rules
paulson@6170
  1075
(recall Fig.\ts\ref{ctt-derived}) and the type-checking tactics, we can
lcp@314
  1076
prove the theorem in nine steps.
lcp@104
  1077
\begin{ttbox}
paulson@5151
  1078
val prems = Goal
lcp@284
  1079
    "[| A type;  !!x. x:A ==> B(x) type;                    \ttback
lcp@284
  1080
\ttback       !!x y.[| x:A;  y:B(x) |] ==> C(x,y) type            \ttback
lcp@284
  1081
\ttback    |] ==> ?a : PROD h: (PROD x:A. SUM y:B(x). C(x,y)).    \ttback
lcp@284
  1082
\ttback                     (SUM f: (PROD x:A. B(x)). PROD x:A. C(x, f`x))";
lcp@104
  1083
{\out Level 0}
lcp@104
  1084
{\out ?a : (PROD x:A. SUM y:B(x). C(x,y)) -->}
lcp@104
  1085
{\out      (SUM f:PROD x:A. B(x). PROD x:A. C(x,f ` x))}
lcp@104
  1086
{\out  1. ?a : (PROD x:A. SUM y:B(x). C(x,y)) -->}
lcp@104
  1087
{\out          (SUM f:PROD x:A. B(x). PROD x:A. C(x,f ` x))}
lcp@111
  1088
\ttbreak
lcp@111
  1089
{\out val prems = ["A type  [A type]",}
lcp@111
  1090
{\out              "?x : A ==> B(?x) type  [!!x. x : A ==> B(x) type]",}
lcp@111
  1091
{\out              "[| ?x : A; ?y : B(?x) |] ==> C(?x, ?y) type}
lcp@111
  1092
{\out               [!!x y. [| x : A; y : B(x) |] ==> C(x, y) type]"]}
lcp@111
  1093
{\out             : thm list}
lcp@104
  1094
\end{ttbox}
lcp@104
  1095
First, \ttindex{intr_tac} applies introduction rules and performs routine
paulson@6170
  1096
type-checking.  This instantiates~$\Var{a}$ to a construction involving
lcp@314
  1097
a $\lambda$-abstraction and an ordered pair.  The pair's components are
lcp@314
  1098
themselves $\lambda$-abstractions and there is a subgoal for each.
lcp@104
  1099
\begin{ttbox}
lcp@104
  1100
by (intr_tac prems);
lcp@104
  1101
{\out Level 1}
lcp@104
  1102
{\out lam x. <lam xa. ?b7(x,xa),lam xa. ?b8(x,xa)>}
lcp@104
  1103
{\out : (PROD x:A. SUM y:B(x). C(x,y)) -->}
lcp@104
  1104
{\out   (SUM f:PROD x:A. B(x). PROD x:A. C(x,f ` x))}
lcp@114
  1105
\ttbreak
lcp@284
  1106
{\out  1. !!h x.}
lcp@284
  1107
{\out        [| h : PROD x:A. SUM y:B(x). C(x,y); x : A |] ==>}
lcp@284
  1108
{\out        ?b7(h,x) : B(x)}
lcp@114
  1109
\ttbreak
lcp@284
  1110
{\out  2. !!h x.}
lcp@284
  1111
{\out        [| h : PROD x:A. SUM y:B(x). C(x,y); x : A |] ==>}
lcp@284
  1112
{\out        ?b8(h,x) : C(x,(lam x. ?b7(h,x)) ` x)}
lcp@104
  1113
\end{ttbox}
lcp@104
  1114
Subgoal~1 asks to find the choice function itself, taking $x\in A$ to some
lcp@284
  1115
$\Var{b@7}(h,x)\in B(x)$.  Subgoal~2 asks, given $x\in A$, for a proof
lcp@284
  1116
object $\Var{b@8}(h,x)$ to witness that the choice function's argument and
lcp@284
  1117
result lie in the relation~$C$.  This latter task will take up most of the
lcp@284
  1118
proof.
lcp@314
  1119
\index{*ProdE theorem}\index{*SumE_fst theorem}\index{*RS}
lcp@104
  1120
\begin{ttbox}
lcp@104
  1121
by (eresolve_tac [ProdE RS SumE_fst] 1);
lcp@104
  1122
{\out Level 2}
lcp@104
  1123
{\out lam x. <lam xa. fst(x ` xa),lam xa. ?b8(x,xa)>}
lcp@104
  1124
{\out : (PROD x:A. SUM y:B(x). C(x,y)) -->}
lcp@104
  1125
{\out   (SUM f:PROD x:A. B(x). PROD x:A. C(x,f ` x))}
lcp@114
  1126
\ttbreak
lcp@284
  1127
{\out  1. !!h x. x : A ==> x : A}
lcp@284
  1128
{\out  2. !!h x.}
lcp@284
  1129
{\out        [| h : PROD x:A. SUM y:B(x). C(x,y); x : A |] ==>}
lcp@284
  1130
{\out        ?b8(h,x) : C(x,(lam x. fst(h ` x)) ` x)}
lcp@104
  1131
\end{ttbox}
lcp@314
  1132
Above, we have composed {\tt fst} with the function~$h$.  Unification
lcp@314
  1133
has deduced that the function must be applied to $x\in A$.  We have our
lcp@314
  1134
choice function.
lcp@104
  1135
\begin{ttbox}
lcp@104
  1136
by (assume_tac 1);
lcp@104
  1137
{\out Level 3}
lcp@104
  1138
{\out lam x. <lam xa. fst(x ` xa),lam xa. ?b8(x,xa)>}
lcp@104
  1139
{\out : (PROD x:A. SUM y:B(x). C(x,y)) -->}
lcp@104
  1140
{\out   (SUM f:PROD x:A. B(x). PROD x:A. C(x,f ` x))}
lcp@284
  1141
{\out  1. !!h x.}
lcp@284
  1142
{\out        [| h : PROD x:A. SUM y:B(x). C(x,y); x : A |] ==>}
lcp@284
  1143
{\out        ?b8(h,x) : C(x,(lam x. fst(h ` x)) ` x)}
lcp@104
  1144
\end{ttbox}
lcp@314
  1145
Before we can compose {\tt snd} with~$h$, the arguments of $C$ must be
lcp@314
  1146
simplified.  The derived rule \tdx{replace_type} lets us replace a type
lcp@284
  1147
by any equivalent type, shown below as the schematic term $\Var{A@{13}}(h,x)$:
lcp@104
  1148
\begin{ttbox}
lcp@104
  1149
by (resolve_tac [replace_type] 1);
lcp@104
  1150
{\out Level 4}
lcp@104
  1151
{\out lam x. <lam xa. fst(x ` xa),lam xa. ?b8(x,xa)>}
lcp@104
  1152
{\out : (PROD x:A. SUM y:B(x). C(x,y)) -->}
lcp@104
  1153
{\out   (SUM f:PROD x:A. B(x). PROD x:A. C(x,f ` x))}
lcp@114
  1154
\ttbreak
lcp@284
  1155
{\out  1. !!h x.}
lcp@284
  1156
{\out        [| h : PROD x:A. SUM y:B(x). C(x,y); x : A |] ==>}
lcp@284
  1157
{\out        C(x,(lam x. fst(h ` x)) ` x) = ?A13(h,x)}
lcp@114
  1158
\ttbreak
lcp@284
  1159
{\out  2. !!h x.}
lcp@284
  1160
{\out        [| h : PROD x:A. SUM y:B(x). C(x,y); x : A |] ==>}
lcp@284
  1161
{\out        ?b8(h,x) : ?A13(h,x)}
lcp@104
  1162
\end{ttbox}
lcp@314
  1163
The derived rule \tdx{subst_eqtyparg} lets us simplify a type's
lcp@104
  1164
argument (by currying, $C(x)$ is a unary type operator):
lcp@104
  1165
\begin{ttbox}
lcp@104
  1166
by (resolve_tac [subst_eqtyparg] 1);
lcp@104
  1167
{\out Level 5}
lcp@104
  1168
{\out lam x. <lam xa. fst(x ` xa),lam xa. ?b8(x,xa)>}
lcp@104
  1169
{\out : (PROD x:A. SUM y:B(x). C(x,y)) -->}
lcp@104
  1170
{\out   (SUM f:PROD x:A. B(x). PROD x:A. C(x,f ` x))}
lcp@114
  1171
\ttbreak
lcp@284
  1172
{\out  1. !!h x.}
lcp@284
  1173
{\out        [| h : PROD x:A. SUM y:B(x). C(x,y); x : A |] ==>}
lcp@284
  1174
{\out        (lam x. fst(h ` x)) ` x = ?c14(h,x) : ?A14(h,x)}
lcp@114
  1175
\ttbreak
lcp@284
  1176
{\out  2. !!h x z.}
lcp@284
  1177
{\out        [| h : PROD x:A. SUM y:B(x). C(x,y); x : A;}
lcp@284
  1178
{\out           z : ?A14(h,x) |] ==>}
lcp@104
  1179
{\out        C(x,z) type}
lcp@114
  1180
\ttbreak
lcp@284
  1181
{\out  3. !!h x.}
lcp@284
  1182
{\out        [| h : PROD x:A. SUM y:B(x). C(x,y); x : A |] ==>}
lcp@284
  1183
{\out        ?b8(h,x) : C(x,?c14(h,x))}
lcp@104
  1184
\end{ttbox}
lcp@284
  1185
Subgoal~1 requires simply $\beta$-contraction, which is the rule
lcp@314
  1186
\tdx{ProdC}.  The term $\Var{c@{14}}(h,x)$ in the last subgoal
lcp@284
  1187
receives the contracted result.
lcp@104
  1188
\begin{ttbox}
lcp@104
  1189
by (resolve_tac [ProdC] 1);
lcp@104
  1190
{\out Level 6}
lcp@104
  1191
{\out lam x. <lam xa. fst(x ` xa),lam xa. ?b8(x,xa)>}
lcp@104
  1192
{\out : (PROD x:A. SUM y:B(x). C(x,y)) -->}
lcp@104
  1193
{\out   (SUM f:PROD x:A. B(x). PROD x:A. C(x,f ` x))}
lcp@114
  1194
\ttbreak
lcp@284
  1195
{\out  1. !!h x.}
lcp@284
  1196
{\out        [| h : PROD x:A. SUM y:B(x). C(x,y); x : A |] ==>}
lcp@284
  1197
{\out        x : ?A15(h,x)}
lcp@114
  1198
\ttbreak
lcp@284
  1199
{\out  2. !!h x xa.}
lcp@284
  1200
{\out        [| h : PROD x:A. SUM y:B(x). C(x,y); x : A;}
lcp@284
  1201
{\out           xa : ?A15(h,x) |] ==>}
lcp@284
  1202
{\out        fst(h ` xa) : ?B15(h,x,xa)}
lcp@114
  1203
\ttbreak
lcp@284
  1204
{\out  3. !!h x z.}
lcp@284
  1205
{\out        [| h : PROD x:A. SUM y:B(x). C(x,y); x : A;}
lcp@284
  1206
{\out           z : ?B15(h,x,x) |] ==>}
lcp@104
  1207
{\out        C(x,z) type}
lcp@114
  1208
\ttbreak
lcp@284
  1209
{\out  4. !!h x.}
lcp@284
  1210
{\out        [| h : PROD x:A. SUM y:B(x). C(x,y); x : A |] ==>}
lcp@284
  1211
{\out        ?b8(h,x) : C(x,fst(h ` x))}
lcp@104
  1212
\end{ttbox}
paulson@6170
  1213
Routine type-checking goals proliferate in Constructive Type Theory, but
lcp@104
  1214
\ttindex{typechk_tac} quickly solves them.  Note the inclusion of
lcp@314
  1215
\tdx{SumE_fst} along with the premises.
lcp@104
  1216
\begin{ttbox}
lcp@104
  1217
by (typechk_tac (SumE_fst::prems));
lcp@104
  1218
{\out Level 7}
lcp@104
  1219
{\out lam x. <lam xa. fst(x ` xa),lam xa. ?b8(x,xa)>}
lcp@104
  1220
{\out : (PROD x:A. SUM y:B(x). C(x,y)) -->}
lcp@104
  1221
{\out   (SUM f:PROD x:A. B(x). PROD x:A. C(x,f ` x))}
lcp@284
  1222
\ttbreak
lcp@284
  1223
{\out  1. !!h x.}
lcp@284
  1224
{\out        [| h : PROD x:A. SUM y:B(x). C(x,y); x : A |] ==>}
lcp@284
  1225
{\out        ?b8(h,x) : C(x,fst(h ` x))}
lcp@104
  1226
\end{ttbox}
lcp@314
  1227
We are finally ready to compose {\tt snd} with~$h$.
lcp@314
  1228
\index{*ProdE theorem}\index{*SumE_snd theorem}\index{*RS}
lcp@104
  1229
\begin{ttbox}
lcp@104
  1230
by (eresolve_tac [ProdE RS SumE_snd] 1);
lcp@104
  1231
{\out Level 8}
lcp@104
  1232
{\out lam x. <lam xa. fst(x ` xa),lam xa. snd(x ` xa)>}
lcp@104
  1233
{\out : (PROD x:A. SUM y:B(x). C(x,y)) -->}
lcp@104
  1234
{\out   (SUM f:PROD x:A. B(x). PROD x:A. C(x,f ` x))}
lcp@284
  1235
\ttbreak
lcp@284
  1236
{\out  1. !!h x. x : A ==> x : A}
lcp@284
  1237
{\out  2. !!h x. x : A ==> B(x) type}
lcp@284
  1238
{\out  3. !!h x xa. [| x : A; xa : B(x) |] ==> C(x,xa) type}
lcp@104
  1239
\end{ttbox}
lcp@104
  1240
The proof object has reached its final form.  We call \ttindex{typechk_tac}
paulson@6170
  1241
to finish the type-checking.
lcp@104
  1242
\begin{ttbox}
lcp@104
  1243
by (typechk_tac prems);
lcp@104
  1244
{\out Level 9}
lcp@104
  1245
{\out lam x. <lam xa. fst(x ` xa),lam xa. snd(x ` xa)>}
lcp@104
  1246
{\out : (PROD x:A. SUM y:B(x). C(x,y)) -->}
lcp@104
  1247
{\out   (SUM f:PROD x:A. B(x). PROD x:A. C(x,f ` x))}
lcp@104
  1248
{\out No subgoals!}
lcp@104
  1249
\end{ttbox}
lcp@314
  1250
It might be instructive to compare this proof with Martin-L\"of's forward
lcp@314
  1251
proof of the Axiom of Choice \cite[page~50]{martinlof84}.
lcp@314
  1252
lcp@314
  1253
\index{Constructive Type Theory|)}