src/HOL/Library/Predicate_Compile_Alternative_Defs.thy
author wenzelm
Wed Sep 12 13:42:28 2012 +0200 (2012-09-12)
changeset 49322 fbb320d02420
parent 48640 053cc8dfde35
child 51143 0a2371e7ced3
permissions -rw-r--r--
tuned headers;
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]
wenzelm@46905
    10
declare inf_bool_def[abs_def, code_pred_inline]
wenzelm@46905
    11
declare less_bool_def[abs_def, code_pred_inline]
wenzelm@46905
    12
declare le_bool_def[abs_def, code_pred_inline]
bulwahn@36253
    13
bulwahn@36253
    14
lemma min_bool_eq [code_pred_inline]: "(min :: bool => bool => bool) == (op &)"
haftmann@45970
    15
by (rule eq_reflection) (auto simp add: fun_eq_iff min_def)
bulwahn@36253
    16
bulwahn@39650
    17
lemma [code_pred_inline]: 
bulwahn@39650
    18
  "((A::bool) ~= (B::bool)) = ((A & ~ B) | (B & ~ A))"
bulwahn@39650
    19
by fast
bulwahn@39650
    20
bulwahn@35953
    21
setup {* Predicate_Compile_Data.ignore_consts [@{const_name Let}] *}
bulwahn@35953
    22
bulwahn@35953
    23
section {* Pairs *}
bulwahn@35953
    24
haftmann@37591
    25
setup {* Predicate_Compile_Data.ignore_consts [@{const_name fst}, @{const_name snd}, @{const_name prod_case}] *}
bulwahn@35953
    26
bulwahn@35953
    27
section {* Bounded quantifiers *}
bulwahn@35953
    28
bulwahn@35953
    29
declare Ball_def[code_pred_inline]
bulwahn@35953
    30
declare Bex_def[code_pred_inline]
bulwahn@35953
    31
bulwahn@47840
    32
section {* Operations on Predicates *}
bulwahn@35953
    33
bulwahn@35953
    34
lemma Diff[code_pred_inline]:
bulwahn@35953
    35
  "(A - B) = (%x. A x \<and> \<not> B x)"
noschinl@46884
    36
  by (simp add: fun_eq_iff)
bulwahn@35953
    37
bulwahn@36253
    38
lemma subset_eq[code_pred_inline]:
bulwahn@36253
    39
  "(P :: 'a => bool) < (Q :: 'a => bool) == ((\<exists>x. Q x \<and> (\<not> P x)) \<and> (\<forall> x. P x --> Q x))"
haftmann@45970
    40
  by (rule eq_reflection) (auto simp add: less_fun_def le_fun_def)
bulwahn@36253
    41
bulwahn@35953
    42
lemma set_equality[code_pred_inline]:
haftmann@45970
    43
  "A = B \<longleftrightarrow> (\<forall>x. A x \<longrightarrow> B x) \<and> (\<forall>x. B x \<longrightarrow> A x)"
haftmann@45970
    44
  by (auto simp add: fun_eq_iff)
haftmann@45970
    45
bulwahn@35953
    46
section {* Setup for Numerals *}
bulwahn@35953
    47
huffman@47108
    48
setup {* Predicate_Compile_Data.ignore_consts [@{const_name numeral}, @{const_name neg_numeral}] *}
huffman@47108
    49
setup {* Predicate_Compile_Data.keep_functions [@{const_name numeral}, @{const_name neg_numeral}] *}
bulwahn@35953
    50
bulwahn@35953
    51
setup {* Predicate_Compile_Data.ignore_consts [@{const_name div}, @{const_name mod}, @{const_name times}] *}
bulwahn@35953
    52
bulwahn@36053
    53
section {* Arithmetic operations *}
bulwahn@36053
    54
bulwahn@36053
    55
subsection {* Arithmetic on naturals and integers *}
bulwahn@36053
    56
bulwahn@36053
    57
definition plus_eq_nat :: "nat => nat => nat => bool"
bulwahn@36053
    58
where
bulwahn@36053
    59
  "plus_eq_nat x y z = (x + y = z)"
bulwahn@35953
    60
bulwahn@36053
    61
definition minus_eq_nat :: "nat => nat => nat => bool"
bulwahn@36053
    62
where
bulwahn@36053
    63
  "minus_eq_nat x y z = (x - y = z)"
bulwahn@36053
    64
bulwahn@36053
    65
definition plus_eq_int :: "int => int => int => bool"
bulwahn@36053
    66
where
bulwahn@36053
    67
  "plus_eq_int x y z = (x + y = z)"
bulwahn@36053
    68
bulwahn@36053
    69
definition minus_eq_int :: "int => int => int => bool"
bulwahn@36053
    70
where
bulwahn@36053
    71
  "minus_eq_int x y z = (x - y = z)"
bulwahn@36053
    72
bulwahn@36053
    73
definition subtract
bulwahn@35953
    74
where
bulwahn@45231
    75
  [code_unfold]: "subtract x y = y - x"
bulwahn@35953
    76
bulwahn@36053
    77
setup {*
bulwahn@36053
    78
let
bulwahn@36053
    79
  val Fun = Predicate_Compile_Aux.Fun
bulwahn@36053
    80
  val Input = Predicate_Compile_Aux.Input
bulwahn@36053
    81
  val Output = Predicate_Compile_Aux.Output
bulwahn@36053
    82
  val Bool = Predicate_Compile_Aux.Bool
bulwahn@36053
    83
  val iio = Fun (Input, Fun (Input, Fun (Output, Bool)))
bulwahn@36053
    84
  val ioi = Fun (Input, Fun (Output, Fun (Input, Bool)))
bulwahn@36053
    85
  val oii = Fun (Output, Fun (Input, Fun (Input, Bool)))
bulwahn@36053
    86
  val ooi = Fun (Output, Fun (Output, Fun (Input, Bool)))
bulwahn@40054
    87
  val plus_nat = Core_Data.functional_compilation @{const_name plus} iio
bulwahn@40054
    88
  val minus_nat = Core_Data.functional_compilation @{const_name "minus"} iio
bulwahn@36053
    89
  fun subtract_nat compfuns (_ : typ) =
bulwahn@36053
    90
    let
bulwahn@45461
    91
      val T = Predicate_Compile_Aux.mk_monadT compfuns @{typ nat}
bulwahn@36053
    92
    in
wenzelm@44241
    93
      absdummy @{typ nat} (absdummy @{typ nat}
wenzelm@44241
    94
        (Const (@{const_name "If"}, @{typ bool} --> T --> T --> T) $
bulwahn@36053
    95
          (@{term "op > :: nat => nat => bool"} $ Bound 1 $ Bound 0) $
bulwahn@45461
    96
          Predicate_Compile_Aux.mk_empty compfuns @{typ nat} $
bulwahn@36053
    97
          Predicate_Compile_Aux.mk_single compfuns
bulwahn@36053
    98
          (@{term "op - :: nat => nat => nat"} $ Bound 0 $ Bound 1)))
bulwahn@36053
    99
    end
bulwahn@36053
   100
  fun enumerate_addups_nat compfuns (_ : typ) =
wenzelm@44241
   101
    absdummy @{typ nat} (Predicate_Compile_Aux.mk_iterate_upto compfuns @{typ "nat * nat"}
wenzelm@44241
   102
    (absdummy @{typ code_numeral} (@{term "Pair :: nat => nat => nat * nat"} $
bulwahn@36053
   103
      (@{term "Code_Numeral.nat_of"} $ Bound 0) $
bulwahn@36053
   104
      (@{term "op - :: nat => nat => nat"} $ Bound 1 $ (@{term "Code_Numeral.nat_of"} $ Bound 0))),
bulwahn@36053
   105
      @{term "0 :: code_numeral"}, @{term "Code_Numeral.of_nat"} $ Bound 0))
bulwahn@36053
   106
  fun enumerate_nats compfuns  (_ : typ) =
bulwahn@36053
   107
    let
bulwahn@36053
   108
      val (single_const, _) = strip_comb (Predicate_Compile_Aux.mk_single compfuns @{term "0 :: nat"})
bulwahn@45461
   109
      val T = Predicate_Compile_Aux.mk_monadT compfuns @{typ nat}
bulwahn@36053
   110
    in
wenzelm@44241
   111
      absdummy @{typ nat} (absdummy @{typ nat}
wenzelm@44241
   112
        (Const (@{const_name If}, @{typ bool} --> T --> T --> T) $
bulwahn@36053
   113
          (@{term "op = :: nat => nat => bool"} $ Bound 0 $ @{term "0::nat"}) $
bulwahn@36053
   114
          (Predicate_Compile_Aux.mk_iterate_upto compfuns @{typ nat} (@{term "Code_Numeral.nat_of"},
bulwahn@36053
   115
            @{term "0::code_numeral"}, @{term "Code_Numeral.of_nat"} $ Bound 1)) $
bulwahn@36053
   116
            (single_const $ (@{term "op + :: nat => nat => nat"} $ Bound 1 $ Bound 0))))
bulwahn@36053
   117
    end
bulwahn@36053
   118
in
bulwahn@40054
   119
  Core_Data.force_modes_and_compilations @{const_name plus_eq_nat}
bulwahn@36053
   120
    [(iio, (plus_nat, false)), (oii, (subtract_nat, false)), (ioi, (subtract_nat, false)),
bulwahn@36053
   121
     (ooi, (enumerate_addups_nat, false))]
bulwahn@36053
   122
  #> Predicate_Compile_Fun.add_function_predicate_translation
bulwahn@36053
   123
       (@{term "plus :: nat => nat => nat"}, @{term "plus_eq_nat"})
bulwahn@40054
   124
  #> Core_Data.force_modes_and_compilations @{const_name minus_eq_nat}
bulwahn@36053
   125
       [(iio, (minus_nat, false)), (oii, (enumerate_nats, false))]
bulwahn@36053
   126
  #> Predicate_Compile_Fun.add_function_predicate_translation
bulwahn@36053
   127
      (@{term "minus :: nat => nat => nat"}, @{term "minus_eq_nat"})
bulwahn@40054
   128
  #> Core_Data.force_modes_and_functions @{const_name plus_eq_int}
bulwahn@36053
   129
    [(iio, (@{const_name plus}, false)), (ioi, (@{const_name subtract}, false)),
bulwahn@36053
   130
     (oii, (@{const_name subtract}, false))]
bulwahn@36053
   131
  #> Predicate_Compile_Fun.add_function_predicate_translation
bulwahn@36053
   132
       (@{term "plus :: int => int => int"}, @{term "plus_eq_int"})
bulwahn@40054
   133
  #> Core_Data.force_modes_and_functions @{const_name minus_eq_int}
bulwahn@36053
   134
    [(iio, (@{const_name minus}, false)), (oii, (@{const_name plus}, false)),
bulwahn@36053
   135
     (ioi, (@{const_name minus}, false))]
bulwahn@36053
   136
  #> Predicate_Compile_Fun.add_function_predicate_translation
bulwahn@36053
   137
      (@{term "minus :: int => int => int"}, @{term "minus_eq_int"})
bulwahn@36053
   138
end
bulwahn@36053
   139
*}
bulwahn@36053
   140
bulwahn@36053
   141
subsection {* Inductive definitions for ordering on naturals *}
bulwahn@35953
   142
bulwahn@35953
   143
inductive less_nat
bulwahn@35953
   144
where
bulwahn@35953
   145
  "less_nat 0 (Suc y)"
bulwahn@35953
   146
| "less_nat x y ==> less_nat (Suc x) (Suc y)"
bulwahn@35953
   147
bulwahn@36246
   148
lemma less_nat[code_pred_inline]:
bulwahn@35953
   149
  "x < y = less_nat x y"
bulwahn@35953
   150
apply (rule iffI)
bulwahn@35953
   151
apply (induct x arbitrary: y)
bulwahn@35953
   152
apply (case_tac y) apply (auto intro: less_nat.intros)
bulwahn@35953
   153
apply (case_tac y)
bulwahn@35953
   154
apply (auto intro: less_nat.intros)
bulwahn@35953
   155
apply (induct rule: less_nat.induct)
bulwahn@35953
   156
apply auto
bulwahn@35953
   157
done
bulwahn@35953
   158
bulwahn@35953
   159
inductive less_eq_nat
bulwahn@35953
   160
where
bulwahn@35953
   161
  "less_eq_nat 0 y"
bulwahn@35953
   162
| "less_eq_nat x y ==> less_eq_nat (Suc x) (Suc y)"
bulwahn@35953
   163
bulwahn@35953
   164
lemma [code_pred_inline]:
bulwahn@35953
   165
"x <= y = less_eq_nat x y"
bulwahn@35953
   166
apply (rule iffI)
bulwahn@35953
   167
apply (induct x arbitrary: y)
bulwahn@35953
   168
apply (auto intro: less_eq_nat.intros)
bulwahn@35953
   169
apply (case_tac y) apply (auto intro: less_eq_nat.intros)
bulwahn@35953
   170
apply (induct rule: less_eq_nat.induct)
bulwahn@35953
   171
apply auto done
bulwahn@35953
   172
bulwahn@35953
   173
section {* Alternative list definitions *}
bulwahn@35953
   174
wenzelm@48640
   175
subsection {* Alternative rules for @{text length} *}
bulwahn@36053
   176
bulwahn@36053
   177
definition size_list :: "'a list => nat"
bulwahn@36053
   178
where "size_list = size"
bulwahn@36053
   179
bulwahn@36053
   180
lemma size_list_simps:
bulwahn@36053
   181
  "size_list [] = 0"
bulwahn@36053
   182
  "size_list (x # xs) = Suc (size_list xs)"
bulwahn@36053
   183
by (auto simp add: size_list_def)
bulwahn@36053
   184
bulwahn@36053
   185
declare size_list_simps[code_pred_def]
bulwahn@36053
   186
declare size_list_def[symmetric, code_pred_inline]
bulwahn@35953
   187
bulwahn@35953
   188
wenzelm@48640
   189
subsection {* Alternative rules for @{text list_all2} *}
bulwahn@35953
   190
bulwahn@35953
   191
lemma list_all2_NilI [code_pred_intro]: "list_all2 P [] []"
bulwahn@35953
   192
by auto
bulwahn@35953
   193
bulwahn@35953
   194
lemma list_all2_ConsI [code_pred_intro]: "list_all2 P xs ys ==> P x y ==> list_all2 P (x#xs) (y#ys)"
bulwahn@35953
   195
by auto
bulwahn@35953
   196
bulwahn@35953
   197
code_pred [skip_proof] list_all2
bulwahn@35953
   198
proof -
bulwahn@35953
   199
  case list_all2
bulwahn@35953
   200
  from this show thesis
bulwahn@35953
   201
    apply -
bulwahn@35953
   202
    apply (case_tac xb)
bulwahn@35953
   203
    apply (case_tac xc)
bulwahn@35953
   204
    apply auto
bulwahn@35953
   205
    apply (case_tac xc)
bulwahn@35953
   206
    apply auto
nipkow@44890
   207
    apply fastforce
bulwahn@35953
   208
    done
bulwahn@35953
   209
qed
bulwahn@35953
   210
bulwahn@40548
   211
section {* Setup for String.literal *}
bulwahn@40548
   212
bulwahn@40548
   213
setup {* Predicate_Compile_Data.ignore_consts [@{const_name "STR"}] *}
bulwahn@40548
   214
bulwahn@36246
   215
section {* Simplification rules for optimisation *}
bulwahn@36246
   216
bulwahn@36246
   217
lemma [code_pred_simp]: "\<not> False == True"
bulwahn@36246
   218
by auto
bulwahn@36246
   219
bulwahn@36246
   220
lemma [code_pred_simp]: "\<not> True == False"
bulwahn@36246
   221
by auto
bulwahn@36246
   222
bulwahn@36246
   223
lemma less_nat_k_0 [code_pred_simp]: "less_nat k 0 == False"
bulwahn@36246
   224
unfolding less_nat[symmetric] by auto
bulwahn@35953
   225
noschinl@46884
   226
end