src/HOL/Library/DAList.thy
author nipkow
Tue, 17 Jun 2025 14:11:40 +0200
changeset 82733 8b537e1af2ec
parent 82596 267db8c321c4
permissions -rw-r--r--
reinstated intersection of lists as inter_list_set
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46237
99c80c2f841a renamed theory AList to DAList
bulwahn
parents: 46171
diff changeset
     1
(*  Title:      HOL/Library/DAList.thy
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
     2
    Author:     Lukas Bulwahn, TU Muenchen
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
     3
*)
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
     4
58881
b9556a055632 modernized header;
wenzelm
parents: 58806
diff changeset
     5
section \<open>Abstract type of association lists with unique keys\<close>
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
     6
46237
99c80c2f841a renamed theory AList to DAList
bulwahn
parents: 46171
diff changeset
     7
theory DAList
46238
9ace9e5b79be renaming theory AList_Impl back to AList (reverting 1fec5b365f9b; AList with distinct key invariant is called DAList)
bulwahn
parents: 46237
diff changeset
     8
imports AList
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
     9
begin
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    10
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    11
text \<open>This was based on some existing fragments in the AFP-Collection framework.\<close>
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    12
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    13
subsection \<open>Preliminaries\<close>
47143
212f7a975d49 association lists with distinct keys uses the quotient infrastructure to obtain code certificates;
bulwahn
parents: 46507
diff changeset
    14
212f7a975d49 association lists with distinct keys uses the quotient infrastructure to obtain code certificates;
bulwahn
parents: 46507
diff changeset
    15
lemma distinct_map_fst_filter:
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    16
  "distinct (map fst xs) \<Longrightarrow> distinct (map fst (List.filter P xs))"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    17
  by (induct xs) auto
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    18
47143
212f7a975d49 association lists with distinct keys uses the quotient infrastructure to obtain code certificates;
bulwahn
parents: 46507
diff changeset
    19
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 60919
diff changeset
    20
subsection \<open>Type \<open>('key, 'value) alist\<close>\<close>
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    21
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    22
typedef ('key, 'value) alist = "{xs :: ('key \<times> 'value) list. (distinct \<circ> map fst) xs}"
46507
1b24c24017dd tuned proofs;
wenzelm
parents: 46238
diff changeset
    23
  morphisms impl_of Alist
1b24c24017dd tuned proofs;
wenzelm
parents: 46238
diff changeset
    24
proof
67091
1393c2340eec more symbols;
wenzelm
parents: 63684
diff changeset
    25
  show "[] \<in> {xs. (distinct \<circ> map fst) xs}"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    26
    by simp
46507
1b24c24017dd tuned proofs;
wenzelm
parents: 46238
diff changeset
    27
qed
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    28
47143
212f7a975d49 association lists with distinct keys uses the quotient infrastructure to obtain code certificates;
bulwahn
parents: 46507
diff changeset
    29
setup_lifting type_definition_alist
212f7a975d49 association lists with distinct keys uses the quotient infrastructure to obtain code certificates;
bulwahn
parents: 46507
diff changeset
    30
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    31
lemma alist_ext: "impl_of xs = impl_of ys \<Longrightarrow> xs = ys"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    32
  by (simp add: impl_of_inject)
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    33
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    34
lemma alist_eq_iff: "xs = ys \<longleftrightarrow> impl_of xs = impl_of ys"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    35
  by (simp add: impl_of_inject)
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    36
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    37
lemma impl_of_distinct [simp, intro]: "distinct (map fst (impl_of xs))"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    38
  using impl_of[of xs] by simp
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    39
82596
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 81113
diff changeset
    40
lemma impl_of_Alist:
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 81113
diff changeset
    41
  \<open>impl_of (Alist xs) = xs\<close> if \<open>distinct (map fst xs)\<close>
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 81113
diff changeset
    42
  using Alist_inverse [of xs] that by simp
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 81113
diff changeset
    43
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    44
lemma Alist_impl_of [code abstype]: "Alist (impl_of xs) = xs"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    45
  by (rule impl_of_inverse)
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    46
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    47
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    48
subsection \<open>Primitive operations\<close>
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    49
55565
f663fc1e653b simplify proofs because of the stronger reflexivity prover
kuncar
parents: 51143
diff changeset
    50
lift_definition lookup :: "('key, 'value) alist \<Rightarrow> 'key \<Rightarrow> 'value option" is map_of  .
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    51
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    52
lift_definition empty :: "('key, 'value) alist" is "[]"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    53
  by simp
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    54
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents: 47178
diff changeset
    55
lift_definition update :: "'key \<Rightarrow> 'value \<Rightarrow> ('key, 'value) alist \<Rightarrow> ('key, 'value) alist"
9caab698dbe4 new package Lifting - initial commit
kuncar
parents: 47178
diff changeset
    56
  is AList.update
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    57
  by (simp add: distinct_update)
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    58
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    59
(* FIXME: we use an unoptimised delete operation. *)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents: 47178
diff changeset
    60
lift_definition delete :: "'key \<Rightarrow> ('key, 'value) alist \<Rightarrow> ('key, 'value) alist"
9caab698dbe4 new package Lifting - initial commit
kuncar
parents: 47178
diff changeset
    61
  is AList.delete
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    62
  by (simp add: distinct_delete)
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    63
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    64
lift_definition map_entry ::
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    65
    "'key \<Rightarrow> ('value \<Rightarrow> 'value) \<Rightarrow> ('key, 'value) alist \<Rightarrow> ('key, 'value) alist"
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents: 47178
diff changeset
    66
  is AList.map_entry
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    67
  by (simp add: distinct_map_entry)
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    68
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents: 47178
diff changeset
    69
lift_definition filter :: "('key \<times> 'value \<Rightarrow> bool) \<Rightarrow> ('key, 'value) alist \<Rightarrow> ('key, 'value) alist"
9caab698dbe4 new package Lifting - initial commit
kuncar
parents: 47178
diff changeset
    70
  is List.filter
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    71
  by (simp add: distinct_map_fst_filter)
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    72
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    73
lift_definition map_default ::
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    74
    "'key \<Rightarrow> 'value \<Rightarrow> ('value \<Rightarrow> 'value) \<Rightarrow> ('key, 'value) alist \<Rightarrow> ('key, 'value) alist"
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents: 47178
diff changeset
    75
  is AList.map_default
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    76
  by (simp add: distinct_map_default)
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    77
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    78
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
    79
subsection \<open>Abstract operation properties\<close>
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    80
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    81
(* FIXME: to be completed *)
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    82
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
    83
lemma lookup_empty [simp]: "lookup empty k = None"
63684
905d3fc815ff added [simp] lemmas
nipkow
parents: 61585
diff changeset
    84
by (simp add: empty_def lookup_def Alist_inverse)
905d3fc815ff added [simp] lemmas
nipkow
parents: 61585
diff changeset
    85
905d3fc815ff added [simp] lemmas
nipkow
parents: 61585
diff changeset
    86
lemma lookup_update:
905d3fc815ff added [simp] lemmas
nipkow
parents: 61585
diff changeset
    87
  "lookup (update k1 v xs) k2 = (if k1 = k2 then Some v else lookup xs k2)"
905d3fc815ff added [simp] lemmas
nipkow
parents: 61585
diff changeset
    88
by(transfer)(simp add: update_conv')
905d3fc815ff added [simp] lemmas
nipkow
parents: 61585
diff changeset
    89
905d3fc815ff added [simp] lemmas
nipkow
parents: 61585
diff changeset
    90
lemma lookup_update_eq [simp]:
905d3fc815ff added [simp] lemmas
nipkow
parents: 61585
diff changeset
    91
  "k1 = k2 \<Longrightarrow> lookup (update k1 v xs) k2 = Some v"
905d3fc815ff added [simp] lemmas
nipkow
parents: 61585
diff changeset
    92
by(simp add: lookup_update)
905d3fc815ff added [simp] lemmas
nipkow
parents: 61585
diff changeset
    93
905d3fc815ff added [simp] lemmas
nipkow
parents: 61585
diff changeset
    94
lemma lookup_update_neq [simp]:
905d3fc815ff added [simp] lemmas
nipkow
parents: 61585
diff changeset
    95
  "k1 \<noteq> k2 \<Longrightarrow> lookup (update k1 v xs) k2 = lookup xs k2"
905d3fc815ff added [simp] lemmas
nipkow
parents: 61585
diff changeset
    96
by(simp add: lookup_update)
905d3fc815ff added [simp] lemmas
nipkow
parents: 61585
diff changeset
    97
905d3fc815ff added [simp] lemmas
nipkow
parents: 61585
diff changeset
    98
lemma update_update_eq [simp]:
905d3fc815ff added [simp] lemmas
nipkow
parents: 61585
diff changeset
    99
  "k1 = k2 \<Longrightarrow> update k2 v2 (update k1 v1 xs) = update k2 v2 xs"
905d3fc815ff added [simp] lemmas
nipkow
parents: 61585
diff changeset
   100
by(transfer)(simp add: update_conv')
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   101
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   102
lemma lookup_delete [simp]: "lookup (delete k al) = (lookup al)(k := None)"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   103
  by (simp add: lookup_def delete_def Alist_inverse distinct_delete delete_conv')
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   104
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   105
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   106
subsection \<open>Further operations\<close>
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   107
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   108
subsubsection \<open>Equality\<close>
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   109
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   110
instantiation alist :: (equal, equal) equal
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   111
begin
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   112
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   113
definition "HOL.equal (xs :: ('a, 'b) alist) ys == impl_of xs = impl_of ys"
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   114
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   115
instance
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 59581
diff changeset
   116
  by standard (simp add: equal_alist_def impl_of_inject)
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   117
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   118
end
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   119
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   120
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   121
subsubsection \<open>Size\<close>
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   122
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   123
instantiation alist :: (type, type) size
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   124
begin
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   125
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   126
definition "size (al :: ('a, 'b) alist) = length (impl_of al)"
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   127
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   128
instance ..
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   129
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   130
end
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   131
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   132
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   133
subsection \<open>Quickcheck generators\<close>
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   134
72607
feebdaa346e5 bundles for reflected term syntax
haftmann
parents: 72581
diff changeset
   135
context
81113
6fefd6c602fa clarified syntax for opening bundles;
wenzelm
parents: 72607
diff changeset
   136
  includes state_combinator_syntax and term_syntax
72607
feebdaa346e5 bundles for reflected term syntax
haftmann
parents: 72581
diff changeset
   137
begin
feebdaa346e5 bundles for reflected term syntax
haftmann
parents: 72581
diff changeset
   138
feebdaa346e5 bundles for reflected term syntax
haftmann
parents: 72581
diff changeset
   139
definition
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   140
  valterm_empty :: "('key :: typerep, 'value :: typerep) alist \<times> (unit \<Rightarrow> Code_Evaluation.term)"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   141
  where "valterm_empty = Code_Evaluation.valtermify empty"
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   142
72607
feebdaa346e5 bundles for reflected term syntax
haftmann
parents: 72581
diff changeset
   143
definition
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   144
  valterm_update :: "'key :: typerep \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow>
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   145
  'value :: typerep \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow>
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   146
  ('key, 'value) alist \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow>
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   147
  ('key, 'value) alist \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   148
  [code_unfold]: "valterm_update k v a = Code_Evaluation.valtermify update {\<cdot>} k {\<cdot>} v {\<cdot>}a"
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   149
72581
de581f98a3a1 bundled syntax for state monad combinators
haftmann
parents: 67091
diff changeset
   150
fun random_aux_alist
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   151
where
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   152
  "random_aux_alist i j =
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   153
    (if i = 0 then Pair valterm_empty
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   154
     else Quickcheck_Random.collapse
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   155
       (Random.select_weight
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   156
         [(i, Quickcheck_Random.random j \<circ>\<rightarrow> (\<lambda>k. Quickcheck_Random.random j \<circ>\<rightarrow>
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   157
           (\<lambda>v. random_aux_alist (i - 1) j \<circ>\<rightarrow> (\<lambda>a. Pair (valterm_update k v a))))),
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   158
          (1, Pair valterm_empty)]))"
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   159
72581
de581f98a3a1 bundled syntax for state monad combinators
haftmann
parents: 67091
diff changeset
   160
end
de581f98a3a1 bundled syntax for state monad combinators
haftmann
parents: 67091
diff changeset
   161
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   162
instantiation alist :: (random, random) random
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   163
begin
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   164
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   165
definition random_alist
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   166
where
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   167
  "random_alist i = random_aux_alist i i"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   168
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   169
instance ..
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   170
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   171
end
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   172
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   173
instantiation alist :: (exhaustive, exhaustive) exhaustive
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   174
begin
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   175
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   176
fun exhaustive_alist ::
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   177
  "(('a, 'b) alist \<Rightarrow> (bool \<times> term list) option) \<Rightarrow> natural \<Rightarrow> (bool \<times> term list) option"
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   178
where
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   179
  "exhaustive_alist f i =
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   180
    (if i = 0 then None
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   181
     else
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   182
      case f empty of
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   183
        Some ts \<Rightarrow> Some ts
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   184
      | None \<Rightarrow>
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   185
          exhaustive_alist
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   186
            (\<lambda>a. Quickcheck_Exhaustive.exhaustive
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   187
              (\<lambda>k. Quickcheck_Exhaustive.exhaustive (\<lambda>v. f (update k v a)) (i - 1)) (i - 1))
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   188
            (i - 1))"
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   189
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   190
instance ..
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   191
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   192
end
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   193
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   194
instantiation alist :: (full_exhaustive, full_exhaustive) full_exhaustive
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   195
begin
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   196
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   197
fun full_exhaustive_alist ::
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   198
  "(('a, 'b) alist \<times> (unit \<Rightarrow> term) \<Rightarrow> (bool \<times> term list) option) \<Rightarrow> natural \<Rightarrow>
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   199
    (bool \<times> term list) option"
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   200
where
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   201
  "full_exhaustive_alist f i =
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   202
    (if i = 0 then None
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   203
     else
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   204
      case f valterm_empty of
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   205
        Some ts \<Rightarrow> Some ts
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   206
      | None \<Rightarrow>
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   207
          full_exhaustive_alist
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   208
            (\<lambda>a.
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   209
              Quickcheck_Exhaustive.full_exhaustive
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   210
                (\<lambda>k. Quickcheck_Exhaustive.full_exhaustive (\<lambda>v. f (valterm_update k v a)) (i - 1))
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   211
              (i - 1))
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55565
diff changeset
   212
            (i - 1))"
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   213
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   214
instance ..
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   215
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   216
end
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   217
59581
09722854b8f4 alist is a BNF
traytel
parents: 58881
diff changeset
   218
09722854b8f4 alist is a BNF
traytel
parents: 58881
diff changeset
   219
section \<open>alist is a BNF\<close>
09722854b8f4 alist is a BNF
traytel
parents: 58881
diff changeset
   220
60919
b0ba7799d05a use lift_bnf in an example
traytel
parents: 60679
diff changeset
   221
lift_bnf (dead 'k, set: 'v) alist [wits: "[] :: ('k \<times> 'v) list"] for map: map rel: rel
b0ba7799d05a use lift_bnf in an example
traytel
parents: 60679
diff changeset
   222
  by auto
59581
09722854b8f4 alist is a BNF
traytel
parents: 58881
diff changeset
   223
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   224
hide_const valterm_empty valterm_update random_aux_alist
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   225
46171
19f68d7671f0 proper hiding of facts and constants in AList_Impl and AList theory
bulwahn
parents: 46167
diff changeset
   226
hide_fact (open) lookup_def empty_def update_def delete_def map_entry_def filter_def map_default_def
59581
09722854b8f4 alist is a BNF
traytel
parents: 58881
diff changeset
   227
hide_const (open) impl_of lookup empty update delete map_entry filter map_default map set rel
46167
25eba8a5d7d0 adding theory association lists with invariant
bulwahn
parents:
diff changeset
   228
46238
9ace9e5b79be renaming theory AList_Impl back to AList (reverting 1fec5b365f9b; AList with distinct key invariant is called DAList)
bulwahn
parents: 46237
diff changeset
   229
end