doc-src/Logics/CTT.tex
author berghofe
Fri, 02 May 1997 16:18:49 +0200
changeset 3096 ccc2c92bb232
parent 975 6c280d1dac35
child 3133 8c55b0f16da2
permissions -rw-r--r--
Updated to LaTeX 2e
lcp@104
     1
%% $Id$
lcp@104
     2
\chapter{Constructive Type Theory}
lcp@314
     3
\index{Constructive Type Theory|(}
lcp@314
     4
lcp@104
     5
Martin-L\"of's Constructive Type Theory \cite{martinlof84,nordstrom90} can
lcp@104
     6
be viewed at many different levels.  It is a formal system that embodies
lcp@104
     7
the principles of intuitionistic mathematics; it embodies the
lcp@104
     8
interpretation of propositions as types; it is a vehicle for deriving
lcp@314
     9
programs from proofs.  
lcp@314
    10
lcp@314
    11
Thompson's book~\cite{thompson91} gives a readable and thorough account of
lcp@314
    12
Type Theory.  Nuprl is an elaborate implementation~\cite{constable86}.
lcp@314
    13
{\sc alf} is a more recent tool that allows proof terms to be edited
lcp@314
    14
directly~\cite{alf}.
lcp@104
    15
lcp@104
    16
Isabelle's original formulation of Type Theory was a kind of sequent
lcp@104
    17
calculus, following Martin-L\"of~\cite{martinlof84}.  It included rules for
lcp@104
    18
building the context, namely variable bindings with their types.  A typical
lcp@104
    19
judgement was
lcp@104
    20
\[   a(x@1,\ldots,x@n)\in A(x@1,\ldots,x@n) \; 
lcp@104
    21
    [ 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
    22
\]
lcp@104
    23
This sequent calculus was not satisfactory because assumptions like
lcp@104
    24
`suppose $A$ is a type' or `suppose $B(x)$ is a type for all $x$ in $A$'
lcp@104
    25
could not be formalized.  
lcp@104
    26
lcp@314
    27
The theory~\thydx{CTT} implements Constructive Type Theory, using
lcp@104
    28
natural deduction.  The judgement above is expressed using $\Forall$ and
lcp@104
    29
$\Imp$:
lcp@104
    30
\[ \begin{array}{r@{}l}
lcp@104
    31
     \Forall x@1\ldots x@n. &
lcp@111
    32
          \List{x@1\in A@1; 
lcp@111
    33
                x@2\in A@2(x@1); \cdots \; 
lcp@111
    34
                x@n\in A@n(x@1,\ldots,x@{n-1})} \Imp \\
lcp@104
    35
     &  \qquad\qquad a(x@1,\ldots,x@n)\in A(x@1,\ldots,x@n) 
lcp@104
    36
    \end{array}
lcp@104
    37
\]
lcp@104
    38
Assumptions can use all the judgement forms, for instance to express that
lcp@104
    39
$B$ is a family of types over~$A$:
lcp@104
    40
\[ \Forall x . x\in A \Imp B(x)\;{\rm type} \]
lcp@104
    41
To justify the {\CTT} formulation it is probably best to appeal directly
lcp@104
    42
to the semantic explanations of the rules~\cite{martinlof84}, rather than
lcp@104
    43
to the rules themselves.  The order of assumptions no longer matters,
lcp@104
    44
unlike in standard Type Theory.  Contexts, which are typical of many modern
lcp@104
    45
type theories, are difficult to represent in Isabelle.  In particular, it
lcp@104
    46
is difficult to enforce that all the variables in a context are distinct.
lcp@314
    47
\index{assumptions!in {\CTT}}
lcp@104
    48
lcp@314
    49
The theory does not use polymorphism.  Terms in {\CTT} have type~\tydx{i}, the
lcp@314
    50
type of individuals.  Types in {\CTT} have type~\tydx{t}.
lcp@104
    51
lcp@314
    52
\begin{figure} \tabcolsep=1em  %wider spacing in tables
lcp@314
    53
\begin{center}
lcp@314
    54
\begin{tabular}{rrr} 
lcp@314
    55
  \it name      & \it meta-type         & \it description \\ 
lcp@314
    56
  \cdx{Type}    & $t \to prop$          & judgement form \\
lcp@314
    57
  \cdx{Eqtype}  & $[t,t]\to prop$       & judgement form\\
lcp@314
    58
  \cdx{Elem}    & $[i, t]\to prop$      & judgement form\\
lcp@314
    59
  \cdx{Eqelem}  & $[i, i, t]\to prop$   & judgement form\\
lcp@314
    60
  \cdx{Reduce}  & $[i, i]\to prop$      & extra judgement form\\[2ex]
lcp@314
    61
lcp@314
    62
  \cdx{N}       &     $t$               & natural numbers type\\
lcp@314
    63
  \cdx{0}       &     $i$               & constructor\\
lcp@314
    64
  \cdx{succ}    & $i\to i$              & constructor\\
lcp@314
    65
  \cdx{rec}     & $[i,i,[i,i]\to i]\to i$       & eliminator\\[2ex]
lcp@314
    66
  \cdx{Prod}    & $[t,i\to t]\to t$     & general product type\\
lcp@314
    67
  \cdx{lambda}  & $(i\to i)\to i$       & constructor\\[2ex]
lcp@314
    68
  \cdx{Sum}     & $[t, i\to t]\to t$    & general sum type\\
lcp@314
    69
  \cdx{pair}    & $[i,i]\to i$          & constructor\\
lcp@314
    70
  \cdx{split}   & $[i,[i,i]\to i]\to i$ & eliminator\\
lcp@314
    71
  \cdx{fst} \cdx{snd} & $i\to i$        & projections\\[2ex]
lcp@314
    72
  \cdx{inl} \cdx{inr} & $i\to i$        & constructors for $+$\\
lcp@314
    73
  \cdx{when}    & $[i,i\to i, i\to i]\to i$    & eliminator for $+$\\[2ex]
lcp@314
    74
  \cdx{Eq}      & $[t,i,i]\to t$        & equality type\\
lcp@314
    75
  \cdx{eq}      & $i$                   & constructor\\[2ex]
lcp@314
    76
  \cdx{F}       & $t$                   & empty type\\
lcp@314
    77
  \cdx{contr}   & $i\to i$              & eliminator\\[2ex]
lcp@314
    78
  \cdx{T}       & $t$                   & singleton type\\
lcp@314
    79
  \cdx{tt}      & $i$                   & constructor
lcp@314
    80
\end{tabular}
lcp@314
    81
\end{center}
lcp@314
    82
\caption{The constants of {\CTT}} \label{ctt-constants}
lcp@314
    83
\end{figure}
lcp@314
    84
lcp@314
    85
lcp@314
    86
{\CTT} supports all of Type Theory apart from list types, well-ordering
lcp@104
    87
types, and universes.  Universes could be introduced {\em\`a la Tarski},
lcp@104
    88
adding new constants as names for types.  The formulation {\em\`a la
lcp@314
    89
  Russell}, where types denote themselves, is only possible if we identify
lcp@314
    90
the meta-types~{\tt i} and~{\tt t}.  Most published formulations of
lcp@314
    91
well-ordering types have difficulties involving extensionality of
lcp@314
    92
functions; I suggest that you use some other method for defining recursive
lcp@314
    93
types.  List types are easy to introduce by declaring new rules.
lcp@104
    94
lcp@104
    95
{\CTT} uses the 1982 version of Type Theory, with extensional equality.
lcp@104
    96
The computation $a=b\in A$ and the equality $c\in Eq(A,a,b)$ are
lcp@104
    97
interchangeable.  Its rewriting tactics prove theorems of the form $a=b\in
lcp@104
    98
A$.  It could be modified to have intensional equality, but rewriting
lcp@104
    99
tactics would have to prove theorems of the form $c\in Eq(A,a,b)$ and the
lcp@314
   100
computation rules might require a separate simplifier.
lcp@104
   101
lcp@104
   102
lcp@104
   103
\begin{figure} \tabcolsep=1em  %wider spacing in tables
lcp@314
   104
\index{lambda abs@$\lambda$-abstractions!in \CTT}
lcp@104
   105
\begin{center}
lcp@104
   106
\begin{tabular}{llrrr} 
lcp@314
   107
  \it symbol &\it name     &\it meta-type & \it priority & \it description \\
lcp@314
   108
  \sdx{lam} & \cdx{lambda}  & $(i\To o)\To i$ & 10 & $\lambda$-abstraction
lcp@104
   109
\end{tabular}
lcp@104
   110
\end{center}
lcp@104
   111
\subcaption{Binders} 
lcp@104
   112
lcp@104
   113
\begin{center}
lcp@314
   114
\index{*"` symbol}\index{function applications!in \CTT}
lcp@314
   115
\index{*"+ symbol}
lcp@104
   116
\begin{tabular}{rrrr} 
lcp@314
   117
  \it symbol & \it meta-type    & \it priority & \it description \\ 
lcp@111
   118
  \tt `         & $[i,i]\to i$  & Left 55       & function application\\
lcp@111
   119
  \tt +         & $[t,t]\to t$  & Right 30      & sum of two types
lcp@104
   120
\end{tabular}
lcp@104
   121
\end{center}
lcp@104
   122
\subcaption{Infixes}
lcp@104
   123
lcp@314
   124
\index{*"* symbol}
lcp@314
   125
\index{*"-"-"> symbol}
lcp@104
   126
\begin{center} \tt\frenchspacing
lcp@104
   127
\begin{tabular}{rrr} 
lcp@111
   128
  \it external                  & \it internal  & \it standard notation \\ 
lcp@314
   129
  \sdx{PROD} $x$:$A$ . $B[x]$   &  Prod($A$, $\lambda x.B[x]$) &
lcp@111
   130
        \rm product $\prod@{x\in A}B[x]$ \\
lcp@314
   131
  \sdx{SUM} $x$:$A$ . $B[x]$    & Sum($A$, $\lambda x.B[x]$) &
lcp@111
   132
        \rm sum $\sum@{x\in A}B[x]$ \\
lcp@104
   133
  $A$ --> $B$     &  Prod($A$, $\lambda x.B$) &
lcp@111
   134
        \rm function space $A\to B$ \\
lcp@104
   135
  $A$ * $B$       &  Sum($A$, $\lambda x.B$)  &
lcp@111
   136
        \rm binary product $A\times B$
lcp@104
   137
\end{tabular}
lcp@104
   138
\end{center}
lcp@104
   139
\subcaption{Translations} 
lcp@104
   140
lcp@314
   141
\index{*"= symbol}
lcp@104
   142
\begin{center}
lcp@104
   143
\dquotes
lcp@104
   144
\[ \begin{array}{rcl}
lcp@111
   145
prop    & = &  type " type"       \\
lcp@111
   146
        & | & type " = " type     \\
lcp@111
   147
        & | & term " : " type        \\
lcp@111
   148
        & | & term " = " term " : " type 
lcp@104
   149
\\[2ex]
lcp@111
   150
type    & = & \hbox{expression of type~$t$} \\
lcp@111
   151
        & | & "PROD~" id " : " type " . " type  \\
lcp@111
   152
        & | & "SUM~~" id " : " type " . " type 
lcp@104
   153
\\[2ex]
lcp@111
   154
term    & = & \hbox{expression of type~$i$} \\
lcp@111
   155
        & | & "lam " id~id^* " . " term   \\
lcp@111
   156
        & | & "< " term " , " term " >"
lcp@104
   157
\end{array} 
lcp@104
   158
\]
lcp@104
   159
\end{center}
lcp@104
   160
\subcaption{Grammar}
lcp@104
   161
\caption{Syntax of {\CTT}} \label{ctt-syntax}
lcp@104
   162
\end{figure}
lcp@104
   163
lcp@104
   164
%%%%\section{Generic Packages}  typedsimp.ML????????????????
lcp@104
   165
lcp@104
   166
lcp@104
   167
\section{Syntax}
lcp@284
   168
The constants are shown in Fig.\ts\ref{ctt-constants}.  The infixes include
lcp@104
   169
the function application operator (sometimes called `apply'), and the
lcp@104
   170
2-place type operators.  Note that meta-level abstraction and application,
lcp@104
   171
$\lambda x.b$ and $f(a)$, differ from object-level abstraction and
lcp@104
   172
application, \hbox{\tt lam $x$.$b$} and $b{\tt`}a$.  A {\CTT}
lcp@104
   173
function~$f$ is simply an individual as far as Isabelle is concerned: its
lcp@104
   174
Isabelle type is~$i$, not say $i\To i$.
lcp@104
   175
lcp@314
   176
The notation for~{\CTT} (Fig.\ts\ref{ctt-syntax}) is based on that of
lcp@314
   177
Nordstr\"om et al.~\cite{nordstrom90}.  The empty type is called $F$ and
lcp@314
   178
the one-element type is $T$; other finite types are built as $T+T+T$, etc.
lcp@314
   179
lcp@314
   180
\index{*SUM symbol}\index{*PROD symbol}
lcp@314
   181
Quantification is expressed using general sums $\sum@{x\in A}B[x]$ and
lcp@314
   182
products $\prod@{x\in A}B[x]$.  Instead of {\tt Sum($A$,$B$)} and {\tt
lcp@314
   183
  Prod($A$,$B$)} we may write \hbox{\tt SUM $x$:$A$.$B[x]$} and \hbox{\tt
lcp@314
   184
  PROD $x$:$A$.$B[x]$}.  For example, we may write
lcp@104
   185
\begin{ttbox}
lcp@284
   186
SUM y:B. PROD x:A. C(x,y)   {\rm for}   Sum(B, \%y. Prod(A, \%x. C(x,y)))
lcp@104
   187
\end{ttbox}
lcp@104
   188
The special cases as \hbox{\tt$A$*$B$} and \hbox{\tt$A$-->$B$} abbreviate
lcp@104
   189
general sums and products over a constant family.\footnote{Unlike normal
lcp@104
   190
infix operators, {\tt*} and {\tt-->} merely define abbreviations; there are
lcp@104
   191
no constants~{\tt op~*} and~\hbox{\tt op~-->}.}  Isabelle accepts these
lcp@104
   192
abbreviations in parsing and uses them whenever possible for printing.
lcp@104
   193
lcp@104
   194
lcp@104
   195
\begin{figure} 
lcp@104
   196
\begin{ttbox}
lcp@314
   197
\tdx{refl_type}         A type ==> A = A
lcp@314
   198
\tdx{refl_elem}         a : A ==> a = a : A
lcp@104
   199
lcp@314
   200
\tdx{sym_type}          A = B ==> B = A
lcp@314
   201
\tdx{sym_elem}          a = b : A ==> b = a : A
lcp@104
   202
lcp@314
   203
\tdx{trans_type}        [| A = B;  B = C |] ==> A = C
lcp@314
   204
\tdx{trans_elem}        [| a = b : A;  b = c : A |] ==> a = c : A
lcp@104
   205
lcp@314
   206
\tdx{equal_types}       [| a : A;  A = B |] ==> a : B
lcp@314
   207
\tdx{equal_typesL}      [| a = b : A;  A = B |] ==> a = b : B
lcp@104
   208
lcp@314
   209
\tdx{subst_type}        [| a : A;  !!z. z:A ==> B(z) type |] ==> B(a) type
lcp@314
   210
\tdx{subst_typeL}       [| a = c : A;  !!z. z:A ==> B(z) = D(z) 
lcp@104
   211
                  |] ==> B(a) = D(c)
lcp@104
   212
lcp@314
   213
\tdx{subst_elem}        [| a : A;  !!z. z:A ==> b(z):B(z) |] ==> b(a):B(a)
lcp@314
   214
\tdx{subst_elemL}       [| a = c : A;  !!z. z:A ==> b(z) = d(z) : B(z) 
lcp@104
   215
                  |] ==> b(a) = d(c) : B(a)
lcp@104
   216
lcp@314
   217
\tdx{refl_red}          Reduce(a,a)
lcp@314
   218
\tdx{red_if_equal}      a = b : A ==> Reduce(a,b)
lcp@314
   219
\tdx{trans_red}         [| a = b : A;  Reduce(b,c) |] ==> a = c : A
lcp@104
   220
\end{ttbox}
lcp@104
   221
\caption{General equality rules} \label{ctt-equality}
lcp@104
   222
\end{figure}
lcp@104
   223
lcp@104
   224
lcp@104
   225
\begin{figure} 
lcp@104
   226
\begin{ttbox}
lcp@314
   227
\tdx{NF}        N type
lcp@104
   228
lcp@314
   229
\tdx{NI0}       0 : N
lcp@314
   230
\tdx{NI_succ}   a : N ==> succ(a) : N
lcp@314
   231
\tdx{NI_succL}  a = b : N ==> succ(a) = succ(b) : N
lcp@104
   232
lcp@314
   233
\tdx{NE}        [| p: N;  a: C(0);  
lcp@104
   234
             !!u v. [| u: N; v: C(u) |] ==> b(u,v): C(succ(u)) 
lcp@284
   235
          |] ==> rec(p, a, \%u v.b(u,v)) : C(p)
lcp@104
   236
lcp@314
   237
\tdx{NEL}       [| p = q : N;  a = c : C(0);  
lcp@104
   238
             !!u v. [| u: N; v: C(u) |] ==> b(u,v)=d(u,v): C(succ(u))
lcp@284
   239
          |] ==> rec(p, a, \%u v.b(u,v)) = rec(q,c,d) : C(p)
lcp@104
   240
lcp@314
   241
\tdx{NC0}       [| a: C(0);  
lcp@104
   242
             !!u v. [| u: N; v: C(u) |] ==> b(u,v): C(succ(u))
lcp@284
   243
          |] ==> rec(0, a, \%u v.b(u,v)) = a : C(0)
lcp@104
   244
lcp@314
   245
\tdx{NC_succ}   [| p: N;  a: C(0);  
lcp@104
   246
             !!u v. [| u: N; v: C(u) |] ==> b(u,v): C(succ(u)) 
lcp@284
   247
          |] ==> rec(succ(p), a, \%u v.b(u,v)) =
lcp@284
   248
                 b(p, rec(p, a, \%u v.b(u,v))) : C(succ(p))
lcp@104
   249
lcp@314
   250
\tdx{zero_ne_succ}      [| a: N;  0 = succ(a) : N |] ==> 0: F
lcp@104
   251
\end{ttbox}
lcp@104
   252
\caption{Rules for type~$N$} \label{ctt-N}
lcp@104
   253
\end{figure}
lcp@104
   254
lcp@104
   255
lcp@104
   256
\begin{figure} 
lcp@104
   257
\begin{ttbox}
lcp@314
   258
\tdx{ProdF}     [| A type; !!x. x:A ==> B(x) type |] ==> PROD x:A.B(x) type
lcp@314
   259
\tdx{ProdFL}    [| A = C;  !!x. x:A ==> B(x) = D(x) |] ==> 
lcp@104
   260
          PROD x:A.B(x) = PROD x:C.D(x)
lcp@104
   261
lcp@314
   262
\tdx{ProdI}     [| A type;  !!x. x:A ==> b(x):B(x)
lcp@104
   263
          |] ==> lam x.b(x) : PROD x:A.B(x)
lcp@314
   264
\tdx{ProdIL}    [| A type;  !!x. x:A ==> b(x) = c(x) : B(x)
lcp@104
   265
          |] ==> lam x.b(x) = lam x.c(x) : PROD x:A.B(x)
lcp@104
   266
lcp@314
   267
\tdx{ProdE}     [| p : PROD x:A.B(x);  a : A |] ==> p`a : B(a)
lcp@314
   268
\tdx{ProdEL}    [| p=q: PROD x:A.B(x);  a=b : A |] ==> p`a = q`b : B(a)
lcp@104
   269
lcp@314
   270
\tdx{ProdC}     [| a : A;  !!x. x:A ==> b(x) : B(x)
lcp@104
   271
          |] ==> (lam x.b(x)) ` a = b(a) : B(a)
lcp@104
   272
lcp@314
   273
\tdx{ProdC2}    p : PROD x:A.B(x) ==> (lam x. p`x) = p : PROD x:A.B(x)
lcp@104
   274
\end{ttbox}
lcp@314
   275
\caption{Rules for the product type $\prod\sb{x\in A}B[x]$} \label{ctt-prod}
lcp@104
   276
\end{figure}
lcp@104
   277
lcp@104
   278
lcp@104
   279
\begin{figure} 
lcp@104
   280
\begin{ttbox}
lcp@314
   281
\tdx{SumF}      [| A type;  !!x. x:A ==> B(x) type |] ==> SUM x:A.B(x) type
lcp@314
   282
\tdx{SumFL}     [| A = C;  !!x. x:A ==> B(x) = D(x) 
lcp@104
   283
          |] ==> SUM x:A.B(x) = SUM x:C.D(x)
lcp@104
   284
lcp@314
   285
\tdx{SumI}      [| a : A;  b : B(a) |] ==> <a,b> : SUM x:A.B(x)
lcp@314
   286
\tdx{SumIL}     [| a=c:A;  b=d:B(a) |] ==> <a,b> = <c,d> : SUM x:A.B(x)
lcp@104
   287
lcp@314
   288
\tdx{SumE}      [| p: SUM x:A.B(x);  
lcp@104
   289
             !!x y. [| x:A; y:B(x) |] ==> c(x,y): C(<x,y>) 
lcp@284
   290
          |] ==> split(p, \%x y.c(x,y)) : C(p)
lcp@104
   291
lcp@314
   292
\tdx{SumEL}     [| p=q : SUM x:A.B(x); 
lcp@104
   293
             !!x y. [| x:A; y:B(x) |] ==> c(x,y)=d(x,y): C(<x,y>)
lcp@284
   294
          |] ==> split(p, \%x y.c(x,y)) = split(q, \%x y.d(x,y)) : C(p)
lcp@104
   295
lcp@314
   296
\tdx{SumC}      [| a: A;  b: B(a);
lcp@104
   297
             !!x y. [| x:A; y:B(x) |] ==> c(x,y): C(<x,y>)
lcp@284
   298
          |] ==> split(<a,b>, \%x y.c(x,y)) = c(a,b) : C(<a,b>)
lcp@104
   299
lcp@314
   300
\tdx{fst_def}   fst(a) == split(a, \%x y.x)
lcp@314
   301
\tdx{snd_def}   snd(a) == split(a, \%x y.y)
lcp@104
   302
\end{ttbox}
lcp@314
   303
\caption{Rules for the sum type $\sum\sb{x\in A}B[x]$} \label{ctt-sum}
lcp@104
   304
\end{figure}
lcp@104
   305
lcp@104
   306
lcp@104
   307
\begin{figure} 
lcp@104
   308
\begin{ttbox}
lcp@314
   309
\tdx{PlusF}       [| A type;  B type |] ==> A+B type
lcp@314
   310
\tdx{PlusFL}      [| A = C;  B = D |] ==> A+B = C+D
lcp@104
   311
lcp@314
   312
\tdx{PlusI_inl}   [| a : A;  B type |] ==> inl(a) : A+B
lcp@314
   313
\tdx{PlusI_inlL}  [| a = c : A;  B type |] ==> inl(a) = inl(c) : A+B
lcp@104
   314
lcp@314
   315
\tdx{PlusI_inr}   [| A type;  b : B |] ==> inr(b) : A+B
lcp@314
   316
\tdx{PlusI_inrL}  [| A type;  b = d : B |] ==> inr(b) = inr(d) : A+B
lcp@104
   317
lcp@314
   318
\tdx{PlusE}     [| p: A+B;
lcp@104
   319
             !!x. x:A ==> c(x): C(inl(x));  
lcp@104
   320
             !!y. y:B ==> d(y): C(inr(y))
lcp@284
   321
          |] ==> when(p, \%x.c(x), \%y.d(y)) : C(p)
lcp@104
   322
lcp@314
   323
\tdx{PlusEL}    [| p = q : A+B;
lcp@104
   324
             !!x. x: A ==> c(x) = e(x) : C(inl(x));   
lcp@104
   325
             !!y. y: B ==> d(y) = f(y) : C(inr(y))
lcp@284
   326
          |] ==> when(p, \%x.c(x), \%y.d(y)) = 
lcp@284
   327
                 when(q, \%x.e(x), \%y.f(y)) : C(p)
lcp@104
   328
lcp@314
   329
\tdx{PlusC_inl} [| a: A;
lcp@104
   330
             !!x. x:A ==> c(x): C(inl(x));  
lcp@104
   331
             !!y. y:B ==> d(y): C(inr(y))
lcp@284
   332
          |] ==> when(inl(a), \%x.c(x), \%y.d(y)) = c(a) : C(inl(a))
lcp@104
   333
lcp@314
   334
\tdx{PlusC_inr} [| b: B;
lcp@104
   335
             !!x. x:A ==> c(x): C(inl(x));  
lcp@104
   336
             !!y. y:B ==> d(y): C(inr(y))
lcp@284
   337
          |] ==> when(inr(b), \%x.c(x), \%y.d(y)) = d(b) : C(inr(b))
lcp@104
   338
\end{ttbox}
lcp@104
   339
\caption{Rules for the binary sum type $A+B$} \label{ctt-plus}
lcp@104
   340
\end{figure}
lcp@104
   341
lcp@104
   342
lcp@104
   343
\begin{figure} 
lcp@104
   344
\begin{ttbox}
lcp@314
   345
\tdx{FF}        F type
lcp@314
   346
\tdx{FE}        [| p: F;  C type |] ==> contr(p) : C
lcp@314
   347
\tdx{FEL}       [| p = q : F;  C type |] ==> contr(p) = contr(q) : C
lcp@314
   348
lcp@314
   349
\tdx{TF}        T type
lcp@314
   350
\tdx{TI}        tt : T
lcp@314
   351
\tdx{TE}        [| p : T;  c : C(tt) |] ==> c : C(p)
lcp@314
   352
\tdx{TEL}       [| p = q : T;  c = d : C(tt) |] ==> c = d : C(p)
lcp@314
   353
\tdx{TC}        p : T ==> p = tt : T)
lcp@104
   354
\end{ttbox}
lcp@104
   355
lcp@314
   356
\caption{Rules for types $F$ and $T$} \label{ctt-ft}
lcp@104
   357
\end{figure}
lcp@104
   358
lcp@104
   359
lcp@104
   360
\begin{figure} 
lcp@104
   361
\begin{ttbox}
lcp@314
   362
\tdx{EqF}       [| A type;  a : A;  b : A |] ==> Eq(A,a,b) type
lcp@314
   363
\tdx{EqFL}      [| A=B;  a=c: A;  b=d : A |] ==> Eq(A,a,b) = Eq(B,c,d)
lcp@314
   364
\tdx{EqI}       a = b : A ==> eq : Eq(A,a,b)
lcp@314
   365
\tdx{EqE}       p : Eq(A,a,b) ==> a = b : A
lcp@314
   366
\tdx{EqC}       p : Eq(A,a,b) ==> p = eq : Eq(A,a,b)
lcp@314
   367
\end{ttbox}
lcp@314
   368
\caption{Rules for the equality type $Eq(A,a,b)$} \label{ctt-eq}
lcp@314
   369
\end{figure}
lcp@104
   370
lcp@314
   371
lcp@314
   372
\begin{figure} 
lcp@314
   373
\begin{ttbox}
lcp@314
   374
\tdx{replace_type}    [| B = A;  a : A |] ==> a : B
lcp@314
   375
\tdx{subst_eqtyparg}  [| a=c : A;  !!z. z:A ==> B(z) type |] ==> B(a)=B(c)
lcp@314
   376
lcp@314
   377
\tdx{subst_prodE}     [| p: Prod(A,B);  a: A;  !!z. z: B(a) ==> c(z): C(z)
lcp@104
   378
                |] ==> c(p`a): C(p`a)
lcp@104
   379
lcp@314
   380
\tdx{SumIL2}    [| c=a : A;  d=b : B(a) |] ==> <c,d> = <a,b> : Sum(A,B)
lcp@104
   381
lcp@314
   382
\tdx{SumE_fst}  p : Sum(A,B) ==> fst(p) : A
lcp@104
   383
lcp@314
   384
\tdx{SumE_snd}  [| p: Sum(A,B);  A type;  !!x. x:A ==> B(x) type
lcp@104
   385
          |] ==> snd(p) : B(fst(p))
lcp@104
   386
\end{ttbox}
lcp@104
   387
lcp@104
   388
\caption{Derived rules for {\CTT}} \label{ctt-derived}
lcp@104
   389
\end{figure}
lcp@104
   390
lcp@104
   391
lcp@104
   392
\section{Rules of inference}
lcp@104
   393
The rules obey the following naming conventions.  Type formation rules have
lcp@104
   394
the suffix~{\tt F}\@.  Introduction rules have the suffix~{\tt I}\@.
lcp@104
   395
Elimination rules have the suffix~{\tt E}\@.  Computation rules, which
lcp@104
   396
describe the reduction of eliminators, have the suffix~{\tt C}\@.  The
lcp@104
   397
equality versions of the rules (which permit reductions on subterms) are
lcp@333
   398
called {\bf long} rules; their names have the suffix~{\tt L}\@.
lcp@333
   399
Introduction and computation rules are often further suffixed with
lcp@104
   400
constructor names.
lcp@104
   401
lcp@314
   402
Figure~\ref{ctt-equality} presents the equality rules.  Most of them are
lcp@314
   403
straightforward: reflexivity, symmetry, transitivity and substitution.  The
lcp@314
   404
judgement \cdx{Reduce} does not belong to Type Theory proper; it has
lcp@314
   405
been added to implement rewriting.  The judgement ${\tt Reduce}(a,b)$ holds
lcp@314
   406
when $a=b:A$ holds.  It also holds when $a$ and $b$ are syntactically
lcp@314
   407
identical, even if they are ill-typed, because rule {\tt refl_red} does
lcp@314
   408
not verify that $a$ belongs to $A$.  
lcp@104
   409
lcp@314
   410
The {\tt Reduce} rules do not give rise to new theorems about the standard
lcp@314
   411
judgements.  The only rule with {\tt Reduce} in a premise is
lcp@314
   412
{\tt trans_red}, whose other premise ensures that $a$ and~$b$ (and thus
lcp@314
   413
$c$) are well-typed.
lcp@104
   414
lcp@314
   415
Figure~\ref{ctt-N} presents the rules for~$N$, the type of natural numbers.
lcp@314
   416
They include \tdx{zero_ne_succ}, which asserts $0\not=n+1$.  This is
lcp@314
   417
the fourth Peano axiom and cannot be derived without universes \cite[page
lcp@314
   418
91]{martinlof84}.  
lcp@314
   419
lcp@314
   420
The constant \cdx{rec} constructs proof terms when mathematical
lcp@314
   421
induction, rule~\tdx{NE}, is applied.  It can also express primitive
lcp@314
   422
recursion.  Since \cdx{rec} can be applied to higher-order functions,
lcp@314
   423
it can even express Ackermann's function, which is not primitive recursive
lcp@314
   424
\cite[page~104]{thompson91}.
lcp@314
   425
lcp@314
   426
Figure~\ref{ctt-prod} shows the rules for general product types, which
lcp@314
   427
include function types as a special case.  The rules correspond to the
lcp@314
   428
predicate calculus rules for universal quantifiers and implication.  They
lcp@314
   429
also permit reasoning about functions, with the rules of a typed
lcp@314
   430
$\lambda$-calculus.
lcp@314
   431
lcp@314
   432
Figure~\ref{ctt-sum} shows the rules for general sum types, which
lcp@314
   433
include binary product types as a special case.  The rules correspond to the
lcp@314
   434
predicate calculus rules for existential quantifiers and conjunction.  They
lcp@314
   435
also permit reasoning about ordered pairs, with the projections
lcp@314
   436
\cdx{fst} and~\cdx{snd}.
lcp@314
   437
lcp@314
   438
Figure~\ref{ctt-plus} shows the rules for binary sum types.  They
lcp@314
   439
correspond to the predicate calculus rules for disjunction.  They also
lcp@314
   440
permit reasoning about disjoint sums, with the injections \cdx{inl}
lcp@314
   441
and~\cdx{inr} and case analysis operator~\cdx{when}.
lcp@314
   442
lcp@314
   443
Figure~\ref{ctt-ft} shows the rules for the empty and unit types, $F$
lcp@314
   444
and~$T$.  They correspond to the predicate calculus rules for absurdity and
lcp@314
   445
truth.
lcp@314
   446
lcp@314
   447
Figure~\ref{ctt-eq} shows the rules for equality types.  If $a=b\in A$ is
lcp@314
   448
provable then \cdx{eq} is a canonical element of the type $Eq(A,a,b)$,
lcp@314
   449
and vice versa.  These rules define extensional equality; the most recent
lcp@314
   450
versions of Type Theory use intensional equality~\cite{nordstrom90}.
lcp@314
   451
lcp@314
   452
Figure~\ref{ctt-derived} presents the derived rules.  The rule
lcp@314
   453
\tdx{subst_prodE} is derived from {\tt prodE}, and is easier to use
lcp@314
   454
in backwards proof.  The rules \tdx{SumE_fst} and \tdx{SumE_snd}
lcp@314
   455
express the typing of~\cdx{fst} and~\cdx{snd}; together, they are
lcp@314
   456
roughly equivalent to~{\tt SumE} with the advantage of creating no
lcp@314
   457
parameters.  Section~\ref{ctt-choice} below demonstrates these rules in a
lcp@314
   458
proof of the Axiom of Choice.
lcp@104
   459
lcp@104
   460
All the rules are given in $\eta$-expanded form.  For instance, every
lcp@104
   461
occurrence of $\lambda u\,v.b(u,v)$ could be abbreviated to~$b$ in the
lcp@314
   462
rules for~$N$.  The expanded form permits Isabelle to preserve bound
lcp@314
   463
variable names during backward proof.  Names of bound variables in the
lcp@314
   464
conclusion (here, $u$ and~$v$) are matched with corresponding bound
lcp@314
   465
variables in the premises.
lcp@104
   466
lcp@104
   467
lcp@104
   468
\section{Rule lists}
lcp@104
   469
The Type Theory tactics provide rewriting, type inference, and logical
lcp@104
   470
reasoning.  Many proof procedures work by repeatedly resolving certain Type
lcp@104
   471
Theory rules against a proof state.  {\CTT} defines lists --- each with
lcp@104
   472
type
lcp@104
   473
\hbox{\tt thm list} --- of related rules. 
lcp@314
   474
\begin{ttdescription}
lcp@104
   475
\item[\ttindexbold{form_rls}] 
lcp@104
   476
contains formation rules for the types $N$, $\Pi$, $\Sigma$, $+$, $Eq$,
lcp@104
   477
$F$, and $T$.
lcp@104
   478
lcp@104
   479
\item[\ttindexbold{formL_rls}] 
lcp@104
   480
contains long formation rules for $\Pi$, $\Sigma$, $+$, and $Eq$.  (For
lcp@314
   481
other types use \tdx{refl_type}.)
lcp@104
   482
lcp@104
   483
\item[\ttindexbold{intr_rls}] 
lcp@104
   484
contains introduction rules for the types $N$, $\Pi$, $\Sigma$, $+$, and
lcp@104
   485
$T$.
lcp@104
   486
lcp@104
   487
\item[\ttindexbold{intrL_rls}] 
lcp@104
   488
contains long introduction rules for $N$, $\Pi$, $\Sigma$, and $+$.  (For
lcp@314
   489
$T$ use \tdx{refl_elem}.)
lcp@104
   490
lcp@104
   491
\item[\ttindexbold{elim_rls}] 
lcp@104
   492
contains elimination rules for the types $N$, $\Pi$, $\Sigma$, $+$, and
lcp@104
   493
$F$.  The rules for $Eq$ and $T$ are omitted because they involve no
lcp@104
   494
eliminator.
lcp@104
   495
lcp@104
   496
\item[\ttindexbold{elimL_rls}] 
lcp@104
   497
contains long elimination rules for $N$, $\Pi$, $\Sigma$, $+$, and $F$.
lcp@104
   498
lcp@104
   499
\item[\ttindexbold{comp_rls}] 
lcp@104
   500
contains computation rules for the types $N$, $\Pi$, $\Sigma$, and $+$.
lcp@104
   501
Those for $Eq$ and $T$ involve no eliminator.
lcp@104
   502
lcp@104
   503
\item[\ttindexbold{basic_defs}] 
lcp@314
   504
contains the definitions of {\tt fst} and {\tt snd}.  
lcp@314
   505
\end{ttdescription}
lcp@104
   506
lcp@104
   507
lcp@104
   508
\section{Tactics for subgoal reordering}
lcp@104
   509
\begin{ttbox}
lcp@104
   510
test_assume_tac : int -> tactic
lcp@104
   511
typechk_tac     : thm list -> tactic
lcp@104
   512
equal_tac       : thm list -> tactic
lcp@104
   513
intr_tac        : thm list -> tactic
lcp@104
   514
\end{ttbox}
lcp@104
   515
Blind application of {\CTT} rules seldom leads to a proof.  The elimination
lcp@104
   516
rules, especially, create subgoals containing new unknowns.  These subgoals
lcp@333
   517
unify with anything, creating a huge search space.  The standard tactic
lcp@314
   518
\ttindex{filt_resolve_tac} 
lcp@314
   519
(see \iflabelundefined{filt_resolve_tac}{the {\em Reference Manual\/}}%
lcp@314
   520
        {\S\ref{filt_resolve_tac}})
lcp@314
   521
%
lcp@333
   522
fails for goals that are too flexible; so does the {\CTT} tactic {\tt
lcp@314
   523
  test_assume_tac}.  Used with the tactical \ttindex{REPEAT_FIRST} they
lcp@314
   524
achieve a simple kind of subgoal reordering: the less flexible subgoals are
lcp@314
   525
attempted first.  Do some single step proofs, or study the examples below,
lcp@314
   526
to see why this is necessary.
lcp@314
   527
\begin{ttdescription}
lcp@104
   528
\item[\ttindexbold{test_assume_tac} $i$] 
lcp@314
   529
uses {\tt assume_tac} to solve the subgoal by assumption, but only if
lcp@104
   530
subgoal~$i$ has the form $a\in A$ and the head of $a$ is not an unknown.
lcp@104
   531
Otherwise, it fails.
lcp@104
   532
lcp@104
   533
\item[\ttindexbold{typechk_tac} $thms$] 
lcp@104
   534
uses $thms$ with formation, introduction, and elimination rules to check
lcp@104
   535
the typing of constructions.  It is designed to solve goals of the form
lcp@975
   536
$a\in \Var{A}$, where $a$ is rigid and $\Var{A}$ is flexible; thus it
lcp@975
   537
performs type inference.  The tactic can also solve goals of
lcp@104
   538
the form $A\;\rm type$.
lcp@104
   539
lcp@104
   540
\item[\ttindexbold{equal_tac} $thms$]
lcp@104
   541
uses $thms$ with the long introduction and elimination rules to solve goals
lcp@104
   542
of the form $a=b\in A$, where $a$ is rigid.  It is intended for deriving
lcp@104
   543
the long rules for defined constants such as the arithmetic operators.  The
lcp@104
   544
tactic can also perform type checking.
lcp@104
   545
lcp@104
   546
\item[\ttindexbold{intr_tac} $thms$]
lcp@104
   547
uses $thms$ with the introduction rules to break down a type.  It is
lcp@104
   548
designed for goals like $\Var{a}\in A$ where $\Var{a}$ is flexible and $A$
lcp@104
   549
rigid.  These typically arise when trying to prove a proposition~$A$,
lcp@104
   550
expressed as a type.
lcp@314
   551
\end{ttdescription}
lcp@104
   552
lcp@104
   553
lcp@104
   554
lcp@104
   555
\section{Rewriting tactics}
lcp@104
   556
\begin{ttbox}
lcp@104
   557
rew_tac     : thm list -> tactic
lcp@104
   558
hyp_rew_tac : thm list -> tactic
lcp@104
   559
\end{ttbox}
lcp@104
   560
Object-level simplification is accomplished through proof, using the {\tt
lcp@314
   561
  CTT} equality rules and the built-in rewriting functor
lcp@314
   562
{\tt TSimpFun}.%
lcp@314
   563
\footnote{This should not be confused with Isabelle's main simplifier; {\tt
lcp@314
   564
    TSimpFun} is only useful for {\CTT} and similar logics with type
lcp@314
   565
  inference rules.  At present it is undocumented.} 
lcp@314
   566
%
lcp@314
   567
The rewrites include the computation rules and other equations.  The long
lcp@314
   568
versions of the other rules permit rewriting of subterms and subtypes.
lcp@314
   569
Also used are transitivity and the extra judgement form \cdx{Reduce}.
lcp@104
   570
Meta-level simplification handles only definitional equality.
lcp@314
   571
\begin{ttdescription}
lcp@104
   572
\item[\ttindexbold{rew_tac} $thms$]
lcp@104
   573
applies $thms$ and the computation rules as left-to-right rewrites.  It
lcp@104
   574
solves the goal $a=b\in A$ by rewriting $a$ to $b$.  If $b$ is an unknown
lcp@104
   575
then it is assigned the rewritten form of~$a$.  All subgoals are rewritten.
lcp@104
   576
lcp@104
   577
\item[\ttindexbold{hyp_rew_tac} $thms$]
lcp@104
   578
is like {\tt rew_tac}, but includes as rewrites any equations present in
lcp@104
   579
the assumptions.
lcp@314
   580
\end{ttdescription}
lcp@104
   581
lcp@104
   582
lcp@104
   583
\section{Tactics for logical reasoning}
lcp@104
   584
Interpreting propositions as types lets {\CTT} express statements of
lcp@104
   585
intuitionistic logic.  However, Constructive Type Theory is not just
lcp@314
   586
another syntax for first-order logic.  There are fundamental differences.
lcp@104
   587
lcp@314
   588
\index{assumptions!in {\CTT}}
lcp@314
   589
Can assumptions be deleted after use?  Not every occurrence of a type
lcp@314
   590
represents a proposition, and Type Theory assumptions declare variables.
lcp@104
   591
In first-order logic, $\disj$-elimination with the assumption $P\disj Q$
lcp@104
   592
creates one subgoal assuming $P$ and another assuming $Q$, and $P\disj Q$
lcp@314
   593
can be deleted safely.  In Type Theory, $+$-elimination with the assumption
lcp@314
   594
$z\in A+B$ creates one subgoal assuming $x\in A$ and another assuming $y\in
lcp@314
   595
B$ (for arbitrary $x$ and $y$).  Deleting $z\in A+B$ when other assumptions
lcp@314
   596
refer to $z$ may render the subgoal unprovable: arguably,
lcp@314
   597
meaningless.
lcp@314
   598
lcp@314
   599
Isabelle provides several tactics for predicate calculus reasoning in \CTT:
lcp@104
   600
\begin{ttbox}
lcp@104
   601
mp_tac       : int -> tactic
lcp@104
   602
add_mp_tac   : int -> tactic
lcp@104
   603
safestep_tac : thm list -> int -> tactic
lcp@104
   604
safe_tac     : thm list -> int -> tactic
lcp@104
   605
step_tac     : thm list -> int -> tactic
lcp@104
   606
pc_tac       : thm list -> int -> tactic
lcp@104
   607
\end{ttbox}
lcp@104
   608
These are loosely based on the intuitionistic proof procedures
lcp@314
   609
of~\thydx{FOL}.  For the reasons discussed above, a rule that is safe for
lcp@104
   610
propositional reasoning may be unsafe for type checking; thus, some of the
lcp@314
   611
`safe' tactics are misnamed.
lcp@314
   612
\begin{ttdescription}
lcp@104
   613
\item[\ttindexbold{mp_tac} $i$] 
lcp@104
   614
searches in subgoal~$i$ for assumptions of the form $f\in\Pi(A,B)$ and
lcp@104
   615
$a\in A$, where~$A$ may be found by unification.  It replaces
lcp@104
   616
$f\in\Pi(A,B)$ by $z\in B(a)$, where~$z$ is a new parameter.  The tactic
lcp@104
   617
can produce multiple outcomes for each suitable pair of assumptions.  In
lcp@104
   618
short, {\tt mp_tac} performs Modus Ponens among the assumptions.
lcp@104
   619
lcp@104
   620
\item[\ttindexbold{add_mp_tac} $i$]
lcp@314
   621
is like {\tt mp_tac}~$i$ but retains the assumption $f\in\Pi(A,B)$.  It
lcp@314
   622
avoids information loss but obviously loops if repeated.
lcp@104
   623
lcp@104
   624
\item[\ttindexbold{safestep_tac} $thms$ $i$]
lcp@104
   625
attacks subgoal~$i$ using formation rules and certain other `safe' rules
lcp@333
   626
(\tdx{FE}, \tdx{ProdI}, \tdx{SumE}, \tdx{PlusE}), calling
lcp@104
   627
{\tt mp_tac} when appropriate.  It also uses~$thms$,
lcp@104
   628
which are typically premises of the rule being derived.
lcp@104
   629
lcp@314
   630
\item[\ttindexbold{safe_tac} $thms$ $i$] attempts to solve subgoal~$i$ by
lcp@314
   631
  means of backtracking, using {\tt safestep_tac}.
lcp@104
   632
lcp@104
   633
\item[\ttindexbold{step_tac} $thms$ $i$]
lcp@104
   634
tries to reduce subgoal~$i$ using {\tt safestep_tac}, then tries unsafe
lcp@104
   635
rules.  It may produce multiple outcomes.
lcp@104
   636
lcp@104
   637
\item[\ttindexbold{pc_tac} $thms$ $i$]
lcp@104
   638
tries to solve subgoal~$i$ by backtracking, using {\tt step_tac}.
lcp@314
   639
\end{ttdescription}
lcp@104
   640
lcp@104
   641
lcp@104
   642
lcp@104
   643
\begin{figure} 
lcp@314
   644
\index{#+@{\tt\#+} symbol}
lcp@314
   645
\index{*"- symbol}
lcp@314
   646
\index{*"|"-"| symbol}
lcp@314
   647
\index{#*@{\tt\#*} symbol}
lcp@314
   648
\index{*div symbol}
lcp@314
   649
\index{*mod symbol}
lcp@314
   650
\begin{constants}
lcp@314
   651
  \it symbol  & \it meta-type & \it priority & \it description \\ 
lcp@314
   652
  \tt \#*       & $[i,i]\To i$  &  Left 70      & multiplication \\
lcp@314
   653
  \tt div       & $[i,i]\To i$  &  Left 70      & division\\
lcp@314
   654
  \tt mod       & $[i,i]\To i$  &  Left 70      & modulus\\
lcp@314
   655
  \tt \#+       & $[i,i]\To i$  &  Left 65      & addition\\
lcp@314
   656
  \tt -         & $[i,i]\To i$  &  Left 65      & subtraction\\
lcp@314
   657
  \verb'|-|'    & $[i,i]\To i$  &  Left 65      & absolute difference
lcp@314
   658
\end{constants}
lcp@104
   659
lcp@104
   660
\begin{ttbox}
lcp@314
   661
\tdx{add_def}           a#+b  == rec(a, b, \%u v.succ(v))  
lcp@314
   662
\tdx{diff_def}          a-b   == rec(b, a, \%u v.rec(v, 0, \%x y.x))  
lcp@314
   663
\tdx{absdiff_def}       a|-|b == (a-b) #+ (b-a)  
lcp@314
   664
\tdx{mult_def}          a#*b  == rec(a, 0, \%u v. b #+ v)  
lcp@104
   665
lcp@314
   666
\tdx{mod_def}           a mod b ==
lcp@314
   667
                  rec(a, 0, \%u v. rec(succ(v) |-| b, 0, \%x y.succ(v)))
lcp@314
   668
lcp@314
   669
\tdx{div_def}           a div b ==
lcp@314
   670
                  rec(a, 0, \%u v. rec(succ(u) mod b, succ(v), \%x y.v))
lcp@314
   671
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
berghofe@3096
   699
\caption{The theory of arithmetic} \label{ctt_arith}
lcp@104
   700
\end{ttbox}
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
lcp@104
   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}
lcp@104
   730
This directory contains examples and experimental proofs in {\CTT}.
lcp@314
   731
\begin{ttdescription}
lcp@314
   732
\item[CTT/ex/typechk.ML]
lcp@104
   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}
lcp@284
   755
goal CTT.thy "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}
lcp@284
   817
goal CTT.thy "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}
lcp@104
   847
val prems = goal CTT.thy
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}
lcp@104
   998
val prems = goal CTT.thy
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
lcp@314
  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}
lcp@104
  1078
val prems = goal CTT.thy
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
lcp@104
  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}
lcp@104
  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}
lcp@104
  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|)}