src/HOL/Predicate.thy
author wenzelm
Fri Oct 27 13:50:08 2017 +0200 (22 months ago)
changeset 66924 b4d4027f743b
parent 66251 cd935b7cb3fb
child 67091 1393c2340eec
permissions -rw-r--r--
more permissive;
berghofe@22259
     1
(*  Title:      HOL/Predicate.thy
haftmann@46664
     2
    Author:     Lukas Bulwahn and Florian Haftmann, TU Muenchen
berghofe@22259
     3
*)
berghofe@22259
     4
wenzelm@60758
     5
section \<open>Predicates as enumerations\<close>
berghofe@22259
     6
berghofe@22259
     7
theory Predicate
Andreas@53943
     8
imports String
berghofe@22259
     9
begin
berghofe@22259
    10
wenzelm@60758
    11
subsection \<open>The type of predicate enumerations (a monad)\<close>
haftmann@30328
    12
haftmann@66012
    13
datatype (plugins only: extraction) (dead 'a) pred = Pred (eval: "'a \<Rightarrow> bool")
haftmann@30328
    14
haftmann@40616
    15
lemma pred_eqI:
haftmann@40616
    16
  "(\<And>w. eval P w \<longleftrightarrow> eval Q w) \<Longrightarrow> P = Q"
haftmann@40616
    17
  by (cases P, cases Q) (auto simp add: fun_eq_iff)
haftmann@30328
    18
haftmann@46038
    19
lemma pred_eq_iff:
haftmann@46038
    20
  "P = Q \<Longrightarrow> (\<And>w. eval P w \<longleftrightarrow> eval Q w)"
haftmann@46038
    21
  by (simp add: pred_eqI)
haftmann@46038
    22
haftmann@44033
    23
instantiation pred :: (type) complete_lattice
haftmann@30328
    24
begin
haftmann@30328
    25
haftmann@30328
    26
definition
haftmann@30328
    27
  "P \<le> Q \<longleftrightarrow> eval P \<le> eval Q"
haftmann@30328
    28
haftmann@30328
    29
definition
haftmann@30328
    30
  "P < Q \<longleftrightarrow> eval P < eval Q"
haftmann@30328
    31
haftmann@30328
    32
definition
haftmann@30328
    33
  "\<bottom> = Pred \<bottom>"
haftmann@30328
    34
haftmann@40616
    35
lemma eval_bot [simp]:
haftmann@40616
    36
  "eval \<bottom>  = \<bottom>"
haftmann@40616
    37
  by (simp add: bot_pred_def)
haftmann@40616
    38
haftmann@30328
    39
definition
haftmann@30328
    40
  "\<top> = Pred \<top>"
haftmann@30328
    41
haftmann@40616
    42
lemma eval_top [simp]:
haftmann@40616
    43
  "eval \<top>  = \<top>"
haftmann@40616
    44
  by (simp add: top_pred_def)
haftmann@40616
    45
haftmann@30328
    46
definition
haftmann@30328
    47
  "P \<sqinter> Q = Pred (eval P \<sqinter> eval Q)"
haftmann@30328
    48
haftmann@40616
    49
lemma eval_inf [simp]:
haftmann@40616
    50
  "eval (P \<sqinter> Q) = eval P \<sqinter> eval Q"
haftmann@40616
    51
  by (simp add: inf_pred_def)
haftmann@40616
    52
haftmann@30328
    53
definition
haftmann@30328
    54
  "P \<squnion> Q = Pred (eval P \<squnion> eval Q)"
haftmann@30328
    55
haftmann@40616
    56
lemma eval_sup [simp]:
haftmann@40616
    57
  "eval (P \<squnion> Q) = eval P \<squnion> eval Q"
haftmann@40616
    58
  by (simp add: sup_pred_def)
haftmann@40616
    59
haftmann@30328
    60
definition
haftmann@56218
    61
  "\<Sqinter>A = Pred (INFIMUM A eval)"
haftmann@30328
    62
haftmann@40616
    63
lemma eval_Inf [simp]:
haftmann@56218
    64
  "eval (\<Sqinter>A) = INFIMUM A eval"
haftmann@40616
    65
  by (simp add: Inf_pred_def)
haftmann@40616
    66
haftmann@30328
    67
definition
haftmann@56218
    68
  "\<Squnion>A = Pred (SUPREMUM A eval)"
haftmann@30328
    69
haftmann@40616
    70
lemma eval_Sup [simp]:
haftmann@56218
    71
  "eval (\<Squnion>A) = SUPREMUM A eval"
haftmann@40616
    72
  by (simp add: Sup_pred_def)
haftmann@40616
    73
haftmann@44033
    74
instance proof
haftmann@44415
    75
qed (auto intro!: pred_eqI simp add: less_eq_pred_def less_pred_def le_fun_def less_fun_def)
haftmann@44033
    76
haftmann@44033
    77
end
haftmann@44033
    78
haftmann@56212
    79
lemma eval_INF [simp]:
haftmann@56218
    80
  "eval (INFIMUM A f) = INFIMUM A (eval \<circ> f)"
haftmann@56166
    81
  using eval_Inf [of "f ` A"] by simp
haftmann@44033
    82
haftmann@56212
    83
lemma eval_SUP [simp]:
haftmann@56218
    84
  "eval (SUPREMUM A f) = SUPREMUM A (eval \<circ> f)"
haftmann@56166
    85
  using eval_Sup [of "f ` A"] by simp
haftmann@44033
    86
haftmann@44033
    87
instantiation pred :: (type) complete_boolean_algebra
haftmann@44033
    88
begin
haftmann@44033
    89
haftmann@32578
    90
definition
haftmann@32578
    91
  "- P = Pred (- eval P)"
haftmann@32578
    92
haftmann@40616
    93
lemma eval_compl [simp]:
haftmann@40616
    94
  "eval (- P) = - eval P"
haftmann@40616
    95
  by (simp add: uminus_pred_def)
haftmann@40616
    96
haftmann@32578
    97
definition
haftmann@32578
    98
  "P - Q = Pred (eval P - eval Q)"
haftmann@32578
    99
haftmann@40616
   100
lemma eval_minus [simp]:
haftmann@40616
   101
  "eval (P - Q) = eval P - eval Q"
haftmann@40616
   102
  by (simp add: minus_pred_def)
haftmann@40616
   103
haftmann@32578
   104
instance proof
noschinl@46884
   105
qed (auto intro!: pred_eqI)
haftmann@30328
   106
berghofe@22259
   107
end
haftmann@30328
   108
haftmann@40616
   109
definition single :: "'a \<Rightarrow> 'a pred" where
haftmann@40616
   110
  "single x = Pred ((op =) x)"
haftmann@40616
   111
haftmann@40616
   112
lemma eval_single [simp]:
haftmann@40616
   113
  "eval (single x) = (op =) x"
haftmann@40616
   114
  by (simp add: single_def)
haftmann@40616
   115
wenzelm@62026
   116
definition bind :: "'a pred \<Rightarrow> ('a \<Rightarrow> 'b pred) \<Rightarrow> 'b pred" (infixl "\<bind>" 70) where
wenzelm@62026
   117
  "P \<bind> f = (SUPREMUM {x. eval P x} f)"
haftmann@40616
   118
haftmann@40616
   119
lemma eval_bind [simp]:
wenzelm@62026
   120
  "eval (P \<bind> f) = eval (SUPREMUM {x. eval P x} f)"
haftmann@40616
   121
  by (simp add: bind_def)
haftmann@40616
   122
haftmann@30328
   123
lemma bind_bind:
wenzelm@62026
   124
  "(P \<bind> Q) \<bind> R = P \<bind> (\<lambda>x. Q x \<bind> R)"
noschinl@46884
   125
  by (rule pred_eqI) auto
haftmann@30328
   126
haftmann@30328
   127
lemma bind_single:
wenzelm@62026
   128
  "P \<bind> single = P"
haftmann@40616
   129
  by (rule pred_eqI) auto
haftmann@30328
   130
haftmann@30328
   131
lemma single_bind:
wenzelm@62026
   132
  "single x \<bind> P = P x"
haftmann@40616
   133
  by (rule pred_eqI) auto
haftmann@30328
   134
haftmann@30328
   135
lemma bottom_bind:
wenzelm@62026
   136
  "\<bottom> \<bind> P = \<bottom>"
haftmann@40674
   137
  by (rule pred_eqI) auto
haftmann@30328
   138
haftmann@30328
   139
lemma sup_bind:
wenzelm@62026
   140
  "(P \<squnion> Q) \<bind> R = P \<bind> R \<squnion> Q \<bind> R"
haftmann@40674
   141
  by (rule pred_eqI) auto
haftmann@30328
   142
haftmann@40616
   143
lemma Sup_bind:
wenzelm@62026
   144
  "(\<Squnion>A \<bind> f) = \<Squnion>((\<lambda>x. x \<bind> f) ` A)"
noschinl@46884
   145
  by (rule pred_eqI) auto
haftmann@30328
   146
haftmann@30328
   147
lemma pred_iffI:
haftmann@30328
   148
  assumes "\<And>x. eval A x \<Longrightarrow> eval B x"
haftmann@30328
   149
  and "\<And>x. eval B x \<Longrightarrow> eval A x"
haftmann@30328
   150
  shows "A = B"
haftmann@40616
   151
  using assms by (auto intro: pred_eqI)
haftmann@30328
   152
  
haftmann@30328
   153
lemma singleI: "eval (single x) x"
haftmann@40616
   154
  by simp
haftmann@30328
   155
haftmann@30328
   156
lemma singleI_unit: "eval (single ()) x"
haftmann@40616
   157
  by simp
haftmann@30328
   158
haftmann@30328
   159
lemma singleE: "eval (single x) y \<Longrightarrow> (y = x \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@40616
   160
  by simp
haftmann@30328
   161
haftmann@30328
   162
lemma singleE': "eval (single x) y \<Longrightarrow> (x = y \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@40616
   163
  by simp
haftmann@30328
   164
wenzelm@62026
   165
lemma bindI: "eval P x \<Longrightarrow> eval (Q x) y \<Longrightarrow> eval (P \<bind> Q) y"
haftmann@40616
   166
  by auto
haftmann@30328
   167
wenzelm@62026
   168
lemma bindE: "eval (R \<bind> Q) y \<Longrightarrow> (\<And>x. eval R x \<Longrightarrow> eval (Q x) y \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@40616
   169
  by auto
haftmann@30328
   170
haftmann@30328
   171
lemma botE: "eval \<bottom> x \<Longrightarrow> P"
haftmann@40616
   172
  by auto
haftmann@30328
   173
haftmann@30328
   174
lemma supI1: "eval A x \<Longrightarrow> eval (A \<squnion> B) x"
haftmann@40616
   175
  by auto
haftmann@30328
   176
haftmann@30328
   177
lemma supI2: "eval B x \<Longrightarrow> eval (A \<squnion> B) x" 
haftmann@40616
   178
  by auto
haftmann@30328
   179
haftmann@30328
   180
lemma supE: "eval (A \<squnion> B) x \<Longrightarrow> (eval A x \<Longrightarrow> P) \<Longrightarrow> (eval B x \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@40616
   181
  by auto
haftmann@30328
   182
haftmann@32578
   183
lemma single_not_bot [simp]:
haftmann@32578
   184
  "single x \<noteq> \<bottom>"
nipkow@39302
   185
  by (auto simp add: single_def bot_pred_def fun_eq_iff)
haftmann@32578
   186
haftmann@32578
   187
lemma not_bot:
haftmann@32578
   188
  assumes "A \<noteq> \<bottom>"
haftmann@32578
   189
  obtains x where "eval A x"
haftmann@45970
   190
  using assms by (cases A) (auto simp add: bot_pred_def)
haftmann@45970
   191
haftmann@32578
   192
wenzelm@60758
   193
subsection \<open>Emptiness check and definite choice\<close>
haftmann@32578
   194
haftmann@32578
   195
definition is_empty :: "'a pred \<Rightarrow> bool" where
haftmann@32578
   196
  "is_empty A \<longleftrightarrow> A = \<bottom>"
haftmann@32578
   197
haftmann@32578
   198
lemma is_empty_bot:
haftmann@32578
   199
  "is_empty \<bottom>"
haftmann@32578
   200
  by (simp add: is_empty_def)
haftmann@32578
   201
haftmann@32578
   202
lemma not_is_empty_single:
haftmann@32578
   203
  "\<not> is_empty (single x)"
nipkow@39302
   204
  by (auto simp add: is_empty_def single_def bot_pred_def fun_eq_iff)
haftmann@32578
   205
haftmann@32578
   206
lemma is_empty_sup:
haftmann@32578
   207
  "is_empty (A \<squnion> B) \<longleftrightarrow> is_empty A \<and> is_empty B"
huffman@36008
   208
  by (auto simp add: is_empty_def)
haftmann@32578
   209
haftmann@40616
   210
definition singleton :: "(unit \<Rightarrow> 'a) \<Rightarrow> 'a pred \<Rightarrow> 'a" where
haftmann@66251
   211
  "singleton default A = (if \<exists>!x. eval A x then THE x. eval A x else default ())" for default
haftmann@32578
   212
haftmann@32578
   213
lemma singleton_eqI:
haftmann@66251
   214
  "\<exists>!x. eval A x \<Longrightarrow> eval A x \<Longrightarrow> singleton default A = x" for default
haftmann@32578
   215
  by (auto simp add: singleton_def)
haftmann@32578
   216
haftmann@32578
   217
lemma eval_singletonI:
haftmann@66251
   218
  "\<exists>!x. eval A x \<Longrightarrow> eval A (singleton default A)" for default
haftmann@32578
   219
proof -
haftmann@32578
   220
  assume assm: "\<exists>!x. eval A x"
wenzelm@53374
   221
  then obtain x where x: "eval A x" ..
wenzelm@60166
   222
  with assm have "singleton default A = x" by (rule singleton_eqI)
wenzelm@53374
   223
  with x show ?thesis by simp
haftmann@32578
   224
qed
haftmann@32578
   225
haftmann@32578
   226
lemma single_singleton:
haftmann@66251
   227
  "\<exists>!x. eval A x \<Longrightarrow> single (singleton default A) = A" for default
haftmann@32578
   228
proof -
haftmann@32578
   229
  assume assm: "\<exists>!x. eval A x"
wenzelm@60166
   230
  then have "eval A (singleton default A)"
haftmann@32578
   231
    by (rule eval_singletonI)
wenzelm@60166
   232
  moreover from assm have "\<And>x. eval A x \<Longrightarrow> singleton default A = x"
haftmann@32578
   233
    by (rule singleton_eqI)
wenzelm@60166
   234
  ultimately have "eval (single (singleton default A)) = eval A"
nipkow@39302
   235
    by (simp (no_asm_use) add: single_def fun_eq_iff) blast
wenzelm@60166
   236
  then have "\<And>x. eval (single (singleton default A)) x = eval A x"
haftmann@40616
   237
    by simp
haftmann@40616
   238
  then show ?thesis by (rule pred_eqI)
haftmann@32578
   239
qed
haftmann@32578
   240
haftmann@32578
   241
lemma singleton_undefinedI:
haftmann@66251
   242
  "\<not> (\<exists>!x. eval A x) \<Longrightarrow> singleton default A = default ()" for default
haftmann@32578
   243
  by (simp add: singleton_def)
haftmann@32578
   244
haftmann@32578
   245
lemma singleton_bot:
haftmann@66251
   246
  "singleton default \<bottom> = default ()" for default
haftmann@32578
   247
  by (auto simp add: bot_pred_def intro: singleton_undefinedI)
haftmann@32578
   248
haftmann@32578
   249
lemma singleton_single:
haftmann@66251
   250
  "singleton default (single x) = x" for default
haftmann@32578
   251
  by (auto simp add: intro: singleton_eqI singleI elim: singleE)
haftmann@32578
   252
haftmann@32578
   253
lemma singleton_sup_single_single:
haftmann@66251
   254
  "singleton default (single x \<squnion> single y) = (if x = y then x else default ())" for default
haftmann@32578
   255
proof (cases "x = y")
haftmann@32578
   256
  case True then show ?thesis by (simp add: singleton_single)
haftmann@32578
   257
next
haftmann@32578
   258
  case False
haftmann@32578
   259
  have "eval (single x \<squnion> single y) x"
haftmann@32578
   260
    and "eval (single x \<squnion> single y) y"
haftmann@32578
   261
  by (auto intro: supI1 supI2 singleI)
haftmann@32578
   262
  with False have "\<not> (\<exists>!z. eval (single x \<squnion> single y) z)"
haftmann@32578
   263
    by blast
wenzelm@60166
   264
  then have "singleton default (single x \<squnion> single y) = default ()"
haftmann@32578
   265
    by (rule singleton_undefinedI)
haftmann@32578
   266
  with False show ?thesis by simp
haftmann@32578
   267
qed
haftmann@32578
   268
haftmann@32578
   269
lemma singleton_sup_aux:
wenzelm@60166
   270
  "singleton default (A \<squnion> B) = (if A = \<bottom> then singleton default B
wenzelm@60166
   271
    else if B = \<bottom> then singleton default A
wenzelm@60166
   272
    else singleton default
haftmann@66251
   273
      (single (singleton default A) \<squnion> single (singleton default B)))" for default
haftmann@32578
   274
proof (cases "(\<exists>!x. eval A x) \<and> (\<exists>!y. eval B y)")
haftmann@32578
   275
  case True then show ?thesis by (simp add: single_singleton)
haftmann@32578
   276
next
haftmann@32578
   277
  case False
haftmann@32578
   278
  from False have A_or_B:
wenzelm@60166
   279
    "singleton default A = default () \<or> singleton default B = default ()"
haftmann@32578
   280
    by (auto intro!: singleton_undefinedI)
wenzelm@60166
   281
  then have rhs: "singleton default
wenzelm@60166
   282
    (single (singleton default A) \<squnion> single (singleton default B)) = default ()"
haftmann@32578
   283
    by (auto simp add: singleton_sup_single_single singleton_single)
haftmann@32578
   284
  from False have not_unique:
haftmann@32578
   285
    "\<not> (\<exists>!x. eval A x) \<or> \<not> (\<exists>!y. eval B y)" by simp
haftmann@32578
   286
  show ?thesis proof (cases "A \<noteq> \<bottom> \<and> B \<noteq> \<bottom>")
haftmann@32578
   287
    case True
haftmann@32578
   288
    then obtain a b where a: "eval A a" and b: "eval B b"
haftmann@32578
   289
      by (blast elim: not_bot)
haftmann@32578
   290
    with True not_unique have "\<not> (\<exists>!x. eval (A \<squnion> B) x)"
haftmann@32578
   291
      by (auto simp add: sup_pred_def bot_pred_def)
wenzelm@60166
   292
    then have "singleton default (A \<squnion> B) = default ()" by (rule singleton_undefinedI)
haftmann@32578
   293
    with True rhs show ?thesis by simp
haftmann@32578
   294
  next
haftmann@32578
   295
    case False then show ?thesis by auto
haftmann@32578
   296
  qed
haftmann@32578
   297
qed
haftmann@32578
   298
haftmann@32578
   299
lemma singleton_sup:
wenzelm@60166
   300
  "singleton default (A \<squnion> B) = (if A = \<bottom> then singleton default B
wenzelm@60166
   301
    else if B = \<bottom> then singleton default A
haftmann@66251
   302
    else if singleton default A = singleton default B then singleton default A else default ())" for default
wenzelm@60166
   303
  using singleton_sup_aux [of default A B] by (simp only: singleton_sup_single_single)
haftmann@32578
   304
haftmann@30328
   305
wenzelm@60758
   306
subsection \<open>Derived operations\<close>
haftmann@30328
   307
haftmann@30328
   308
definition if_pred :: "bool \<Rightarrow> unit pred" where
haftmann@30328
   309
  if_pred_eq: "if_pred b = (if b then single () else \<bottom>)"
haftmann@30328
   310
bulwahn@33754
   311
definition holds :: "unit pred \<Rightarrow> bool" where
bulwahn@33754
   312
  holds_eq: "holds P = eval P ()"
bulwahn@33754
   313
haftmann@30328
   314
definition not_pred :: "unit pred \<Rightarrow> unit pred" where
haftmann@30328
   315
  not_pred_eq: "not_pred P = (if eval P () then \<bottom> else single ())"
haftmann@30328
   316
haftmann@30328
   317
lemma if_predI: "P \<Longrightarrow> eval (if_pred P) ()"
haftmann@30328
   318
  unfolding if_pred_eq by (auto intro: singleI)
haftmann@30328
   319
haftmann@30328
   320
lemma if_predE: "eval (if_pred b) x \<Longrightarrow> (b \<Longrightarrow> x = () \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@30328
   321
  unfolding if_pred_eq by (cases b) (auto elim: botE)
haftmann@30328
   322
haftmann@30328
   323
lemma not_predI: "\<not> P \<Longrightarrow> eval (not_pred (Pred (\<lambda>u. P))) ()"
haftmann@66012
   324
  unfolding not_pred_eq by (auto intro: singleI)
haftmann@30328
   325
haftmann@30328
   326
lemma not_predI': "\<not> eval P () \<Longrightarrow> eval (not_pred P) ()"
haftmann@30328
   327
  unfolding not_pred_eq by (auto intro: singleI)
haftmann@30328
   328
haftmann@30328
   329
lemma not_predE: "eval (not_pred (Pred (\<lambda>u. P))) x \<Longrightarrow> (\<not> P \<Longrightarrow> thesis) \<Longrightarrow> thesis"
haftmann@30328
   330
  unfolding not_pred_eq
nipkow@62390
   331
  by (auto split: if_split_asm elim: botE)
haftmann@30328
   332
haftmann@30328
   333
lemma not_predE': "eval (not_pred P) x \<Longrightarrow> (\<not> eval P x \<Longrightarrow> thesis) \<Longrightarrow> thesis"
haftmann@30328
   334
  unfolding not_pred_eq
nipkow@62390
   335
  by (auto split: if_split_asm elim: botE)
bulwahn@33754
   336
lemma "f () = False \<or> f () = True"
bulwahn@33754
   337
by simp
haftmann@30328
   338
blanchet@37549
   339
lemma closure_of_bool_cases [no_atp]:
haftmann@44007
   340
  fixes f :: "unit \<Rightarrow> bool"
haftmann@44007
   341
  assumes "f = (\<lambda>u. False) \<Longrightarrow> P f"
haftmann@44007
   342
  assumes "f = (\<lambda>u. True) \<Longrightarrow> P f"
haftmann@44007
   343
  shows "P f"
bulwahn@33754
   344
proof -
haftmann@44007
   345
  have "f = (\<lambda>u. False) \<or> f = (\<lambda>u. True)"
bulwahn@33754
   346
    apply (cases "f ()")
bulwahn@33754
   347
    apply (rule disjI2)
bulwahn@33754
   348
    apply (rule ext)
bulwahn@33754
   349
    apply (simp add: unit_eq)
bulwahn@33754
   350
    apply (rule disjI1)
bulwahn@33754
   351
    apply (rule ext)
bulwahn@33754
   352
    apply (simp add: unit_eq)
bulwahn@33754
   353
    done
wenzelm@41550
   354
  from this assms show ?thesis by blast
bulwahn@33754
   355
qed
bulwahn@33754
   356
bulwahn@33754
   357
lemma unit_pred_cases:
haftmann@44007
   358
  assumes "P \<bottom>"
haftmann@44007
   359
  assumes "P (single ())"
haftmann@44007
   360
  shows "P Q"
haftmann@44415
   361
using assms unfolding bot_pred_def bot_fun_def bot_bool_def empty_def single_def proof (cases Q)
haftmann@44007
   362
  fix f
haftmann@44007
   363
  assume "P (Pred (\<lambda>u. False))" "P (Pred (\<lambda>u. () = u))"
haftmann@44007
   364
  then have "P (Pred f)" 
haftmann@44007
   365
    by (cases _ f rule: closure_of_bool_cases) simp_all
haftmann@44007
   366
  moreover assume "Q = Pred f"
haftmann@44007
   367
  ultimately show "P Q" by simp
haftmann@44007
   368
qed
haftmann@44007
   369
  
bulwahn@33754
   370
lemma holds_if_pred:
bulwahn@33754
   371
  "holds (if_pred b) = b"
bulwahn@33754
   372
unfolding if_pred_eq holds_eq
bulwahn@33754
   373
by (cases b) (auto intro: singleI elim: botE)
bulwahn@33754
   374
bulwahn@33754
   375
lemma if_pred_holds:
bulwahn@33754
   376
  "if_pred (holds P) = P"
bulwahn@33754
   377
unfolding if_pred_eq holds_eq
bulwahn@33754
   378
by (rule unit_pred_cases) (auto intro: singleI elim: botE)
bulwahn@33754
   379
bulwahn@33754
   380
lemma is_empty_holds:
bulwahn@33754
   381
  "is_empty P \<longleftrightarrow> \<not> holds P"
bulwahn@33754
   382
unfolding is_empty_def holds_eq
bulwahn@33754
   383
by (rule unit_pred_cases) (auto elim: botE intro: singleI)
haftmann@30328
   384
haftmann@41311
   385
definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a pred \<Rightarrow> 'b pred" where
wenzelm@62026
   386
  "map f P = P \<bind> (single o f)"
haftmann@41311
   387
haftmann@41311
   388
lemma eval_map [simp]:
haftmann@44363
   389
  "eval (map f P) = (\<Squnion>x\<in>{x. eval P x}. (\<lambda>y. f x = y))"
haftmann@44415
   390
  by (auto simp add: map_def comp_def)
haftmann@41311
   391
blanchet@55467
   392
functor map: map
haftmann@44363
   393
  by (rule ext, rule pred_eqI, auto)+
haftmann@41311
   394
haftmann@41311
   395
wenzelm@60758
   396
subsection \<open>Implementation\<close>
haftmann@30328
   397
blanchet@58350
   398
datatype (plugins only: code extraction) (dead 'a) seq =
blanchet@58334
   399
  Empty
blanchet@58334
   400
| Insert "'a" "'a pred"
blanchet@58334
   401
| Join "'a pred" "'a seq"
haftmann@30328
   402
haftmann@30328
   403
primrec pred_of_seq :: "'a seq \<Rightarrow> 'a pred" where
haftmann@44414
   404
  "pred_of_seq Empty = \<bottom>"
haftmann@44414
   405
| "pred_of_seq (Insert x P) = single x \<squnion> P"
haftmann@44414
   406
| "pred_of_seq (Join P xq) = P \<squnion> pred_of_seq xq"
haftmann@30328
   407
haftmann@30328
   408
definition Seq :: "(unit \<Rightarrow> 'a seq) \<Rightarrow> 'a pred" where
haftmann@30328
   409
  "Seq f = pred_of_seq (f ())"
haftmann@30328
   410
haftmann@30328
   411
code_datatype Seq
haftmann@30328
   412
haftmann@30328
   413
primrec member :: "'a seq \<Rightarrow> 'a \<Rightarrow> bool"  where
haftmann@30328
   414
  "member Empty x \<longleftrightarrow> False"
haftmann@44414
   415
| "member (Insert y P) x \<longleftrightarrow> x = y \<or> eval P x"
haftmann@44414
   416
| "member (Join P xq) x \<longleftrightarrow> eval P x \<or> member xq x"
haftmann@30328
   417
haftmann@30328
   418
lemma eval_member:
haftmann@30328
   419
  "member xq = eval (pred_of_seq xq)"
haftmann@30328
   420
proof (induct xq)
haftmann@30328
   421
  case Empty show ?case
nipkow@39302
   422
  by (auto simp add: fun_eq_iff elim: botE)
haftmann@30328
   423
next
haftmann@30328
   424
  case Insert show ?case
nipkow@39302
   425
  by (auto simp add: fun_eq_iff elim: supE singleE intro: supI1 supI2 singleI)
haftmann@30328
   426
next
haftmann@30328
   427
  case Join then show ?case
nipkow@39302
   428
  by (auto simp add: fun_eq_iff elim: supE intro: supI1 supI2)
haftmann@30328
   429
qed
haftmann@30328
   430
haftmann@46038
   431
lemma eval_code [(* FIXME declare simp *)code]: "eval (Seq f) = member (f ())"
haftmann@30328
   432
  unfolding Seq_def by (rule sym, rule eval_member)
haftmann@30328
   433
haftmann@30328
   434
lemma single_code [code]:
haftmann@30328
   435
  "single x = Seq (\<lambda>u. Insert x \<bottom>)"
haftmann@30328
   436
  unfolding Seq_def by simp
haftmann@30328
   437
haftmann@41080
   438
primrec "apply" :: "('a \<Rightarrow> 'b pred) \<Rightarrow> 'a seq \<Rightarrow> 'b seq" where
haftmann@44415
   439
  "apply f Empty = Empty"
wenzelm@62026
   440
| "apply f (Insert x P) = Join (f x) (Join (P \<bind> f) Empty)"
wenzelm@62026
   441
| "apply f (Join P xq) = Join (P \<bind> f) (apply f xq)"
haftmann@30328
   442
haftmann@30328
   443
lemma apply_bind:
wenzelm@62026
   444
  "pred_of_seq (apply f xq) = pred_of_seq xq \<bind> f"
haftmann@30328
   445
proof (induct xq)
haftmann@30328
   446
  case Empty show ?case
haftmann@30328
   447
    by (simp add: bottom_bind)
haftmann@30328
   448
next
haftmann@30328
   449
  case Insert show ?case
haftmann@30328
   450
    by (simp add: single_bind sup_bind)
haftmann@30328
   451
next
haftmann@30328
   452
  case Join then show ?case
haftmann@30328
   453
    by (simp add: sup_bind)
haftmann@30328
   454
qed
haftmann@30328
   455
  
haftmann@30328
   456
lemma bind_code [code]:
wenzelm@62026
   457
  "Seq g \<bind> f = Seq (\<lambda>u. apply f (g ()))"
haftmann@30328
   458
  unfolding Seq_def by (rule sym, rule apply_bind)
haftmann@30328
   459
haftmann@30328
   460
lemma bot_set_code [code]:
haftmann@30328
   461
  "\<bottom> = Seq (\<lambda>u. Empty)"
haftmann@30328
   462
  unfolding Seq_def by simp
haftmann@30328
   463
haftmann@30376
   464
primrec adjunct :: "'a pred \<Rightarrow> 'a seq \<Rightarrow> 'a seq" where
haftmann@44415
   465
  "adjunct P Empty = Join P Empty"
haftmann@44415
   466
| "adjunct P (Insert x Q) = Insert x (Q \<squnion> P)"
haftmann@44415
   467
| "adjunct P (Join Q xq) = Join Q (adjunct P xq)"
haftmann@30376
   468
haftmann@30376
   469
lemma adjunct_sup:
haftmann@30376
   470
  "pred_of_seq (adjunct P xq) = P \<squnion> pred_of_seq xq"
haftmann@30376
   471
  by (induct xq) (simp_all add: sup_assoc sup_commute sup_left_commute)
haftmann@30376
   472
haftmann@30328
   473
lemma sup_code [code]:
haftmann@30328
   474
  "Seq f \<squnion> Seq g = Seq (\<lambda>u. case f ()
haftmann@30328
   475
    of Empty \<Rightarrow> g ()
haftmann@30328
   476
     | Insert x P \<Rightarrow> Insert x (P \<squnion> Seq g)
haftmann@30376
   477
     | Join P xq \<Rightarrow> adjunct (Seq g) (Join P xq))"
haftmann@30328
   478
proof (cases "f ()")
haftmann@30328
   479
  case Empty
haftmann@30328
   480
  thus ?thesis
haftmann@34007
   481
    unfolding Seq_def by (simp add: sup_commute [of "\<bottom>"])
haftmann@30328
   482
next
haftmann@30328
   483
  case Insert
haftmann@30328
   484
  thus ?thesis
haftmann@30328
   485
    unfolding Seq_def by (simp add: sup_assoc)
haftmann@30328
   486
next
haftmann@30328
   487
  case Join
haftmann@30328
   488
  thus ?thesis
haftmann@30376
   489
    unfolding Seq_def
haftmann@30376
   490
    by (simp add: adjunct_sup sup_assoc sup_commute sup_left_commute)
haftmann@30328
   491
qed
haftmann@30328
   492
haftmann@30430
   493
primrec contained :: "'a seq \<Rightarrow> 'a pred \<Rightarrow> bool" where
haftmann@44415
   494
  "contained Empty Q \<longleftrightarrow> True"
haftmann@44415
   495
| "contained (Insert x P) Q \<longleftrightarrow> eval Q x \<and> P \<le> Q"
haftmann@44415
   496
| "contained (Join P xq) Q \<longleftrightarrow> P \<le> Q \<and> contained xq Q"
haftmann@30430
   497
haftmann@30430
   498
lemma single_less_eq_eval:
haftmann@30430
   499
  "single x \<le> P \<longleftrightarrow> eval P x"
haftmann@44415
   500
  by (auto simp add: less_eq_pred_def le_fun_def)
haftmann@30430
   501
haftmann@30430
   502
lemma contained_less_eq:
haftmann@30430
   503
  "contained xq Q \<longleftrightarrow> pred_of_seq xq \<le> Q"
haftmann@30430
   504
  by (induct xq) (simp_all add: single_less_eq_eval)
haftmann@30430
   505
haftmann@30430
   506
lemma less_eq_pred_code [code]:
haftmann@30430
   507
  "Seq f \<le> Q = (case f ()
haftmann@30430
   508
   of Empty \<Rightarrow> True
haftmann@30430
   509
    | Insert x P \<Rightarrow> eval Q x \<and> P \<le> Q
haftmann@30430
   510
    | Join P xq \<Rightarrow> P \<le> Q \<and> contained xq Q)"
haftmann@30430
   511
  by (cases "f ()")
haftmann@30430
   512
    (simp_all add: Seq_def single_less_eq_eval contained_less_eq)
haftmann@30430
   513
haftmann@66012
   514
instantiation pred :: (type) equal
haftmann@66012
   515
begin
haftmann@66012
   516
haftmann@66012
   517
definition equal_pred
haftmann@66012
   518
  where [simp]: "HOL.equal P Q \<longleftrightarrow> P = (Q :: 'a pred)"
haftmann@66012
   519
haftmann@66012
   520
instance by standard simp
haftmann@66012
   521
haftmann@66012
   522
end
haftmann@66012
   523
    
haftmann@66012
   524
lemma [code]:
haftmann@66012
   525
  "HOL.equal P Q \<longleftrightarrow> P \<le> Q \<and> Q \<le> P" for P Q :: "'a pred"
haftmann@66012
   526
  by auto
haftmann@38857
   527
haftmann@38857
   528
lemma [code nbe]:
haftmann@66012
   529
  "HOL.equal P P \<longleftrightarrow> True" for P :: "'a pred"
haftmann@38857
   530
  by (fact equal_refl)
haftmann@30430
   531
haftmann@30430
   532
lemma [code]:
blanchet@55416
   533
  "case_pred f P = f (eval P)"
haftmann@66012
   534
  by (fact pred.case_eq_if)
haftmann@30430
   535
haftmann@30430
   536
lemma [code]:
blanchet@55416
   537
  "rec_pred f P = f (eval P)"
haftmann@30430
   538
  by (cases P) simp
haftmann@30328
   539
bulwahn@31105
   540
inductive eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool" where "eq x x"
bulwahn@31105
   541
bulwahn@31105
   542
lemma eq_is_eq: "eq x y \<equiv> (x = y)"
haftmann@31108
   543
  by (rule eq_reflection) (auto intro: eq.intros elim: eq.cases)
haftmann@30948
   544
haftmann@32578
   545
primrec null :: "'a seq \<Rightarrow> bool" where
haftmann@44415
   546
  "null Empty \<longleftrightarrow> True"
haftmann@44415
   547
| "null (Insert x P) \<longleftrightarrow> False"
haftmann@44415
   548
| "null (Join P xq) \<longleftrightarrow> is_empty P \<and> null xq"
haftmann@32578
   549
haftmann@32578
   550
lemma null_is_empty:
haftmann@32578
   551
  "null xq \<longleftrightarrow> is_empty (pred_of_seq xq)"
haftmann@32578
   552
  by (induct xq) (simp_all add: is_empty_bot not_is_empty_single is_empty_sup)
haftmann@32578
   553
haftmann@32578
   554
lemma is_empty_code [code]:
haftmann@32578
   555
  "is_empty (Seq f) \<longleftrightarrow> null (f ())"
haftmann@32578
   556
  by (simp add: null_is_empty Seq_def)
haftmann@32578
   557
bulwahn@33111
   558
primrec the_only :: "(unit \<Rightarrow> 'a) \<Rightarrow> 'a seq \<Rightarrow> 'a" where
haftmann@66251
   559
  "the_only default Empty = default ()" for default
haftmann@66251
   560
| "the_only default (Insert x P) =
haftmann@66251
   561
    (if is_empty P then x else let y = singleton default P in if x = y then x else default ())" for default
haftmann@66251
   562
| "the_only default (Join P xq) =
wenzelm@60166
   563
    (if is_empty P then the_only default xq else if null xq then singleton default P
wenzelm@60166
   564
       else let x = singleton default P; y = the_only default xq in
haftmann@66251
   565
       if x = y then x else default ())" for default
haftmann@32578
   566
haftmann@32578
   567
lemma the_only_singleton:
haftmann@66251
   568
  "the_only default xq = singleton default (pred_of_seq xq)" for default
haftmann@32578
   569
  by (induct xq)
haftmann@32578
   570
    (auto simp add: singleton_bot singleton_single is_empty_def
haftmann@32578
   571
    null_is_empty Let_def singleton_sup)
haftmann@32578
   572
haftmann@32578
   573
lemma singleton_code [code]:
wenzelm@60166
   574
  "singleton default (Seq f) =
wenzelm@60166
   575
    (case f () of
wenzelm@60166
   576
      Empty \<Rightarrow> default ()
haftmann@32578
   577
    | Insert x P \<Rightarrow> if is_empty P then x
wenzelm@60166
   578
        else let y = singleton default P in
wenzelm@60166
   579
          if x = y then x else default ()
wenzelm@60166
   580
    | Join P xq \<Rightarrow> if is_empty P then the_only default xq
wenzelm@60166
   581
        else if null xq then singleton default P
wenzelm@60166
   582
        else let x = singleton default P; y = the_only default xq in
haftmann@66251
   583
          if x = y then x else default ())" for default
haftmann@32578
   584
  by (cases "f ()")
haftmann@32578
   585
   (auto simp add: Seq_def the_only_singleton is_empty_def
haftmann@32578
   586
      null_is_empty singleton_bot singleton_single singleton_sup Let_def)
haftmann@32578
   587
haftmann@44414
   588
definition the :: "'a pred \<Rightarrow> 'a" where
haftmann@37767
   589
  "the A = (THE x. eval A x)"
bulwahn@33111
   590
haftmann@40674
   591
lemma the_eqI:
haftmann@41080
   592
  "(THE x. eval P x) = x \<Longrightarrow> the P = x"
haftmann@40674
   593
  by (simp add: the_def)
haftmann@40674
   594
Andreas@53943
   595
lemma the_eq [code]: "the A = singleton (\<lambda>x. Code.abort (STR ''not_unique'') (\<lambda>_. the A)) A"
Andreas@53943
   596
  by (rule the_eqI) (simp add: singleton_def the_def)
bulwahn@33110
   597
haftmann@36531
   598
code_reflect Predicate
haftmann@36513
   599
  datatypes pred = Seq and seq = Empty | Insert | Join
haftmann@36513
   600
wenzelm@60758
   601
ML \<open>
haftmann@30948
   602
signature PREDICATE =
haftmann@30948
   603
sig
haftmann@51126
   604
  val anamorph: ('a -> ('b * 'a) option) -> int -> 'a -> 'b list * 'a
haftmann@30948
   605
  datatype 'a pred = Seq of (unit -> 'a seq)
haftmann@30948
   606
  and 'a seq = Empty | Insert of 'a * 'a pred | Join of 'a pred * 'a seq
haftmann@51126
   607
  val map: ('a -> 'b) -> 'a pred -> 'b pred
haftmann@30959
   608
  val yield: 'a pred -> ('a * 'a pred) option
haftmann@30959
   609
  val yieldn: int -> 'a pred -> 'a list * 'a pred
haftmann@30948
   610
end;
haftmann@30948
   611
haftmann@30948
   612
structure Predicate : PREDICATE =
haftmann@30948
   613
struct
haftmann@30948
   614
haftmann@51126
   615
fun anamorph f k x =
haftmann@51126
   616
 (if k = 0 then ([], x)
haftmann@51126
   617
  else case f x
haftmann@51126
   618
   of NONE => ([], x)
haftmann@51126
   619
    | SOME (v, y) => let
haftmann@51126
   620
        val k' = k - 1;
haftmann@51126
   621
        val (vs, z) = anamorph f k' y
haftmann@51126
   622
      in (v :: vs, z) end);
haftmann@51126
   623
haftmann@36513
   624
datatype pred = datatype Predicate.pred
haftmann@36513
   625
datatype seq = datatype Predicate.seq
haftmann@36513
   626
haftmann@51126
   627
fun map f = @{code Predicate.map} f;
haftmann@30959
   628
haftmann@36513
   629
fun yield (Seq f) = next (f ())
haftmann@36513
   630
and next Empty = NONE
haftmann@36513
   631
  | next (Insert (x, P)) = SOME (x, P)
haftmann@36513
   632
  | next (Join (P, xq)) = (case yield P
haftmann@30959
   633
     of NONE => next xq
haftmann@36513
   634
      | SOME (x, Q) => SOME (x, Seq (fn _ => Join (Q, xq))));
haftmann@30959
   635
haftmann@51126
   636
fun yieldn k = anamorph yield k;
haftmann@30948
   637
haftmann@30948
   638
end;
wenzelm@60758
   639
\<close>
haftmann@30948
   640
wenzelm@60758
   641
text \<open>Conversion from and to sets\<close>
haftmann@46038
   642
haftmann@46038
   643
definition pred_of_set :: "'a set \<Rightarrow> 'a pred" where
haftmann@46038
   644
  "pred_of_set = Pred \<circ> (\<lambda>A x. x \<in> A)"
haftmann@46038
   645
haftmann@46038
   646
lemma eval_pred_of_set [simp]:
haftmann@46038
   647
  "eval (pred_of_set A) x \<longleftrightarrow> x \<in>A"
haftmann@46038
   648
  by (simp add: pred_of_set_def)
haftmann@46038
   649
haftmann@46038
   650
definition set_of_pred :: "'a pred \<Rightarrow> 'a set" where
haftmann@46038
   651
  "set_of_pred = Collect \<circ> eval"
haftmann@46038
   652
haftmann@46038
   653
lemma member_set_of_pred [simp]:
haftmann@46038
   654
  "x \<in> set_of_pred P \<longleftrightarrow> Predicate.eval P x"
haftmann@46038
   655
  by (simp add: set_of_pred_def)
haftmann@46038
   656
haftmann@46038
   657
definition set_of_seq :: "'a seq \<Rightarrow> 'a set" where
haftmann@46038
   658
  "set_of_seq = set_of_pred \<circ> pred_of_seq"
haftmann@46038
   659
haftmann@46038
   660
lemma member_set_of_seq [simp]:
haftmann@46038
   661
  "x \<in> set_of_seq xq = Predicate.member xq x"
haftmann@46038
   662
  by (simp add: set_of_seq_def eval_member)
haftmann@46038
   663
haftmann@46038
   664
lemma of_pred_code [code]:
haftmann@46038
   665
  "set_of_pred (Predicate.Seq f) = (case f () of
haftmann@46038
   666
     Predicate.Empty \<Rightarrow> {}
haftmann@46038
   667
   | Predicate.Insert x P \<Rightarrow> insert x (set_of_pred P)
haftmann@46038
   668
   | Predicate.Join P xq \<Rightarrow> set_of_pred P \<union> set_of_seq xq)"
haftmann@46038
   669
  by (auto split: seq.split simp add: eval_code)
haftmann@46038
   670
haftmann@46038
   671
lemma of_seq_code [code]:
haftmann@46038
   672
  "set_of_seq Predicate.Empty = {}"
haftmann@46038
   673
  "set_of_seq (Predicate.Insert x P) = insert x (set_of_pred P)"
haftmann@46038
   674
  "set_of_seq (Predicate.Join P xq) = set_of_pred P \<union> set_of_seq xq"
haftmann@46038
   675
  by auto
haftmann@46038
   676
wenzelm@60758
   677
text \<open>Lazy Evaluation of an indexed function\<close>
haftmann@46664
   678
haftmann@51143
   679
function iterate_upto :: "(natural \<Rightarrow> 'a) \<Rightarrow> natural \<Rightarrow> natural \<Rightarrow> 'a Predicate.pred"
haftmann@46664
   680
where
haftmann@46664
   681
  "iterate_upto f n m =
haftmann@46664
   682
    Predicate.Seq (%u. if n > m then Predicate.Empty
haftmann@46664
   683
     else Predicate.Insert (f n) (iterate_upto f (n + 1) m))"
haftmann@46664
   684
by pat_completeness auto
haftmann@46664
   685
haftmann@51143
   686
termination by (relation "measure (%(f, n, m). nat_of_natural (m + 1 - n))")
haftmann@51143
   687
  (auto simp add: less_natural_def)
haftmann@46664
   688
wenzelm@60758
   689
text \<open>Misc\<close>
haftmann@46664
   690
haftmann@47399
   691
declare Inf_set_fold [where 'a = "'a Predicate.pred", code]
haftmann@47399
   692
declare Sup_set_fold [where 'a = "'a Predicate.pred", code]
haftmann@46664
   693
haftmann@46664
   694
(* FIXME: better implement conversion by bisection *)
haftmann@46664
   695
haftmann@46664
   696
lemma pred_of_set_fold_sup:
haftmann@46664
   697
  assumes "finite A"
haftmann@46664
   698
  shows "pred_of_set A = Finite_Set.fold sup bot (Predicate.single ` A)" (is "?lhs = ?rhs")
haftmann@46664
   699
proof (rule sym)
haftmann@46664
   700
  interpret comp_fun_idem "sup :: 'a Predicate.pred \<Rightarrow> 'a Predicate.pred \<Rightarrow> 'a Predicate.pred"
haftmann@46664
   701
    by (fact comp_fun_idem_sup)
wenzelm@60758
   702
  from \<open>finite A\<close> show "?rhs = ?lhs" by (induct A) (auto intro!: pred_eqI)
haftmann@46664
   703
qed
haftmann@46664
   704
haftmann@46664
   705
lemma pred_of_set_set_fold_sup:
haftmann@46664
   706
  "pred_of_set (set xs) = fold sup (List.map Predicate.single xs) bot"
haftmann@46664
   707
proof -
haftmann@46664
   708
  interpret comp_fun_idem "sup :: 'a Predicate.pred \<Rightarrow> 'a Predicate.pred \<Rightarrow> 'a Predicate.pred"
haftmann@46664
   709
    by (fact comp_fun_idem_sup)
haftmann@46664
   710
  show ?thesis by (simp add: pred_of_set_fold_sup fold_set_fold [symmetric])
haftmann@46664
   711
qed
haftmann@46664
   712
haftmann@46664
   713
lemma pred_of_set_set_foldr_sup [code]:
haftmann@46664
   714
  "pred_of_set (set xs) = foldr sup (List.map Predicate.single xs) bot"
haftmann@46664
   715
  by (simp add: pred_of_set_set_fold_sup ac_simps foldr_fold fun_eq_iff)
haftmann@46664
   716
haftmann@30328
   717
no_notation
wenzelm@62026
   718
  bind (infixl "\<bind>" 70)
haftmann@30328
   719
wenzelm@36176
   720
hide_type (open) pred seq
wenzelm@36176
   721
hide_const (open) Pred eval single bind is_empty singleton if_pred not_pred holds
Andreas@53943
   722
  Empty Insert Join Seq member pred_of_seq "apply" adjunct null the_only eq map the
haftmann@46664
   723
  iterate_upto
haftmann@46664
   724
hide_fact (open) null_def member_def
haftmann@30328
   725
haftmann@30328
   726
end