doc-src/TutorialI/Recdef/examples.thy
author nipkow
Wed, 13 Dec 2000 09:39:53 +0100
changeset 10654 458068404143
parent 10362 c6b197ccf1f1
child 11160 e0ab13bec5c8
permissions -rw-r--r--
*** empty log message ***
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     1
(*<*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     2
theory examples = Main:;
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     3
(*>*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     4
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     5
text{*
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     6
Here is a simple example, the Fibonacci function:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     7
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     8
9933
9feb1e0c4cb3 *** empty log message ***
nipkow
parents: 9792
diff changeset
     9
consts fib :: "nat \<Rightarrow> nat";
9feb1e0c4cb3 *** empty log message ***
nipkow
parents: 9792
diff changeset
    10
recdef fib "measure(\<lambda>n. n)"
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    11
  "fib 0 = 0"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    12
  "fib 1 = 1"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    13
  "fib (Suc(Suc x)) = fib x + fib (Suc x)";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    14
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    15
text{*\noindent
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    16
The definition of @{term"fib"} is accompanied by a \bfindex{measure function}
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    17
@{term"%n. n"} which maps the argument of @{term"fib"} to a
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    18
natural number. The requirement is that in each equation the measure of the
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    19
argument on the left-hand side is strictly greater than the measure of the
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    20
argument of each recursive call. In the case of @{term"fib"} this is
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    21
obviously true because the measure function is the identity and
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    22
@{term"Suc(Suc x)"} is strictly greater than both @{term"x"} and
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 8771
diff changeset
    23
@{term"Suc x"}.
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    24
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    25
Slightly more interesting is the insertion of a fixed element
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    26
between any two elements of a list:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    27
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    28
9933
9feb1e0c4cb3 *** empty log message ***
nipkow
parents: 9792
diff changeset
    29
consts sep :: "'a \<times> 'a list \<Rightarrow> 'a list";
9feb1e0c4cb3 *** empty log message ***
nipkow
parents: 9792
diff changeset
    30
recdef sep "measure (\<lambda>(a,xs). length xs)"
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    31
  "sep(a, [])     = []"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    32
  "sep(a, [x])    = [x]"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    33
  "sep(a, x#y#zs) = x # a # sep(a,y#zs)";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    34
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    35
text{*\noindent
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    36
This time the measure is the length of the list, which decreases with the
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    37
recursive call; the first component of the argument tuple is irrelevant.
10654
458068404143 *** empty log message ***
nipkow
parents: 10362
diff changeset
    38
The details of tupled $\lambda$-abstractions @{text"\<lambda>(x\<^sub>1,\<dots>,x\<^sub>n)"} are
458068404143 *** empty log message ***
nipkow
parents: 10362
diff changeset
    39
explained in \S\ref{sec:products}, but for now your intuition is all you need.
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    40
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    41
Pattern matching need not be exhaustive:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    42
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    43
9933
9feb1e0c4cb3 *** empty log message ***
nipkow
parents: 9792
diff changeset
    44
consts last :: "'a list \<Rightarrow> 'a";
9feb1e0c4cb3 *** empty log message ***
nipkow
parents: 9792
diff changeset
    45
recdef last "measure (\<lambda>xs. length xs)"
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    46
  "last [x]      = x"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    47
  "last (x#y#zs) = last (y#zs)";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    48
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    49
text{*
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    50
Overlapping patterns are disambiguated by taking the order of equations into
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    51
account, just as in functional programming:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    52
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    53
9933
9feb1e0c4cb3 *** empty log message ***
nipkow
parents: 9792
diff changeset
    54
consts sep1 :: "'a \<times> 'a list \<Rightarrow> 'a list";
9feb1e0c4cb3 *** empty log message ***
nipkow
parents: 9792
diff changeset
    55
recdef sep1 "measure (\<lambda>(a,xs). length xs)"
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    56
  "sep1(a, x#y#zs) = x # a # sep1(a,y#zs)"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    57
  "sep1(a, xs)     = xs";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    58
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    59
text{*\noindent
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    60
This defines exactly the same function as @{term"sep"} above, i.e.\
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    61
@{prop"sep1 = sep"}.
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    62
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    63
\begin{warn}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    64
  \isacommand{recdef} only takes the first argument of a (curried)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    65
  recursive function into account. This means both the termination measure
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    66
  and pattern matching can only use that first argument. In general, you will
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    67
  therefore have to combine several arguments into a tuple. In case only one
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    68
  argument is relevant for termination, you can also rearrange the order of
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    69
  arguments as in the following definition:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    70
\end{warn}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    71
*}
9933
9feb1e0c4cb3 *** empty log message ***
nipkow
parents: 9792
diff changeset
    72
consts sep2 :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a list";
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    73
recdef sep2 "measure length"
10362
c6b197ccf1f1 *** empty log message ***
nipkow
parents: 9933
diff changeset
    74
  "sep2 (x#y#zs) = (\<lambda>a. x # a # sep2 (y#zs) a)"
9933
9feb1e0c4cb3 *** empty log message ***
nipkow
parents: 9792
diff changeset
    75
  "sep2 xs       = (\<lambda>a. xs)";
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    76
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    77
text{*
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    78
Because of its pattern-matching syntax, \isacommand{recdef} is also useful
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    79
for the definition of non-recursive functions:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    80
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    81
9933
9feb1e0c4cb3 *** empty log message ***
nipkow
parents: 9792
diff changeset
    82
consts swap12 :: "'a list \<Rightarrow> 'a list";
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    83
recdef swap12 "{}"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    84
  "swap12 (x#y#zs) = y#x#zs"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    85
  "swap12 zs       = zs";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    86
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    87
text{*\noindent
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8745
diff changeset
    88
For non-recursive functions the termination measure degenerates to the empty
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    89
set @{term"{}"}.
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    90
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    91
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    92
(*<*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    93
end
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    94
(*>*)