doc-src/TutorialI/Recdef/examples.thy
author nipkow
Tue, 25 Apr 2000 08:09:10 +0200
changeset 8771 026f37a86ea7
parent 8745 13b32661dde4
child 9541 d17c0b34d5c8
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
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     9
consts fib :: "nat \\<Rightarrow> nat";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    10
recdef fib "measure(\\<lambda>n. n)"
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
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    16
The definition of \isa{fib} is accompanied by a \bfindex{measure function}
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8745
diff changeset
    17
\isa{\isasymlambda{}n.$\;$n} which maps the argument of \isa{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
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    20
argument of each recursive call. In the case of \isa{fib} this is
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    21
obviously true because the measure function is the identity and
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    22
\isa{Suc(Suc~x)} is strictly greater than both \isa{x} and
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    23
\isa{Suc~x}.
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
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    29
consts sep :: "'a * 'a list \\<Rightarrow> 'a list";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    30
recdef sep "measure (\\<lambda>(a,xs). length xs)"
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.
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    38
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    39
Pattern matching need not be exhaustive:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    40
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    41
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    42
consts last :: "'a list \\<Rightarrow> 'a";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    43
recdef last "measure (\\<lambda>xs. length xs)"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    44
  "last [x]      = x"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    45
  "last (x#y#zs) = last (y#zs)";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    46
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    47
text{*
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    48
Overlapping patterns are disambiguated by taking the order of equations into
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    49
account, just as in functional programming:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    50
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    51
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    52
consts sep1 :: "'a * 'a list \\<Rightarrow> 'a list";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    53
recdef sep1 "measure (\\<lambda>(a,xs). length xs)"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    54
  "sep1(a, x#y#zs) = x # a # sep1(a,y#zs)"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    55
  "sep1(a, xs)     = xs";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    56
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    57
text{*\noindent
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    58
This defines exactly the same function as \isa{sep} above, i.e.\
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    59
\isa{sep1 = sep}.
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    60
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    61
\begin{warn}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    62
  \isacommand{recdef} only takes the first argument of a (curried)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    63
  recursive function into account. This means both the termination measure
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    64
  and pattern matching can only use that first argument. In general, you will
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    65
  therefore have to combine several arguments into a tuple. In case only one
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    66
  argument is relevant for termination, you can also rearrange the order of
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    67
  arguments as in the following definition:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    68
\end{warn}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    69
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    70
consts sep2 :: "'a list \\<Rightarrow> 'a \\<Rightarrow> 'a list";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    71
recdef sep2 "measure length"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    72
  "sep2 (x#y#zs) = (\\<lambda>a. x # a # sep2 zs a)"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    73
  "sep2 xs       = (\\<lambda>a. xs)";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    74
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    75
text{*
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    76
Because of its pattern-matching syntax, \isacommand{recdef} is also useful
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    77
for the definition of non-recursive functions:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    78
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    79
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    80
consts swap12 :: "'a list \\<Rightarrow> 'a list";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    81
recdef swap12 "{}"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    82
  "swap12 (x#y#zs) = y#x#zs"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    83
  "swap12 zs       = zs";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    84
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    85
text{*\noindent
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8745
diff changeset
    86
For non-recursive functions the termination measure degenerates to the empty
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    87
set \isa{\{\}}.
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    88
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    89
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    90
(*<*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    91
end
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    92
(*>*)