src/HOL/Library/Predicate_Compile_Alternative_Defs.thy
author haftmann
Mon Jun 28 15:03:07 2010 +0200 (2010-06-28)
changeset 37591 d3daea901123
parent 36253 6e969ce3dfcc
child 39198 f967a16dfcdd
permissions -rw-r--r--
merged constants "split" and "prod_case"
bulwahn@35953
     1
theory Predicate_Compile_Alternative_Defs
bulwahn@36053
     2
imports Main
bulwahn@35953
     3
begin
bulwahn@35953
     4
bulwahn@35953
     5
section {* Common constants *}
bulwahn@35953
     6
bulwahn@35953
     7
declare HOL.if_bool_eq_disj[code_pred_inline]
bulwahn@35953
     8
bulwahn@36253
     9
declare bool_diff_def[code_pred_inline]
bulwahn@36253
    10
declare inf_bool_eq_raw[code_pred_inline]
bulwahn@36253
    11
declare less_bool_def_raw[code_pred_inline]
bulwahn@36253
    12
declare le_bool_def_raw[code_pred_inline]
bulwahn@36253
    13
bulwahn@36253
    14
lemma min_bool_eq [code_pred_inline]: "(min :: bool => bool => bool) == (op &)"
bulwahn@36253
    15
by (rule eq_reflection) (auto simp add: expand_fun_eq min_def le_bool_def)
bulwahn@36253
    16
bulwahn@35953
    17
setup {* Predicate_Compile_Data.ignore_consts [@{const_name Let}] *}
bulwahn@35953
    18
bulwahn@35953
    19
section {* Pairs *}
bulwahn@35953
    20
haftmann@37591
    21
setup {* Predicate_Compile_Data.ignore_consts [@{const_name fst}, @{const_name snd}, @{const_name prod_case}] *}
bulwahn@35953
    22
bulwahn@35953
    23
section {* Bounded quantifiers *}
bulwahn@35953
    24
bulwahn@35953
    25
declare Ball_def[code_pred_inline]
bulwahn@35953
    26
declare Bex_def[code_pred_inline]
bulwahn@35953
    27
bulwahn@35953
    28
section {* Set operations *}
bulwahn@35953
    29
bulwahn@35953
    30
declare Collect_def[code_pred_inline]
bulwahn@35953
    31
declare mem_def[code_pred_inline]
bulwahn@35953
    32
bulwahn@35953
    33
declare eq_reflection[OF empty_def, code_pred_inline]
bulwahn@35953
    34
declare insert_code[code_pred_def]
bulwahn@35953
    35
bulwahn@35953
    36
declare subset_iff[code_pred_inline]
bulwahn@35953
    37
bulwahn@35953
    38
declare Int_def[code_pred_inline]
bulwahn@35953
    39
declare eq_reflection[OF Un_def, code_pred_inline]
bulwahn@35953
    40
declare eq_reflection[OF UNION_def, code_pred_inline]
bulwahn@35953
    41
bulwahn@35953
    42
lemma Diff[code_pred_inline]:
bulwahn@35953
    43
  "(A - B) = (%x. A x \<and> \<not> B x)"
bulwahn@35953
    44
by (auto simp add: mem_def)
bulwahn@35953
    45
bulwahn@36253
    46
lemma subset_eq[code_pred_inline]:
bulwahn@36253
    47
  "(P :: 'a => bool) < (Q :: 'a => bool) == ((\<exists>x. Q x \<and> (\<not> P x)) \<and> (\<forall> x. P x --> Q x))"
bulwahn@36253
    48
by (rule eq_reflection) (fastsimp simp add: mem_def)
bulwahn@36253
    49
bulwahn@35953
    50
lemma set_equality[code_pred_inline]:
bulwahn@35953
    51
  "(A = B) = ((\<forall>x. A x \<longrightarrow> B x) \<and> (\<forall>x. B x \<longrightarrow> A x))"
bulwahn@35953
    52
by (fastsimp simp add: mem_def)
bulwahn@35953
    53
bulwahn@35953
    54
section {* Setup for Numerals *}
bulwahn@35953
    55
bulwahn@35953
    56
setup {* Predicate_Compile_Data.ignore_consts [@{const_name number_of}] *}
bulwahn@35953
    57
setup {* Predicate_Compile_Data.keep_functions [@{const_name number_of}] *}
bulwahn@35953
    58
bulwahn@35953
    59
setup {* Predicate_Compile_Data.ignore_consts [@{const_name div}, @{const_name mod}, @{const_name times}] *}
bulwahn@35953
    60
bulwahn@36053
    61
section {* Arithmetic operations *}
bulwahn@36053
    62
bulwahn@36053
    63
subsection {* Arithmetic on naturals and integers *}
bulwahn@36053
    64
bulwahn@36053
    65
definition plus_eq_nat :: "nat => nat => nat => bool"
bulwahn@36053
    66
where
bulwahn@36053
    67
  "plus_eq_nat x y z = (x + y = z)"
bulwahn@35953
    68
bulwahn@36053
    69
definition minus_eq_nat :: "nat => nat => nat => bool"
bulwahn@36053
    70
where
bulwahn@36053
    71
  "minus_eq_nat x y z = (x - y = z)"
bulwahn@36053
    72
bulwahn@36053
    73
definition plus_eq_int :: "int => int => int => bool"
bulwahn@36053
    74
where
bulwahn@36053
    75
  "plus_eq_int x y z = (x + y = z)"
bulwahn@36053
    76
bulwahn@36053
    77
definition minus_eq_int :: "int => int => int => bool"
bulwahn@36053
    78
where
bulwahn@36053
    79
  "minus_eq_int x y z = (x - y = z)"
bulwahn@36053
    80
bulwahn@36053
    81
definition subtract
bulwahn@35953
    82
where
bulwahn@36053
    83
  [code_inline]: "subtract x y = y - x"
bulwahn@35953
    84
bulwahn@36053
    85
setup {*
bulwahn@36053
    86
let
bulwahn@36053
    87
  val Fun = Predicate_Compile_Aux.Fun
bulwahn@36053
    88
  val Input = Predicate_Compile_Aux.Input
bulwahn@36053
    89
  val Output = Predicate_Compile_Aux.Output
bulwahn@36053
    90
  val Bool = Predicate_Compile_Aux.Bool
bulwahn@36053
    91
  val iio = Fun (Input, Fun (Input, Fun (Output, Bool)))
bulwahn@36053
    92
  val ioi = Fun (Input, Fun (Output, Fun (Input, Bool)))
bulwahn@36053
    93
  val oii = Fun (Output, Fun (Input, Fun (Input, Bool)))
bulwahn@36053
    94
  val ooi = Fun (Output, Fun (Output, Fun (Input, Bool)))
bulwahn@36053
    95
  val plus_nat = Predicate_Compile_Core.functional_compilation @{const_name plus} iio
bulwahn@36053
    96
  val minus_nat = Predicate_Compile_Core.functional_compilation @{const_name "minus"} iio
bulwahn@36053
    97
  fun subtract_nat compfuns (_ : typ) =
bulwahn@36053
    98
    let
bulwahn@36053
    99
      val T = Predicate_Compile_Aux.mk_predT compfuns @{typ nat}
bulwahn@36053
   100
    in
bulwahn@36053
   101
      absdummy (@{typ nat}, absdummy (@{typ nat},
bulwahn@36053
   102
        Const (@{const_name "If"}, @{typ bool} --> T --> T --> T) $
bulwahn@36053
   103
          (@{term "op > :: nat => nat => bool"} $ Bound 1 $ Bound 0) $
bulwahn@36053
   104
          Predicate_Compile_Aux.mk_bot compfuns @{typ nat} $
bulwahn@36053
   105
          Predicate_Compile_Aux.mk_single compfuns
bulwahn@36053
   106
          (@{term "op - :: nat => nat => nat"} $ Bound 0 $ Bound 1)))
bulwahn@36053
   107
    end
bulwahn@36053
   108
  fun enumerate_addups_nat compfuns (_ : typ) =
bulwahn@36053
   109
    absdummy (@{typ nat}, Predicate_Compile_Aux.mk_iterate_upto compfuns @{typ "nat * nat"}
bulwahn@36053
   110
    (absdummy (@{typ code_numeral}, @{term "Pair :: nat => nat => nat * nat"} $
bulwahn@36053
   111
      (@{term "Code_Numeral.nat_of"} $ Bound 0) $
bulwahn@36053
   112
      (@{term "op - :: nat => nat => nat"} $ Bound 1 $ (@{term "Code_Numeral.nat_of"} $ Bound 0))),
bulwahn@36053
   113
      @{term "0 :: code_numeral"}, @{term "Code_Numeral.of_nat"} $ Bound 0))
bulwahn@36053
   114
  fun enumerate_nats compfuns  (_ : typ) =
bulwahn@36053
   115
    let
bulwahn@36053
   116
      val (single_const, _) = strip_comb (Predicate_Compile_Aux.mk_single compfuns @{term "0 :: nat"})
bulwahn@36053
   117
      val T = Predicate_Compile_Aux.mk_predT compfuns @{typ nat}
bulwahn@36053
   118
    in
bulwahn@36053
   119
      absdummy(@{typ nat}, absdummy (@{typ nat},
bulwahn@36053
   120
        Const (@{const_name If}, @{typ bool} --> T --> T --> T) $
bulwahn@36053
   121
          (@{term "op = :: nat => nat => bool"} $ Bound 0 $ @{term "0::nat"}) $
bulwahn@36053
   122
          (Predicate_Compile_Aux.mk_iterate_upto compfuns @{typ nat} (@{term "Code_Numeral.nat_of"},
bulwahn@36053
   123
            @{term "0::code_numeral"}, @{term "Code_Numeral.of_nat"} $ Bound 1)) $
bulwahn@36053
   124
            (single_const $ (@{term "op + :: nat => nat => nat"} $ Bound 1 $ Bound 0))))
bulwahn@36053
   125
    end
bulwahn@36053
   126
in
bulwahn@36053
   127
  Predicate_Compile_Core.force_modes_and_compilations @{const_name plus_eq_nat}
bulwahn@36053
   128
    [(iio, (plus_nat, false)), (oii, (subtract_nat, false)), (ioi, (subtract_nat, false)),
bulwahn@36053
   129
     (ooi, (enumerate_addups_nat, false))]
bulwahn@36053
   130
  #> Predicate_Compile_Fun.add_function_predicate_translation
bulwahn@36053
   131
       (@{term "plus :: nat => nat => nat"}, @{term "plus_eq_nat"})
bulwahn@36053
   132
  #> Predicate_Compile_Core.force_modes_and_compilations @{const_name minus_eq_nat}
bulwahn@36053
   133
       [(iio, (minus_nat, false)), (oii, (enumerate_nats, false))]
bulwahn@36053
   134
  #> Predicate_Compile_Fun.add_function_predicate_translation
bulwahn@36053
   135
      (@{term "minus :: nat => nat => nat"}, @{term "minus_eq_nat"})
bulwahn@36053
   136
  #> Predicate_Compile_Core.force_modes_and_functions @{const_name plus_eq_int}
bulwahn@36053
   137
    [(iio, (@{const_name plus}, false)), (ioi, (@{const_name subtract}, false)),
bulwahn@36053
   138
     (oii, (@{const_name subtract}, false))]
bulwahn@36053
   139
  #> Predicate_Compile_Fun.add_function_predicate_translation
bulwahn@36053
   140
       (@{term "plus :: int => int => int"}, @{term "plus_eq_int"})
bulwahn@36053
   141
  #> Predicate_Compile_Core.force_modes_and_functions @{const_name minus_eq_int}
bulwahn@36053
   142
    [(iio, (@{const_name minus}, false)), (oii, (@{const_name plus}, false)),
bulwahn@36053
   143
     (ioi, (@{const_name minus}, false))]
bulwahn@36053
   144
  #> Predicate_Compile_Fun.add_function_predicate_translation
bulwahn@36053
   145
      (@{term "minus :: int => int => int"}, @{term "minus_eq_int"})
bulwahn@36053
   146
end
bulwahn@36053
   147
*}
bulwahn@36053
   148
bulwahn@36053
   149
subsection {* Inductive definitions for ordering on naturals *}
bulwahn@35953
   150
bulwahn@35953
   151
inductive less_nat
bulwahn@35953
   152
where
bulwahn@35953
   153
  "less_nat 0 (Suc y)"
bulwahn@35953
   154
| "less_nat x y ==> less_nat (Suc x) (Suc y)"
bulwahn@35953
   155
bulwahn@36246
   156
lemma less_nat[code_pred_inline]:
bulwahn@35953
   157
  "x < y = less_nat x y"
bulwahn@35953
   158
apply (rule iffI)
bulwahn@35953
   159
apply (induct x arbitrary: y)
bulwahn@35953
   160
apply (case_tac y) apply (auto intro: less_nat.intros)
bulwahn@35953
   161
apply (case_tac y)
bulwahn@35953
   162
apply (auto intro: less_nat.intros)
bulwahn@35953
   163
apply (induct rule: less_nat.induct)
bulwahn@35953
   164
apply auto
bulwahn@35953
   165
done
bulwahn@35953
   166
bulwahn@35953
   167
inductive less_eq_nat
bulwahn@35953
   168
where
bulwahn@35953
   169
  "less_eq_nat 0 y"
bulwahn@35953
   170
| "less_eq_nat x y ==> less_eq_nat (Suc x) (Suc y)"
bulwahn@35953
   171
bulwahn@35953
   172
lemma [code_pred_inline]:
bulwahn@35953
   173
"x <= y = less_eq_nat x y"
bulwahn@35953
   174
apply (rule iffI)
bulwahn@35953
   175
apply (induct x arbitrary: y)
bulwahn@35953
   176
apply (auto intro: less_eq_nat.intros)
bulwahn@35953
   177
apply (case_tac y) apply (auto intro: less_eq_nat.intros)
bulwahn@35953
   178
apply (induct rule: less_eq_nat.induct)
bulwahn@35953
   179
apply auto done
bulwahn@35953
   180
bulwahn@35953
   181
section {* Alternative list definitions *}
bulwahn@35953
   182
bulwahn@36053
   183
subsection {* Alternative rules for length *}
bulwahn@36053
   184
bulwahn@36053
   185
definition size_list :: "'a list => nat"
bulwahn@36053
   186
where "size_list = size"
bulwahn@36053
   187
bulwahn@36053
   188
lemma size_list_simps:
bulwahn@36053
   189
  "size_list [] = 0"
bulwahn@36053
   190
  "size_list (x # xs) = Suc (size_list xs)"
bulwahn@36053
   191
by (auto simp add: size_list_def)
bulwahn@36053
   192
bulwahn@36053
   193
declare size_list_simps[code_pred_def]
bulwahn@36053
   194
declare size_list_def[symmetric, code_pred_inline]
bulwahn@35953
   195
bulwahn@35953
   196
subsection {* Alternative rules for set *}
bulwahn@35953
   197
bulwahn@35953
   198
lemma set_ConsI1 [code_pred_intro]:
bulwahn@35953
   199
  "set (x # xs) x"
bulwahn@35953
   200
unfolding mem_def[symmetric, of _ x]
bulwahn@35953
   201
by auto
bulwahn@35953
   202
bulwahn@35953
   203
lemma set_ConsI2 [code_pred_intro]:
bulwahn@35953
   204
  "set xs x ==> set (x' # xs) x" 
bulwahn@35953
   205
unfolding mem_def[symmetric, of _ x]
bulwahn@35953
   206
by auto
bulwahn@35953
   207
bulwahn@35953
   208
code_pred [skip_proof] set
bulwahn@35953
   209
proof -
bulwahn@35953
   210
  case set
bulwahn@35953
   211
  from this show thesis
bulwahn@35953
   212
    apply (case_tac xb)
bulwahn@35953
   213
    apply auto
bulwahn@35953
   214
    unfolding mem_def[symmetric, of _ xc]
bulwahn@35953
   215
    apply auto
bulwahn@35953
   216
    unfolding mem_def
bulwahn@35953
   217
    apply fastsimp
bulwahn@35953
   218
    done
bulwahn@35953
   219
qed
bulwahn@35953
   220
bulwahn@35953
   221
subsection {* Alternative rules for list_all2 *}
bulwahn@35953
   222
bulwahn@35953
   223
lemma list_all2_NilI [code_pred_intro]: "list_all2 P [] []"
bulwahn@35953
   224
by auto
bulwahn@35953
   225
bulwahn@35953
   226
lemma list_all2_ConsI [code_pred_intro]: "list_all2 P xs ys ==> P x y ==> list_all2 P (x#xs) (y#ys)"
bulwahn@35953
   227
by auto
bulwahn@35953
   228
bulwahn@35953
   229
code_pred [skip_proof] list_all2
bulwahn@35953
   230
proof -
bulwahn@35953
   231
  case list_all2
bulwahn@35953
   232
  from this show thesis
bulwahn@35953
   233
    apply -
bulwahn@35953
   234
    apply (case_tac xb)
bulwahn@35953
   235
    apply (case_tac xc)
bulwahn@35953
   236
    apply auto
bulwahn@35953
   237
    apply (case_tac xc)
bulwahn@35953
   238
    apply auto
bulwahn@35953
   239
    apply fastsimp
bulwahn@35953
   240
    done
bulwahn@35953
   241
qed
bulwahn@35953
   242
bulwahn@36246
   243
section {* Simplification rules for optimisation *}
bulwahn@36246
   244
bulwahn@36246
   245
lemma [code_pred_simp]: "\<not> False == True"
bulwahn@36246
   246
by auto
bulwahn@36246
   247
bulwahn@36246
   248
lemma [code_pred_simp]: "\<not> True == False"
bulwahn@36246
   249
by auto
bulwahn@36246
   250
bulwahn@36246
   251
lemma less_nat_k_0 [code_pred_simp]: "less_nat k 0 == False"
bulwahn@36246
   252
unfolding less_nat[symmetric] by auto
bulwahn@35953
   253
bulwahn@35953
   254
bulwahn@35953
   255
end