src/HOL/Library/IArray.thy
author blanchet
Thu, 11 Sep 2014 19:32:36 +0200
changeset 58310 91ea607a34d8
parent 58269 ad644790cbbb
child 58881 b9556a055632
permissions -rw-r--r--
updated news
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50138
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
     1
header "Immutable Arrays with Code Generation"
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
     2
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
     3
theory IArray
51094
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
     4
imports Main
50138
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
     5
begin
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
     6
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
     7
text{* Immutable arrays are lists wrapped up in an additional constructor.
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
     8
There are no update operations. Hence code generation can safely implement
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
     9
this type by efficient target language arrays.  Currently only SML is
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
    10
provided. Should be extended to other target languages and more operations.
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
    11
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
    12
Note that arrays cannot be printed directly but only by turning them into
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
    13
lists first. Arrays could be converted back into lists for printing if they
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
    14
were wrapped up in an additional constructor. *}
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
    15
58310
91ea607a34d8 updated news
blanchet
parents: 58269
diff changeset
    16
datatype 'a iarray = IArray "'a list"
50138
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
    17
51094
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    18
primrec list_of :: "'a iarray \<Rightarrow> 'a list" where
50138
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
    19
"list_of (IArray xs) = xs"
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
    20
hide_const (open) list_of
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
    21
51094
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    22
definition of_fun :: "(nat \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a iarray" where
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    23
[simp]: "of_fun f n = IArray (map f [0..<n])"
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    24
hide_const (open) of_fun
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    25
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    26
definition sub :: "'a iarray \<Rightarrow> nat \<Rightarrow> 'a" (infixl "!!" 100) where
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    27
[simp]: "as !! n = IArray.list_of as ! n"
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    28
hide_const (open) sub
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    29
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    30
definition length :: "'a iarray \<Rightarrow> nat" where
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    31
[simp]: "length as = List.length (IArray.list_of as)"
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    32
hide_const (open) length
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    33
54459
f43ae1afd08a added functions all and exists to IArray
nipkow
parents: 52435
diff changeset
    34
fun all :: "('a \<Rightarrow> bool) \<Rightarrow> 'a iarray \<Rightarrow> bool" where
f43ae1afd08a added functions all and exists to IArray
nipkow
parents: 52435
diff changeset
    35
"all p (IArray as) = (ALL a : set as. p a)"
f43ae1afd08a added functions all and exists to IArray
nipkow
parents: 52435
diff changeset
    36
hide_const (open) all
f43ae1afd08a added functions all and exists to IArray
nipkow
parents: 52435
diff changeset
    37
f43ae1afd08a added functions all and exists to IArray
nipkow
parents: 52435
diff changeset
    38
fun exists :: "('a \<Rightarrow> bool) \<Rightarrow> 'a iarray \<Rightarrow> bool" where
f43ae1afd08a added functions all and exists to IArray
nipkow
parents: 52435
diff changeset
    39
"exists p (IArray as) = (EX a : set as. p a)"
f43ae1afd08a added functions all and exists to IArray
nipkow
parents: 52435
diff changeset
    40
hide_const (open) exists
f43ae1afd08a added functions all and exists to IArray
nipkow
parents: 52435
diff changeset
    41
51094
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    42
lemma list_of_code [code]:
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    43
"IArray.list_of as = map (\<lambda>n. as !! n) [0 ..< IArray.length as]"
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    44
by (cases as) (simp add: map_nth)
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    45
50138
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
    46
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
    47
subsection "Code Generation"
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
    48
51094
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    49
code_reserved SML Vector
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    50
52435
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51161
diff changeset
    51
code_printing
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51161
diff changeset
    52
  type_constructor iarray \<rightharpoonup> (SML) "_ Vector.vector"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51161
diff changeset
    53
| constant IArray \<rightharpoonup> (SML) "Vector.fromList"
54459
f43ae1afd08a added functions all and exists to IArray
nipkow
parents: 52435
diff changeset
    54
| constant IArray.all \<rightharpoonup> (SML) "Vector.all"
f43ae1afd08a added functions all and exists to IArray
nipkow
parents: 52435
diff changeset
    55
| constant IArray.exists \<rightharpoonup> (SML) "Vector.exists"
51094
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    56
51161
6ed12ae3b3e1 attempt to re-establish conventions which theories are loaded into the grand unified library theory;
haftmann
parents: 51143
diff changeset
    57
lemma [code]:
58269
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    58
  "size (as :: 'a iarray) = Suc (length (IArray.list_of as))"
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    59
  by (cases as) simp
51161
6ed12ae3b3e1 attempt to re-establish conventions which theories are loaded into the grand unified library theory;
haftmann
parents: 51143
diff changeset
    60
6ed12ae3b3e1 attempt to re-establish conventions which theories are loaded into the grand unified library theory;
haftmann
parents: 51143
diff changeset
    61
lemma [code]:
58269
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    62
  "size_iarray f as = Suc (size_list f (IArray.list_of as))"
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    63
  by (cases as) simp
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    64
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    65
lemma [code]:
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    66
  "rec_iarray f as = f (IArray.list_of as)"
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    67
  by (cases as) simp
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    68
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    69
lemma [code]:
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    70
  "case_iarray f as = f (IArray.list_of as)"
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    71
  by (cases as) simp
51161
6ed12ae3b3e1 attempt to re-establish conventions which theories are loaded into the grand unified library theory;
haftmann
parents: 51143
diff changeset
    72
6ed12ae3b3e1 attempt to re-establish conventions which theories are loaded into the grand unified library theory;
haftmann
parents: 51143
diff changeset
    73
lemma [code]:
58269
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    74
  "set_iarray as = set (IArray.list_of as)"
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    75
  by (case_tac as) auto
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    76
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    77
lemma [code]:
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    78
  "map_iarray f as = IArray (map f (IArray.list_of as))"
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    79
  by (case_tac as) auto
51161
6ed12ae3b3e1 attempt to re-establish conventions which theories are loaded into the grand unified library theory;
haftmann
parents: 51143
diff changeset
    80
6ed12ae3b3e1 attempt to re-establish conventions which theories are loaded into the grand unified library theory;
haftmann
parents: 51143
diff changeset
    81
lemma [code]:
58269
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    82
  "rel_iarray r as bs = list_all2 r (IArray.list_of as) (IArray.list_of bs)"
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    83
  by (case_tac as) (case_tac bs, auto)
51161
6ed12ae3b3e1 attempt to re-establish conventions which theories are loaded into the grand unified library theory;
haftmann
parents: 51143
diff changeset
    84
6ed12ae3b3e1 attempt to re-establish conventions which theories are loaded into the grand unified library theory;
haftmann
parents: 51143
diff changeset
    85
lemma [code]:
58269
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    86
  "HOL.equal as bs \<longleftrightarrow> HOL.equal (IArray.list_of as) (IArray.list_of bs)"
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    87
  by (cases as, cases bs) (simp add: equal)
51161
6ed12ae3b3e1 attempt to re-establish conventions which theories are loaded into the grand unified library theory;
haftmann
parents: 51143
diff changeset
    88
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51094
diff changeset
    89
primrec tabulate :: "integer \<times> (integer \<Rightarrow> 'a) \<Rightarrow> 'a iarray" where
58269
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    90
  "tabulate (n, f) = IArray (map (f \<circ> integer_of_nat) [0..<nat_of_integer n])"
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    91
51094
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    92
hide_const (open) tabulate
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    93
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    94
lemma [code]:
58269
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    95
  "IArray.of_fun f n = IArray.tabulate (integer_of_nat n, f \<circ> nat_of_integer)"
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
    96
  by simp
51094
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
    97
52435
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51161
diff changeset
    98
code_printing
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51161
diff changeset
    99
  constant IArray.tabulate \<rightharpoonup> (SML) "Vector.tabulate"
51094
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
   100
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51094
diff changeset
   101
primrec sub' :: "'a iarray \<times> integer \<Rightarrow> 'a" where
58269
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
   102
  [code del]: "sub' (as, n) = IArray.list_of as ! nat_of_integer n"
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
   103
51094
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
   104
hide_const (open) sub'
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
   105
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
   106
lemma [code]:
57117
a2eb1bdb9270 terminating code equations
haftmann
parents: 56846
diff changeset
   107
  "IArray.sub' (IArray as, n) = as ! nat_of_integer n"
a2eb1bdb9270 terminating code equations
haftmann
parents: 56846
diff changeset
   108
  by simp
a2eb1bdb9270 terminating code equations
haftmann
parents: 56846
diff changeset
   109
a2eb1bdb9270 terminating code equations
haftmann
parents: 56846
diff changeset
   110
lemma [code]:
58269
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
   111
  "as !! n = IArray.sub' (as, integer_of_nat n)"
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
   112
  by simp
51094
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
   113
52435
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51161
diff changeset
   114
code_printing
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51161
diff changeset
   115
  constant IArray.sub' \<rightharpoonup> (SML) "Vector.sub"
51094
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
   116
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51094
diff changeset
   117
definition length' :: "'a iarray \<Rightarrow> integer" where
58269
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
   118
  [code del, simp]: "length' as = integer_of_nat (List.length (IArray.list_of as))"
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
   119
51094
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
   120
hide_const (open) length'
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
   121
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
   122
lemma [code]:
57117
a2eb1bdb9270 terminating code equations
haftmann
parents: 56846
diff changeset
   123
  "IArray.length' (IArray as) = integer_of_nat (List.length as)" 
a2eb1bdb9270 terminating code equations
haftmann
parents: 56846
diff changeset
   124
  by simp
a2eb1bdb9270 terminating code equations
haftmann
parents: 56846
diff changeset
   125
a2eb1bdb9270 terminating code equations
haftmann
parents: 56846
diff changeset
   126
lemma [code]:
58269
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
   127
  "IArray.length as = nat_of_integer (IArray.length' as)"
ad644790cbbb tuned IArray code generator w.r.t. map rel set
blanchet
parents: 58266
diff changeset
   128
  by simp
51094
84b03c49c223 IArray ignorant of particular representation of nat
haftmann
parents: 50138
diff changeset
   129
52435
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51161
diff changeset
   130
code_printing
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51161
diff changeset
   131
  constant IArray.length' \<rightharpoonup> (SML) "Vector.length"
50138
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
   132
ca989d793b34 new theory of immutable arrays
nipkow
parents:
diff changeset
   133
end