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