doc-src/Exercises/2003/a5/a5.thy
author wenzelm
Thu, 22 Apr 2004 11:01:34 +0200
changeset 14651 02b8f3bcf7fe
parent 14526 51dc6c7b1fd7
permissions -rw-r--r--
improved notation;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14526
mehta
parents:
diff changeset
     1
(*<*)
mehta
parents:
diff changeset
     2
theory a5 = Main:
mehta
parents:
diff changeset
     3
(*>*)
mehta
parents:
diff changeset
     4
mehta
parents:
diff changeset
     5
subsection {*BIGNAT - Specification and Verification*};
mehta
parents:
diff changeset
     6
mehta
parents:
diff changeset
     7
text{*
mehta
parents:
diff changeset
     8
Hardware platforms have a limit on the largest number they can represent. This is normally fixed by the bit lengths of registers and ALUs used. In order to be able to perform calculations that require arbitrarily large numbers, the provided arithmetic operations need to be extended in order for them to work on an abstract data type representing numbers of arbitrary size.
mehta
parents:
diff changeset
     9
mehta
parents:
diff changeset
    10
In this exercise we will build and verify an implementation for BIGNAT, an abstract data type representing natural numbers of arbitrary size.
mehta
parents:
diff changeset
    11
*}
mehta
parents:
diff changeset
    12
mehta
parents:
diff changeset
    13
text {*\subsubsection*{Representation}*}
mehta
parents:
diff changeset
    14
mehta
parents:
diff changeset
    15
text{*
mehta
parents:
diff changeset
    16
A BIGNAT is represented as a list of natural numbers in a range supported by the target machine. In our case, this will be all natural numbers in the range [0, BASE-1]. (Note: natural numbers in Isabelle are of arbitrary size)
mehta
parents:
diff changeset
    17
*}
mehta
parents:
diff changeset
    18
mehta
parents:
diff changeset
    19
types 
mehta
parents:
diff changeset
    20
  bigNat = "nat list"
mehta
parents:
diff changeset
    21
mehta
parents:
diff changeset
    22
text{*
mehta
parents:
diff changeset
    23
Define a function @{term "valid"} that takes a value for BASE, and checks if the given BIGNAT is valid.
mehta
parents:
diff changeset
    24
*}
mehta
parents:
diff changeset
    25
mehta
parents:
diff changeset
    26
consts valid :: "nat \<Rightarrow> bigNat \<Rightarrow> bool"
mehta
parents:
diff changeset
    27
mehta
parents:
diff changeset
    28
text{*
mehta
parents:
diff changeset
    29
Define a function @{term "val"} that takes a BIGNAT and its corresponding BASE, and returns the natural number represented by it. *}
mehta
parents:
diff changeset
    30
mehta
parents:
diff changeset
    31
consts val :: "nat \<Rightarrow> bigNat \<Rightarrow> nat"
mehta
parents:
diff changeset
    32
mehta
parents:
diff changeset
    33
text {*\subsubsection*{Addition}*}
mehta
parents:
diff changeset
    34
mehta
parents:
diff changeset
    35
text{*
mehta
parents:
diff changeset
    36
Define a function @{term "add"} that adds two BIGNATs with the same value for BASE. Make sure that your algorithm preserves the validity of the BIGNAT representation.
mehta
parents:
diff changeset
    37
*}
mehta
parents:
diff changeset
    38
mehta
parents:
diff changeset
    39
consts add :: "nat \<Rightarrow> bigNat \<Rightarrow> bigNat \<Rightarrow> bigNat"
mehta
parents:
diff changeset
    40
mehta
parents:
diff changeset
    41
text{*
mehta
parents:
diff changeset
    42
Using @{term "val"}, verify formally that your function @{term "add"} computes the sum of two BIGNATs correctly.*}
mehta
parents:
diff changeset
    43
mehta
parents:
diff changeset
    44
text{*
mehta
parents:
diff changeset
    45
Using @{term "valid"}, verify formally that your function @{term "add"} preserves the validity of the BIGNAT representation.*}
mehta
parents:
diff changeset
    46
mehta
parents:
diff changeset
    47
mehta
parents:
diff changeset
    48
text {*
mehta
parents:
diff changeset
    49
\subsubsection*{Multiplication}*}
mehta
parents:
diff changeset
    50
mehta
parents:
diff changeset
    51
text{*
mehta
parents:
diff changeset
    52
Define a function @{term "mult"} that multiplies two BIGNATs with the same value for BASE. You may use @{term "add"}, but not so often as to make the solution trivial. Make sure that your algorithm preserves the validity of the BIGNAT representation.
mehta
parents:
diff changeset
    53
*}
mehta
parents:
diff changeset
    54
consts mult :: "nat \<Rightarrow> bigNat \<Rightarrow> bigNat \<Rightarrow> bigNat"
mehta
parents:
diff changeset
    55
text{*
mehta
parents:
diff changeset
    56
Using @{term "val"}, verify formally that your function @{term "mult"} computes the product of two BIGNATs correctly.*}
mehta
parents:
diff changeset
    57
mehta
parents:
diff changeset
    58
text{*
mehta
parents:
diff changeset
    59
Using @{term "valid"}, verify formally that your function @{term "mult"} preserves the validity of the BIGNAT representation.*}
mehta
parents:
diff changeset
    60
mehta
parents:
diff changeset
    61
mehta
parents:
diff changeset
    62
(*<*) end (*>*)
mehta
parents:
diff changeset
    63
mehta
parents:
diff changeset
    64
mehta
parents:
diff changeset
    65
mehta
parents:
diff changeset
    66