src/HOL/Record.thy
author haftmann
Thu, 12 Aug 2010 17:56:43 +0200
changeset 38394 3142c1e21a0e
parent 36176 3fe7e97ccca8
child 38528 bbaaaf6f1cbe
permissions -rw-r--r--
moved Record.thy from session Plain to Main; avoid variable name acc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4870
cc36acb5b114 Extensible records with structural subtyping in HOL. See
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Record.thy
32763
ebfaf9e3c03a tuned header;
wenzelm
parents: 32756
diff changeset
     2
    Author:     Wolfgang Naraschewski, TU Muenchen
ebfaf9e3c03a tuned header;
wenzelm
parents: 32756
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
ebfaf9e3c03a tuned header;
wenzelm
parents: 32756
diff changeset
     4
    Author:     Norbert Schirmer, TU Muenchen
ebfaf9e3c03a tuned header;
wenzelm
parents: 32756
diff changeset
     5
    Author:     Thomas Sewell, NICTA
33595
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
     6
    Author:     Florian Haftmann, TU Muenchen
4870
cc36acb5b114 Extensible records with structural subtyping in HOL. See
wenzelm
parents:
diff changeset
     7
*)
cc36acb5b114 Extensible records with structural subtyping in HOL. See
wenzelm
parents:
diff changeset
     8
22817
9dfadec17cc4 added header;
wenzelm
parents: 22759
diff changeset
     9
header {* Extensible records with structural subtyping *}
9dfadec17cc4 added header;
wenzelm
parents: 22759
diff changeset
    10
15131
c69542757a4d New theory header syntax.
nipkow
parents: 14701
diff changeset
    11
theory Record
38394
3142c1e21a0e moved Record.thy from session Plain to Main; avoid variable name acc
haftmann
parents: 36176
diff changeset
    12
imports Plain Quickcheck
3142c1e21a0e moved Record.thy from session Plain to Main; avoid variable name acc
haftmann
parents: 36176
diff changeset
    13
uses ("Tools/typecopy.ML") ("Tools/record.ML") ("Tools/quickcheck_record.ML")
15131
c69542757a4d New theory header syntax.
nipkow
parents: 14701
diff changeset
    14
begin
4870
cc36acb5b114 Extensible records with structural subtyping in HOL. See
wenzelm
parents:
diff changeset
    15
33595
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    16
subsection {* Introduction *}
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    17
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    18
text {*
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    19
  Records are isomorphic to compound tuple types. To implement
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    20
  efficient records, we make this isomorphism explicit. Consider the
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    21
  record access/update simplification @{text "alpha (beta_update f
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    22
  rec) = alpha rec"} for distinct fields alpha and beta of some record
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    23
  rec with n fields. There are @{text "n ^ 2"} such theorems, which
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    24
  prohibits storage of all of them for large n. The rules can be
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    25
  proved on the fly by case decomposition and simplification in O(n)
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    26
  time. By creating O(n) isomorphic-tuple types while defining the
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    27
  record, however, we can prove the access/update simplification in
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    28
  @{text "O(log(n)^2)"} time.
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    29
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    30
  The O(n) cost of case decomposition is not because O(n) steps are
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    31
  taken, but rather because the resulting rule must contain O(n) new
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    32
  variables and an O(n) size concrete record construction. To sidestep
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    33
  this cost, we would like to avoid case decomposition in proving
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    34
  access/update theorems.
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    35
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    36
  Record types are defined as isomorphic to tuple types. For instance,
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    37
  a record type with fields @{text "'a"}, @{text "'b"}, @{text "'c"}
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    38
  and @{text "'d"} might be introduced as isomorphic to @{text "'a \<times>
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    39
  ('b \<times> ('c \<times> 'd))"}. If we balance the tuple tree to @{text "('a \<times>
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    40
  'b) \<times> ('c \<times> 'd)"} then accessors can be defined by converting to the
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    41
  underlying type then using O(log(n)) fst or snd operations.
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    42
  Updators can be defined similarly, if we introduce a @{text
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    43
  "fst_update"} and @{text "snd_update"} function. Furthermore, we can
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    44
  prove the access/update theorem in O(log(n)) steps by using simple
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    45
  rewrites on fst, snd, @{text "fst_update"} and @{text "snd_update"}.
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    46
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    47
  The catch is that, although O(log(n)) steps were taken, the
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    48
  underlying type we converted to is a tuple tree of size
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    49
  O(n). Processing this term type wastes performance. We avoid this
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    50
  for large n by taking each subtree of size K and defining a new type
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    51
  isomorphic to that tuple subtree. A record can now be defined as
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    52
  isomorphic to a tuple tree of these O(n/K) new types, or, if @{text
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    53
  "n > K*K"}, we can repeat the process, until the record can be
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    54
  defined in terms of a tuple tree of complexity less than the
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    55
  constant K.
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    56
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    57
  If we prove the access/update theorem on this type with the
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    58
  analagous steps to the tuple tree, we consume @{text "O(log(n)^2)"}
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    59
  time as the intermediate terms are @{text "O(log(n))"} in size and
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    60
  the types needed have size bounded by K.  To enable this analagous
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    61
  traversal, we define the functions seen below: @{text
34151
8d57ce46b3f7 prefer prefix "iso" over potentially misleading "is"; tuned
haftmann
parents: 33595
diff changeset
    62
  "iso_tuple_fst"}, @{text "iso_tuple_snd"}, @{text "iso_tuple_fst_update"}
8d57ce46b3f7 prefer prefix "iso" over potentially misleading "is"; tuned
haftmann
parents: 33595
diff changeset
    63
  and @{text "iso_tuple_snd_update"}. These functions generalise tuple
33595
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    64
  operations by taking a parameter that encapsulates a tuple
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    65
  isomorphism.  The rewrites needed on these functions now need an
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    66
  additional assumption which is that the isomorphism works.
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    67
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    68
  These rewrites are typically used in a structured way. They are here
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    69
  presented as the introduction rule @{text "isomorphic_tuple.intros"}
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    70
  rather than as a rewrite rule set. The introduction form is an
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    71
  optimisation, as net matching can be performed at one term location
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    72
  for each step rather than the simplifier searching the term for
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    73
  possible pattern matches. The rule set is used as it is viewed
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    74
  outside the locale, with the locale assumption (that the isomorphism
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    75
  is valid) left as a rule assumption. All rules are structured to aid
7264824baf66 substantial simplification restores code generation
haftmann
parents: 32799
diff changeset
    76
  net matching, using either a point-free form or an encapsulating