src/HOL/Predicate_Compile_Examples/Predicate_Compile_Tests.thy
author krauss
Mon Mar 28 17:33:16 2011 +0200 (2011-03-28)
changeset 42142 d24a93025feb
parent 41413 64cd30d6b0b8
child 42208 02513eb26eb7
permissions -rw-r--r--
raised various timeouts to accommodate sluggish SML/NJ
bulwahn@39655
     1
theory Predicate_Compile_Tests
wenzelm@41413
     2
imports "~~/src/HOL/Library/Predicate_Compile_Alternative_Defs"
bulwahn@39655
     3
begin
bulwahn@39655
     4
krauss@42142
     5
declare [[values_timeout = 240.0]]
krauss@42142
     6
bulwahn@39655
     7
subsection {* Basic predicates *}
bulwahn@39655
     8
bulwahn@39655
     9
inductive False' :: "bool"
bulwahn@39655
    10
bulwahn@39655
    11
code_pred (expected_modes: bool) False' .
bulwahn@39655
    12
code_pred [dseq] False' .
bulwahn@39655
    13
code_pred [random_dseq] False' .
bulwahn@39655
    14
bulwahn@39655
    15
values [expected "{}" pred] "{x. False'}"
bulwahn@39655
    16
values [expected "{}" dseq 1] "{x. False'}"
bulwahn@39655
    17
values [expected "{}" random_dseq 1, 1, 1] "{x. False'}"
bulwahn@39655
    18
bulwahn@39655
    19
value "False'"
bulwahn@39655
    20
bulwahn@39655
    21
inductive True' :: "bool"
bulwahn@39655
    22
where
bulwahn@39655
    23
  "True ==> True'"
bulwahn@39655
    24
bulwahn@39655
    25
code_pred True' .
bulwahn@39655
    26
code_pred [dseq] True' .
bulwahn@39655
    27
code_pred [random_dseq] True' .
bulwahn@39655
    28
bulwahn@39655
    29
thm True'.equation
bulwahn@39655
    30
thm True'.dseq_equation
bulwahn@39655
    31
thm True'.random_dseq_equation
bulwahn@39655
    32
values [expected "{()}" ]"{x. True'}"
bulwahn@39655
    33
values [expected "{}" dseq 0] "{x. True'}"
bulwahn@39655
    34
values [expected "{()}" dseq 1] "{x. True'}"
bulwahn@39655
    35
values [expected "{()}" dseq 2] "{x. True'}"
bulwahn@39655
    36
values [expected "{}" random_dseq 1, 1, 0] "{x. True'}"
bulwahn@39655
    37
values [expected "{}" random_dseq 1, 1, 1] "{x. True'}"
bulwahn@39655
    38
values [expected "{()}" random_dseq 1, 1, 2] "{x. True'}"
bulwahn@39655
    39
values [expected "{()}" random_dseq 1, 1, 3] "{x. True'}"
bulwahn@39655
    40
bulwahn@39655
    41
inductive EmptySet :: "'a \<Rightarrow> bool"
bulwahn@39655
    42
bulwahn@39655
    43
code_pred (expected_modes: o => bool, i => bool) EmptySet .
bulwahn@39655
    44
bulwahn@39655
    45
definition EmptySet' :: "'a \<Rightarrow> bool"
bulwahn@39655
    46
where "EmptySet' = {}"
bulwahn@39655
    47
bulwahn@39655
    48
code_pred (expected_modes: o => bool, i => bool) [inductify] EmptySet' .
bulwahn@39655
    49
bulwahn@39655
    50
inductive EmptyRel :: "'a \<Rightarrow> 'b \<Rightarrow> bool"
bulwahn@39655
    51
bulwahn@39655
    52
code_pred (expected_modes: o => o => bool, i => o => bool, o => i => bool, i => i => bool) EmptyRel .
bulwahn@39655
    53
bulwahn@39655
    54
inductive EmptyClosure :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool"
bulwahn@39655
    55
for r :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
bulwahn@39655
    56
bulwahn@39655
    57
code_pred
bulwahn@39655
    58
  (expected_modes: (o => o => bool) => o => o => bool, (o => o => bool) => i => o => bool,
bulwahn@39655
    59
         (o => o => bool) => o => i => bool, (o => o => bool) => i => i => bool,
bulwahn@39655
    60
         (i => o => bool) => o => o => bool, (i => o => bool) => i => o => bool,
bulwahn@39655
    61
         (i => o => bool) => o => i => bool, (i => o => bool) => i => i => bool,
bulwahn@39655
    62
         (o => i => bool) => o => o => bool, (o => i => bool) => i => o => bool,
bulwahn@39655
    63
         (o => i => bool) => o => i => bool, (o => i => bool) => i => i => bool,
bulwahn@39655
    64
         (i => i => bool) => o => o => bool, (i => i => bool) => i => o => bool,
bulwahn@39655
    65
         (i => i => bool) => o => i => bool, (i => i => bool) => i => i => bool)
bulwahn@39655
    66
  EmptyClosure .
bulwahn@39655
    67
bulwahn@39655
    68
thm EmptyClosure.equation
bulwahn@39655
    69
bulwahn@39655
    70
(* TODO: inductive package is broken!
bulwahn@39655
    71
inductive False'' :: "bool"
bulwahn@39655
    72
where
bulwahn@39655
    73
  "False \<Longrightarrow> False''"
bulwahn@39655
    74
bulwahn@40100
    75
code_pred (expected_modes: bool) False'' .
bulwahn@39655
    76
bulwahn@39655
    77
inductive EmptySet'' :: "'a \<Rightarrow> bool"
bulwahn@39655
    78
where
bulwahn@39655
    79
  "False \<Longrightarrow> EmptySet'' x"
bulwahn@39655
    80
bulwahn@40100
    81
code_pred (expected_modes: i => bool, o => bool) [inductify] EmptySet'' .
bulwahn@39655
    82
*)
bulwahn@39655
    83
bulwahn@39655
    84
consts a' :: 'a
bulwahn@39655
    85
bulwahn@39655
    86
inductive Fact :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
bulwahn@39655
    87
where
bulwahn@39655
    88
"Fact a' a'"
bulwahn@39655
    89
bulwahn@39655
    90
code_pred (expected_modes: o => o => bool, i => o => bool, o => i => bool, i => i => bool) Fact .
bulwahn@39655
    91
bulwahn@39655
    92
inductive zerozero :: "nat * nat => bool"
bulwahn@39655
    93
where
bulwahn@39655
    94
  "zerozero (0, 0)"
bulwahn@39655
    95
bulwahn@39655
    96
code_pred (expected_modes: i => bool, i * o => bool, o * i => bool, o => bool) zerozero .
bulwahn@39655
    97
code_pred [dseq] zerozero .
bulwahn@39655
    98
code_pred [random_dseq] zerozero .
bulwahn@39655
    99
bulwahn@39655
   100
thm zerozero.equation
bulwahn@39655
   101
thm zerozero.dseq_equation
bulwahn@39655
   102
thm zerozero.random_dseq_equation
bulwahn@39655
   103
bulwahn@39655
   104
text {* We expect the user to expand the tuples in the values command.
bulwahn@39655
   105
The following values command is not supported. *}
bulwahn@39655
   106
(*values "{x. zerozero x}" *)
bulwahn@39655
   107
text {* Instead, the user must type *}
bulwahn@39655
   108
values "{(x, y). zerozero (x, y)}"
bulwahn@39655
   109
bulwahn@39655
   110
values [expected "{}" dseq 0] "{(x, y). zerozero (x, y)}"
bulwahn@39655
   111
values [expected "{(0::nat, 0::nat)}" dseq 1] "{(x, y). zerozero (x, y)}"
bulwahn@39655
   112
values [expected "{(0::nat, 0::nat)}" dseq 2] "{(x, y). zerozero (x, y)}"
bulwahn@39655
   113
values [expected "{}" random_dseq 1, 1, 2] "{(x, y). zerozero (x, y)}"
bulwahn@39655
   114
values [expected "{(0::nat, 0:: nat)}" random_dseq 1, 1, 3] "{(x, y). zerozero (x, y)}"
bulwahn@39655
   115
bulwahn@39655
   116
inductive nested_tuples :: "((int * int) * int * int) => bool"
bulwahn@39655
   117
where
bulwahn@39655
   118
  "nested_tuples ((0, 1), 2, 3)"
bulwahn@39655
   119
bulwahn@39655
   120
code_pred nested_tuples .
bulwahn@39655
   121
bulwahn@39655
   122
inductive JamesBond :: "nat => int => code_numeral => bool"
bulwahn@39655
   123
where
bulwahn@39655
   124
  "JamesBond 0 0 7"
bulwahn@39655
   125
bulwahn@39655
   126
code_pred JamesBond .
bulwahn@39655
   127
bulwahn@39655
   128
values [expected "{(0::nat, 0::int , 7::code_numeral)}"] "{(a, b, c). JamesBond a b c}"
bulwahn@39655
   129
values [expected "{(0::nat, 7::code_numeral, 0:: int)}"] "{(a, c, b). JamesBond a b c}"
bulwahn@39655
   130
values [expected "{(0::int, 0::nat, 7::code_numeral)}"] "{(b, a, c). JamesBond a b c}"
bulwahn@39655
   131
values [expected "{(0::int, 7::code_numeral, 0::nat)}"] "{(b, c, a). JamesBond a b c}"
bulwahn@39655
   132
values [expected "{(7::code_numeral, 0::nat, 0::int)}"] "{(c, a, b). JamesBond a b c}"
bulwahn@39655
   133
values [expected "{(7::code_numeral, 0::int, 0::nat)}"] "{(c, b, a). JamesBond a b c}"
bulwahn@39655
   134
bulwahn@39655
   135
values [expected "{(7::code_numeral, 0::int)}"] "{(a, b). JamesBond 0 b a}"
bulwahn@39655
   136
values [expected "{(7::code_numeral, 0::nat)}"] "{(c, a). JamesBond a 0 c}"
bulwahn@39655
   137
values [expected "{(0::nat, 7::code_numeral)}"] "{(a, c). JamesBond a 0 c}"
bulwahn@39655
   138
bulwahn@39655
   139
bulwahn@39655
   140
subsection {* Alternative Rules *}
bulwahn@39655
   141
bulwahn@39655
   142
datatype char = C | D | E | F | G | H
bulwahn@39655
   143
bulwahn@39655
   144
inductive is_C_or_D
bulwahn@39655
   145
where
bulwahn@39655
   146
  "(x = C) \<or> (x = D) ==> is_C_or_D x"
bulwahn@39655
   147
bulwahn@39655
   148
code_pred (expected_modes: i => bool) is_C_or_D .
bulwahn@39655
   149
thm is_C_or_D.equation
bulwahn@39655
   150
bulwahn@39655
   151
inductive is_D_or_E
bulwahn@39655
   152
where
bulwahn@39655
   153
  "(x = D) \<or> (x = E) ==> is_D_or_E x"
bulwahn@39655
   154
bulwahn@39655
   155
lemma [code_pred_intro]:
bulwahn@39655
   156
  "is_D_or_E D"
bulwahn@39655
   157
by (auto intro: is_D_or_E.intros)
bulwahn@39655
   158
bulwahn@39655
   159
lemma [code_pred_intro]:
bulwahn@39655
   160
  "is_D_or_E E"
bulwahn@39655
   161
by (auto intro: is_D_or_E.intros)
bulwahn@39655
   162
bulwahn@39655
   163
code_pred (expected_modes: o => bool, i => bool) is_D_or_E
bulwahn@39655
   164
proof -
bulwahn@39655
   165
  case is_D_or_E
bulwahn@39655
   166
  from is_D_or_E.prems show thesis
bulwahn@39655
   167
  proof 
bulwahn@39655
   168
    fix xa
bulwahn@39655
   169
    assume x: "x = xa"
bulwahn@39655
   170
    assume "xa = D \<or> xa = E"
bulwahn@39655
   171
    from this show thesis
bulwahn@39655
   172
    proof
bulwahn@39655
   173
      assume "xa = D" from this x is_D_or_E(1) show thesis by simp
bulwahn@39655
   174
    next
bulwahn@39655
   175
      assume "xa = E" from this x is_D_or_E(2) show thesis by simp
bulwahn@39655
   176
    qed
bulwahn@39655
   177
  qed
bulwahn@39655
   178
qed
bulwahn@39655
   179
bulwahn@39655
   180
thm is_D_or_E.equation
bulwahn@39655
   181
bulwahn@39655
   182
inductive is_F_or_G
bulwahn@39655
   183
where
bulwahn@39655
   184
  "x = F \<or> x = G ==> is_F_or_G x"
bulwahn@39655
   185
bulwahn@39655
   186
lemma [code_pred_intro]:
bulwahn@39655
   187
  "is_F_or_G F"
bulwahn@39655
   188
by (auto intro: is_F_or_G.intros)
bulwahn@39655
   189
bulwahn@39655
   190
lemma [code_pred_intro]:
bulwahn@39655
   191
  "is_F_or_G G"
bulwahn@39655
   192
by (auto intro: is_F_or_G.intros)
bulwahn@39655
   193
bulwahn@39655
   194
inductive is_FGH
bulwahn@39655
   195
where
bulwahn@39655
   196
  "is_F_or_G x ==> is_FGH x"
bulwahn@39655
   197
| "is_FGH H"
bulwahn@39655
   198
bulwahn@39655
   199
text {* Compilation of is_FGH requires elimination rule for is_F_or_G *}
bulwahn@39655
   200
bulwahn@39655
   201
code_pred (expected_modes: o => bool, i => bool) is_FGH
bulwahn@39655
   202
proof -
bulwahn@39655
   203
  case is_F_or_G
bulwahn@39655
   204
  from is_F_or_G.prems show thesis
bulwahn@39655
   205
  proof
bulwahn@39655
   206
    fix xa
bulwahn@39655
   207
    assume x: "x = xa"
bulwahn@39655
   208
    assume "xa = F \<or> xa = G"
bulwahn@39655
   209
    from this show thesis
bulwahn@39655
   210
    proof
bulwahn@39655
   211
      assume "xa = F"
bulwahn@39655
   212
      from this x is_F_or_G(1) show thesis by simp
bulwahn@39655
   213
    next
bulwahn@39655
   214
      assume "xa = G"
bulwahn@39655
   215
      from this x is_F_or_G(2) show thesis by simp
bulwahn@39655
   216
    qed
bulwahn@39655
   217
  qed
bulwahn@39655
   218
qed
bulwahn@39655
   219
bulwahn@39655
   220
subsection {* Named alternative rules *}
bulwahn@39655
   221
bulwahn@39655
   222
inductive appending
bulwahn@39655
   223
where
bulwahn@39655
   224
  nil: "appending [] ys ys"
bulwahn@39655
   225
| cons: "appending xs ys zs \<Longrightarrow> appending (x#xs) ys (x#zs)"
bulwahn@39655
   226
bulwahn@39655
   227
lemma appending_alt_nil: "ys = zs \<Longrightarrow> appending [] ys zs"
bulwahn@39655
   228
by (auto intro: appending.intros)
bulwahn@39655
   229
bulwahn@39655
   230
lemma appending_alt_cons: "xs' = x # xs \<Longrightarrow> appending xs ys zs \<Longrightarrow> zs' = x # zs \<Longrightarrow> appending xs' ys zs'"
bulwahn@39655
   231
by (auto intro: appending.intros)
bulwahn@39655
   232
bulwahn@39655
   233
text {* With code_pred_intro, we can give fact names to the alternative rules,
bulwahn@39655
   234
  which are used for the code_pred command. *}
bulwahn@39655
   235
bulwahn@39655
   236
declare appending_alt_nil[code_pred_intro alt_nil] appending_alt_cons[code_pred_intro alt_cons]
bulwahn@39655
   237
 
bulwahn@39655
   238
code_pred appending
bulwahn@39655
   239
proof -
bulwahn@39655
   240
  case appending
bulwahn@39655
   241
  from appending.prems show thesis
bulwahn@39655
   242
  proof(cases)
bulwahn@39655
   243
    case nil
bulwahn@39655
   244
    from alt_nil nil show thesis by auto
bulwahn@39655
   245
  next
bulwahn@39655
   246
    case cons
bulwahn@39655
   247
    from alt_cons cons show thesis by fastsimp
bulwahn@39655
   248
  qed
bulwahn@39655
   249
qed
bulwahn@39655
   250
bulwahn@39655
   251
bulwahn@39655
   252
inductive ya_even and ya_odd :: "nat => bool"
bulwahn@39655
   253
where
bulwahn@39655
   254
  even_zero: "ya_even 0"
bulwahn@39655
   255
| odd_plus1: "ya_even x ==> ya_odd (x + 1)"
bulwahn@39655
   256
| even_plus1: "ya_odd x ==> ya_even (x + 1)"
bulwahn@39655
   257
bulwahn@39655
   258
bulwahn@39655
   259
declare even_zero[code_pred_intro even_0]
bulwahn@39655
   260
bulwahn@39655
   261
lemma [code_pred_intro odd_Suc]: "ya_even x ==> ya_odd (Suc x)"
bulwahn@39655
   262
by (auto simp only: Suc_eq_plus1 intro: ya_even_ya_odd.intros)
bulwahn@39655
   263
bulwahn@39655
   264
lemma [code_pred_intro even_Suc]:"ya_odd x ==> ya_even (Suc x)"
bulwahn@39655
   265
by (auto simp only: Suc_eq_plus1 intro: ya_even_ya_odd.intros)
bulwahn@39655
   266
bulwahn@39655
   267
code_pred ya_even
bulwahn@39655
   268
proof -
bulwahn@39655
   269
  case ya_even
bulwahn@39655
   270
  from ya_even.prems show thesis
bulwahn@39655
   271
  proof (cases)
bulwahn@39655
   272
    case even_zero
bulwahn@39655
   273
    from even_zero even_0 show thesis by simp
bulwahn@39655
   274
  next
bulwahn@39655
   275
    case even_plus1
bulwahn@39655
   276
    from even_plus1 even_Suc show thesis by simp
bulwahn@39655
   277
  qed
bulwahn@39655
   278
next
bulwahn@39655
   279
  case ya_odd
bulwahn@39655
   280
  from ya_odd.prems show thesis
bulwahn@39655
   281
  proof (cases)
bulwahn@39655
   282
    case odd_plus1
bulwahn@39655
   283
    from odd_plus1 odd_Suc show thesis by simp
bulwahn@39655
   284
  qed
bulwahn@39655
   285
qed
bulwahn@39655
   286
bulwahn@39655
   287
subsection {* Preprocessor Inlining  *}
bulwahn@39655
   288
bulwahn@39655
   289
definition "equals == (op =)"
bulwahn@39655
   290
 
bulwahn@39655
   291
inductive zerozero' :: "nat * nat => bool" where
bulwahn@39655
   292
  "equals (x, y) (0, 0) ==> zerozero' (x, y)"
bulwahn@39655
   293
bulwahn@39655
   294
code_pred (expected_modes: i => bool) zerozero' .
bulwahn@39655
   295
bulwahn@39655
   296
lemma zerozero'_eq: "zerozero' x == zerozero x"
bulwahn@39655
   297
proof -
bulwahn@39655
   298
  have "zerozero' = zerozero"
bulwahn@39655
   299
    apply (auto simp add: mem_def)
bulwahn@39655
   300
    apply (cases rule: zerozero'.cases)
bulwahn@39655
   301
    apply (auto simp add: equals_def intro: zerozero.intros)
bulwahn@39655
   302
    apply (cases rule: zerozero.cases)
bulwahn@39655
   303
    apply (auto simp add: equals_def intro: zerozero'.intros)
bulwahn@39655
   304
    done
bulwahn@39655
   305
  from this show "zerozero' x == zerozero x" by auto
bulwahn@39655
   306
qed
bulwahn@39655
   307
bulwahn@39655
   308
declare zerozero'_eq [code_pred_inline]
bulwahn@39655
   309
bulwahn@39655
   310
definition "zerozero'' x == zerozero' x"
bulwahn@39655
   311
bulwahn@39655
   312
text {* if preprocessing fails, zerozero'' will not have all modes. *}
bulwahn@39655
   313
bulwahn@39655
   314
code_pred (expected_modes: i * i => bool, i * o => bool, o * i => bool, o => bool) [inductify] zerozero'' .
bulwahn@39655
   315
bulwahn@39655
   316
subsection {* Sets and Numerals *}
bulwahn@39655
   317
bulwahn@39655
   318
definition
bulwahn@39655
   319
  "one_or_two = {Suc 0, (Suc (Suc 0))}"
bulwahn@39655
   320
bulwahn@39655
   321
code_pred [inductify] one_or_two .
bulwahn@39655
   322
bulwahn@39655
   323
code_pred [dseq] one_or_two .
bulwahn@39655
   324
code_pred [random_dseq] one_or_two .
bulwahn@39655
   325
thm one_or_two.dseq_equation
bulwahn@39655
   326
values [expected "{Suc 0::nat, 2::nat}"] "{x. one_or_two x}"
bulwahn@39655
   327
values [random_dseq 0,0,10] 3 "{x. one_or_two x}"
bulwahn@39655
   328
bulwahn@39655
   329
inductive one_or_two' :: "nat => bool"
bulwahn@39655
   330
where
bulwahn@39655
   331
  "one_or_two' 1"
bulwahn@39655
   332
| "one_or_two' 2"
bulwahn@39655
   333
bulwahn@39655
   334
code_pred one_or_two' .
bulwahn@39655
   335
thm one_or_two'.equation
bulwahn@39655
   336
bulwahn@39655
   337
values "{x. one_or_two' x}"
bulwahn@39655
   338
bulwahn@39655
   339
definition one_or_two'':
bulwahn@39655
   340
  "one_or_two'' == {1, (2::nat)}"
bulwahn@39655
   341
bulwahn@39655
   342
code_pred [inductify] one_or_two'' .
bulwahn@39655
   343
thm one_or_two''.equation
bulwahn@39655
   344
bulwahn@39655
   345
values "{x. one_or_two'' x}"
bulwahn@39655
   346
bulwahn@39655
   347
subsection {* even predicate *}
bulwahn@39655
   348
bulwahn@39655
   349
inductive even :: "nat \<Rightarrow> bool" and odd :: "nat \<Rightarrow> bool" where
bulwahn@39655
   350
    "even 0"
bulwahn@39655
   351
  | "even n \<Longrightarrow> odd (Suc n)"
bulwahn@39655
   352
  | "odd n \<Longrightarrow> even (Suc n)"
bulwahn@39655
   353
bulwahn@39655
   354
code_pred (expected_modes: i => bool, o => bool) even .
bulwahn@39655
   355
code_pred [dseq] even .
bulwahn@39655
   356
code_pred [random_dseq] even .
bulwahn@39655
   357
bulwahn@39655
   358
thm odd.equation
bulwahn@39655
   359
thm even.equation
bulwahn@39655
   360
thm odd.dseq_equation
bulwahn@39655
   361
thm even.dseq_equation
bulwahn@39655
   362
thm odd.random_dseq_equation
bulwahn@39655
   363
thm even.random_dseq_equation
bulwahn@39655
   364
bulwahn@39655
   365
values "{x. even 2}"
bulwahn@39655
   366
values "{x. odd 2}"
bulwahn@39655
   367
values 10 "{n. even n}"
bulwahn@39655
   368
values 10 "{n. odd n}"
bulwahn@39655
   369
values [expected "{}" dseq 2] "{x. even 6}"
bulwahn@39655
   370
values [expected "{}" dseq 6] "{x. even 6}"
bulwahn@39655
   371
values [expected "{()}" dseq 7] "{x. even 6}"
bulwahn@39655
   372
values [dseq 2] "{x. odd 7}"
bulwahn@39655
   373
values [dseq 6] "{x. odd 7}"
bulwahn@39655
   374
values [dseq 7] "{x. odd 7}"
bulwahn@39655
   375
values [expected "{()}" dseq 8] "{x. odd 7}"
bulwahn@39655
   376
bulwahn@39655
   377
values [expected "{}" dseq 0] 8 "{x. even x}"
bulwahn@39655
   378
values [expected "{0::nat}" dseq 1] 8 "{x. even x}"
bulwahn@39655
   379
values [expected "{0::nat, 2}" dseq 3] 8 "{x. even x}"
bulwahn@39655
   380
values [expected "{0::nat, 2}" dseq 4] 8 "{x. even x}"
bulwahn@39655
   381
values [expected "{0::nat, 2, 4}" dseq 6] 8 "{x. even x}"
bulwahn@39655
   382
bulwahn@39655
   383
values [random_dseq 1, 1, 0] 8 "{x. even x}"
bulwahn@39655
   384
values [random_dseq 1, 1, 1] 8 "{x. even x}"
bulwahn@39655
   385
values [random_dseq 1, 1, 2] 8 "{x. even x}"
bulwahn@39655
   386
values [random_dseq 1, 1, 3] 8 "{x. even x}"
bulwahn@39655
   387
values [random_dseq 1, 1, 6] 8 "{x. even x}"
bulwahn@39655
   388
bulwahn@39655
   389
values [expected "{}" random_dseq 1, 1, 7] "{x. odd 7}"
bulwahn@39655
   390
values [random_dseq 1, 1, 8] "{x. odd 7}"
bulwahn@39655
   391
values [random_dseq 1, 1, 9] "{x. odd 7}"
bulwahn@39655
   392
bulwahn@39655
   393
definition odd' where "odd' x == \<not> even x"
bulwahn@39655
   394
bulwahn@39655
   395
code_pred (expected_modes: i => bool) [inductify] odd' .
bulwahn@39655
   396
code_pred [dseq inductify] odd' .
bulwahn@39655
   397
code_pred [random_dseq inductify] odd' .
bulwahn@39655
   398
bulwahn@39655
   399
values [expected "{}" dseq 2] "{x. odd' 7}"
bulwahn@39655
   400
values [expected "{()}" dseq 9] "{x. odd' 7}"
bulwahn@39655
   401
values [expected "{}" dseq 2] "{x. odd' 8}"
bulwahn@39655
   402
values [expected "{}" dseq 10] "{x. odd' 8}"
bulwahn@39655
   403
bulwahn@39655
   404
bulwahn@39655
   405
inductive is_even :: "nat \<Rightarrow> bool"
bulwahn@39655
   406
where
bulwahn@39655
   407
  "n mod 2 = 0 \<Longrightarrow> is_even n"
bulwahn@39655
   408
bulwahn@39655
   409
code_pred (expected_modes: i => bool) is_even .
bulwahn@39655
   410
bulwahn@39655
   411
subsection {* append predicate *}
bulwahn@39655
   412
bulwahn@39655
   413
inductive append :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" where
bulwahn@39655
   414
    "append [] xs xs"
bulwahn@39655
   415
  | "append xs ys zs \<Longrightarrow> append (x # xs) ys (x # zs)"
bulwahn@39655
   416
bulwahn@39655
   417
code_pred (modes: i => i => o => bool as "concat", o => o => i => bool as "slice", o => i => i => bool as prefix,
bulwahn@39655
   418
  i => o => i => bool as suffix, i => i => i => bool) append .
bulwahn@39655
   419
code_pred (modes: i \<Rightarrow> i \<Rightarrow> o \<Rightarrow> bool as "concat", o \<Rightarrow> o \<Rightarrow> i \<Rightarrow> bool as "slice", o \<Rightarrow> i \<Rightarrow> i \<Rightarrow> bool as prefix,
bulwahn@39655
   420
  i \<Rightarrow> o \<Rightarrow> i \<Rightarrow> bool as suffix, i \<Rightarrow> i \<Rightarrow> i \<Rightarrow> bool) append .
bulwahn@39655
   421
bulwahn@39655
   422
code_pred [dseq] append .
bulwahn@39655
   423
code_pred [random_dseq] append .
bulwahn@39655
   424
bulwahn@39655
   425
thm append.equation
bulwahn@39655
   426
thm append.dseq_equation
bulwahn@39655
   427
thm append.random_dseq_equation
bulwahn@39655
   428
bulwahn@39655
   429
values "{(ys, xs). append xs ys [0, Suc 0, 2]}"
bulwahn@39655
   430
values "{zs. append [0, Suc 0, 2] [17, 8] zs}"
bulwahn@39655
   431
values "{ys. append [0, Suc 0, 2] ys [0, Suc 0, 2, 17, 0, 5]}"
bulwahn@39655
   432
bulwahn@39655
   433
values [expected "{}" dseq 0] 10 "{(xs, ys). append xs ys [1, 2, 3, 4, (5::nat)]}"
bulwahn@39655
   434
values [expected "{(([]::nat list), [Suc 0, 2, 3, 4, (5::nat)])}" dseq 1] 10 "{(xs, ys). append xs ys [1, 2, 3, 4, (5::nat)]}"
bulwahn@39655
   435
values [dseq 4] 10 "{(xs, ys). append xs ys [1, 2, 3, 4, (5::nat)]}"
bulwahn@39655
   436
values [dseq 6] 10 "{(xs, ys). append xs ys [1, 2, 3, 4, (5::nat)]}"
bulwahn@39655
   437
values [random_dseq 1, 1, 4] 10 "{(xs, ys). append xs ys [1, 2, 3, 4, (5::nat)]}"
bulwahn@39655
   438
values [random_dseq 1, 1, 1] 10 "{(xs, ys, zs::int list). append xs ys zs}"
bulwahn@39655
   439
values [random_dseq 1, 1, 3] 10 "{(xs, ys, zs::int list). append xs ys zs}"
bulwahn@39655
   440
values [random_dseq 3, 1, 3] 10 "{(xs, ys, zs::int list). append xs ys zs}"
bulwahn@39655
   441
values [random_dseq 1, 3, 3] 10 "{(xs, ys, zs::int list). append xs ys zs}"
bulwahn@39655
   442
values [random_dseq 1, 1, 4] 10 "{(xs, ys, zs::int list). append xs ys zs}"
bulwahn@39655
   443
bulwahn@39655
   444
value [code] "Predicate.the (concat [0::int, 1, 2] [3, 4, 5])"
bulwahn@39655
   445
value [code] "Predicate.the (slice ([]::int list))"
bulwahn@39655
   446
bulwahn@39655
   447
bulwahn@39655
   448
text {* tricky case with alternative rules *}
bulwahn@39655
   449
bulwahn@39655
   450
inductive append2
bulwahn@39655
   451
where
bulwahn@39655
   452
  "append2 [] xs xs"
bulwahn@39655
   453
| "append2 xs ys zs \<Longrightarrow> append2 (x # xs) ys (x # zs)"
bulwahn@39655
   454
bulwahn@39655
   455
lemma append2_Nil: "append2 [] (xs::'b list) xs"
bulwahn@39655
   456
  by (simp add: append2.intros(1))
bulwahn@39655
   457
bulwahn@39655
   458
lemmas [code_pred_intro] = append2_Nil append2.intros(2)
bulwahn@39655
   459
bulwahn@39655
   460
code_pred (expected_modes: i => i => o => bool, o => o => i => bool, o => i => i => bool,
bulwahn@39655
   461
  i => o => i => bool, i => i => i => bool) append2
bulwahn@39655
   462
proof -
bulwahn@39655
   463
  case append2
bulwahn@39655
   464
  from append2.prems show thesis
bulwahn@39655
   465
  proof
bulwahn@39655
   466
    fix xs
bulwahn@39655
   467
    assume "xa = []" "xb = xs" "xc = xs"
bulwahn@39655
   468
    from this append2(1) show thesis by simp
bulwahn@39655
   469
  next
bulwahn@39655
   470
    fix xs ys zs x
bulwahn@39655
   471
    assume "xa = x # xs" "xb = ys" "xc = x # zs" "append2 xs ys zs"
bulwahn@39655
   472
    from this append2(2) show thesis by fastsimp
bulwahn@39655
   473
  qed
bulwahn@39655
   474
qed
bulwahn@39655
   475
bulwahn@39655
   476
inductive tupled_append :: "'a list \<times> 'a list \<times> 'a list \<Rightarrow> bool"
bulwahn@39655
   477
where
bulwahn@39655
   478
  "tupled_append ([], xs, xs)"
bulwahn@39655
   479
| "tupled_append (xs, ys, zs) \<Longrightarrow> tupled_append (x # xs, ys, x # zs)"
bulwahn@39655
   480
bulwahn@39655
   481
code_pred (expected_modes: i * i * o => bool, o * o * i => bool, o * i * i => bool,
bulwahn@39655
   482
  i * o * i => bool, i * i * i => bool) tupled_append .
bulwahn@39655
   483
bulwahn@39655
   484
code_pred (expected_modes: i \<times> i \<times> o \<Rightarrow> bool, o \<times> o \<times> i \<Rightarrow> bool, o \<times> i \<times> i \<Rightarrow> bool,
bulwahn@39655
   485
  i \<times> o \<times> i \<Rightarrow> bool, i \<times> i \<times> i \<Rightarrow> bool) tupled_append .
bulwahn@39655
   486
bulwahn@39655
   487
code_pred [random_dseq] tupled_append .
bulwahn@39655
   488
thm tupled_append.equation
bulwahn@39655
   489
bulwahn@39655
   490
values "{xs. tupled_append ([(1::nat), 2, 3], [4, 5], xs)}"
bulwahn@39655
   491
bulwahn@39655
   492
inductive tupled_append'
bulwahn@39655
   493
where
bulwahn@39655
   494
"tupled_append' ([], xs, xs)"
bulwahn@39655
   495
| "[| ys = fst (xa, y); x # zs = snd (xa, y);
bulwahn@39655
   496
 tupled_append' (xs, ys, zs) |] ==> tupled_append' (x # xs, xa, y)"
bulwahn@39655
   497
bulwahn@39655
   498
code_pred (expected_modes: i * i * o => bool, o * o * i => bool, o * i * i => bool,
bulwahn@39655
   499
  i * o * i => bool, i * i * i => bool) tupled_append' .
bulwahn@39655
   500
thm tupled_append'.equation
bulwahn@39655
   501
bulwahn@39655
   502
inductive tupled_append'' :: "'a list \<times> 'a list \<times> 'a list \<Rightarrow> bool"
bulwahn@39655
   503
where
bulwahn@39655
   504
  "tupled_append'' ([], xs, xs)"
bulwahn@39655
   505
| "ys = fst yszs ==> x # zs = snd yszs ==> tupled_append'' (xs, ys, zs) \<Longrightarrow> tupled_append'' (x # xs, yszs)"
bulwahn@39655
   506
bulwahn@39655
   507
code_pred (expected_modes: i * i * o => bool, o * o * i => bool, o * i * i => bool,
bulwahn@39655
   508
  i * o * i => bool, i * i * i => bool) tupled_append'' .
bulwahn@39655
   509
thm tupled_append''.equation
bulwahn@39655
   510
bulwahn@39655
   511
inductive tupled_append''' :: "'a list \<times> 'a list \<times> 'a list \<Rightarrow> bool"
bulwahn@39655
   512
where
bulwahn@39655
   513
  "tupled_append''' ([], xs, xs)"
bulwahn@39655
   514
| "yszs = (ys, zs) ==> tupled_append''' (xs, yszs) \<Longrightarrow> tupled_append''' (x # xs, ys, x # zs)"
bulwahn@39655
   515
bulwahn@39655
   516
code_pred (expected_modes: i * i * o => bool, o * o * i => bool, o * i * i => bool,
bulwahn@39655
   517
  i * o * i => bool, i * i * i => bool) tupled_append''' .
bulwahn@39655
   518
thm tupled_append'''.equation
bulwahn@39655
   519
bulwahn@39655
   520
subsection {* map_ofP predicate *}
bulwahn@39655
   521
bulwahn@39655
   522
inductive map_ofP :: "('a \<times> 'b) list \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
bulwahn@39655
   523
where
bulwahn@39655
   524
  "map_ofP ((a, b)#xs) a b"
bulwahn@39655
   525
| "map_ofP xs a b \<Longrightarrow> map_ofP (x#xs) a b"
bulwahn@39655
   526
bulwahn@39655
   527
code_pred (expected_modes: i => o => o => bool, i => i => o => bool, i => o => i => bool, i => i => i => bool) map_ofP .
bulwahn@39655
   528
thm map_ofP.equation
bulwahn@39655
   529
bulwahn@39655
   530
subsection {* filter predicate *}
bulwahn@39655
   531
bulwahn@39655
   532
inductive filter1
bulwahn@39655
   533
for P
bulwahn@39655
   534
where
bulwahn@39655
   535
  "filter1 P [] []"
bulwahn@39655
   536
| "P x ==> filter1 P xs ys ==> filter1 P (x#xs) (x#ys)"
bulwahn@39655
   537
| "\<not> P x ==> filter1 P xs ys ==> filter1 P (x#xs) ys"
bulwahn@39655
   538
bulwahn@39655
   539
code_pred (expected_modes: (i => bool) => i => o => bool, (i => bool) => i => i => bool) filter1 .
bulwahn@39655
   540
code_pred [dseq] filter1 .
bulwahn@39655
   541
code_pred [random_dseq] filter1 .
bulwahn@39655
   542
bulwahn@39655
   543
thm filter1.equation
bulwahn@39655
   544
bulwahn@39655
   545
values [expected "{[0::nat, 2, 4]}"] "{xs. filter1 even [0, 1, 2, 3, 4] xs}"
bulwahn@39655
   546
values [expected "{}" dseq 9] "{xs. filter1 even [0, 1, 2, 3, 4] xs}"
bulwahn@39655
   547
values [expected "{[0::nat, 2, 4]}" dseq 10] "{xs. filter1 even [0, 1, 2, 3, 4] xs}"
bulwahn@39655
   548
bulwahn@39655
   549
inductive filter2
bulwahn@39655
   550
where
bulwahn@39655
   551
  "filter2 P [] []"
bulwahn@39655
   552
| "P x ==> filter2 P xs ys ==> filter2 P (x#xs) (x#ys)"
bulwahn@39655
   553
| "\<not> P x ==> filter2 P xs ys ==> filter2 P (x#xs) ys"
bulwahn@39655
   554
bulwahn@39655
   555
code_pred (expected_modes: (i => bool) => i => i => bool, (i => bool) => i => o => bool) filter2 .
bulwahn@39655
   556
code_pred [dseq] filter2 .
bulwahn@39655
   557
code_pred [random_dseq] filter2 .
bulwahn@39655
   558
bulwahn@39655
   559
thm filter2.equation
bulwahn@39655
   560
thm filter2.random_dseq_equation
bulwahn@39655
   561
bulwahn@39655
   562
inductive filter3
bulwahn@39655
   563
for P
bulwahn@39655
   564
where
bulwahn@39655
   565
  "List.filter P xs = ys ==> filter3 P xs ys"
bulwahn@39655
   566
bulwahn@39655
   567
code_pred (expected_modes: (o => bool) => i => o => bool, (o => bool) => i => i => bool , (i => bool) => i => o => bool, (i => bool) => i => i => bool) [skip_proof] filter3 .
bulwahn@39655
   568
bulwahn@39655
   569
code_pred filter3 .
bulwahn@39655
   570
thm filter3.equation
bulwahn@39655
   571
bulwahn@39655
   572
(*
bulwahn@39655
   573
inductive filter4
bulwahn@39655
   574
where
bulwahn@39655
   575
  "List.filter P xs = ys ==> filter4 P xs ys"
bulwahn@39655
   576
bulwahn@39655
   577
code_pred (expected_modes: i => i => o => bool, i => i => i => bool) filter4 .
bulwahn@39655
   578
(*code_pred [depth_limited] filter4 .*)
bulwahn@39655
   579
(*code_pred [random] filter4 .*)
bulwahn@39655
   580
*)
bulwahn@39655
   581
subsection {* reverse predicate *}
bulwahn@39655
   582
bulwahn@39655
   583
inductive rev where
bulwahn@39655
   584
    "rev [] []"
bulwahn@39655
   585
  | "rev xs xs' ==> append xs' [x] ys ==> rev (x#xs) ys"
bulwahn@39655
   586
bulwahn@39655
   587
code_pred (expected_modes: i => o => bool, o => i => bool, i => i => bool) rev .
bulwahn@39655
   588
bulwahn@39655
   589
thm rev.equation
bulwahn@39655
   590
bulwahn@39655
   591
values "{xs. rev [0, 1, 2, 3::nat] xs}"
bulwahn@39655
   592
bulwahn@39655
   593
inductive tupled_rev where
bulwahn@39655
   594
  "tupled_rev ([], [])"
bulwahn@39655
   595
| "tupled_rev (xs, xs') \<Longrightarrow> tupled_append (xs', [x], ys) \<Longrightarrow> tupled_rev (x#xs, ys)"
bulwahn@39655
   596
bulwahn@39655
   597
code_pred (expected_modes: i * o => bool, o * i => bool, i * i => bool) tupled_rev .
bulwahn@39655
   598
thm tupled_rev.equation
bulwahn@39655
   599
bulwahn@39655
   600
subsection {* partition predicate *}
bulwahn@39655
   601
bulwahn@39655
   602
inductive partition :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool"
bulwahn@39655
   603
  for f where
bulwahn@39655
   604
    "partition f [] [] []"
bulwahn@39655
   605
  | "f x \<Longrightarrow> partition f xs ys zs \<Longrightarrow> partition f (x # xs) (x # ys) zs"
bulwahn@39655
   606
  | "\<not> f x \<Longrightarrow> partition f xs ys zs \<Longrightarrow> partition f (x # xs) ys (x # zs)"
bulwahn@39655
   607
bulwahn@39655
   608
code_pred (expected_modes: (i => bool) => i => o => o => bool, (i => bool) => o => i => i => bool,
bulwahn@39655
   609
  (i => bool) => i => i => o => bool, (i => bool) => i => o => i => bool, (i => bool) => i => i => i => bool)
bulwahn@39655
   610
  partition .
bulwahn@39655
   611
code_pred [dseq] partition .
bulwahn@39655
   612
code_pred [random_dseq] partition .
bulwahn@39655
   613
bulwahn@39655
   614
values 10 "{(ys, zs). partition is_even
bulwahn@39655
   615
  [0, Suc 0, 2, 3, 4, 5, 6, 7] ys zs}"
bulwahn@39655
   616
values 10 "{zs. partition is_even zs [0, 2] [3, 5]}"
bulwahn@39655
   617
values 10 "{zs. partition is_even zs [0, 7] [3, 5]}"
bulwahn@39655
   618
bulwahn@39655
   619
inductive tupled_partition :: "('a \<Rightarrow> bool) \<Rightarrow> ('a list \<times> 'a list \<times> 'a list) \<Rightarrow> bool"
bulwahn@39655
   620
  for f where
bulwahn@39655
   621
   "tupled_partition f ([], [], [])"
bulwahn@39655
   622
  | "f x \<Longrightarrow> tupled_partition f (xs, ys, zs) \<Longrightarrow> tupled_partition f (x # xs, x # ys, zs)"
bulwahn@39655
   623
  | "\<not> f x \<Longrightarrow> tupled_partition f (xs, ys, zs) \<Longrightarrow> tupled_partition f (x # xs, ys, x # zs)"
bulwahn@39655
   624
bulwahn@39655
   625
code_pred (expected_modes: (i => bool) => i => bool, (i => bool) => (i * i * o) => bool, (i => bool) => (i * o * i) => bool,
bulwahn@39655
   626
  (i => bool) => (o * i * i) => bool, (i => bool) => (i * o * o) => bool) tupled_partition .
bulwahn@39655
   627
bulwahn@39655
   628
thm tupled_partition.equation
bulwahn@39655
   629
bulwahn@39655
   630
lemma [code_pred_intro]:
bulwahn@39655
   631
  "r a b \<Longrightarrow> tranclp r a b"
bulwahn@39655
   632
  "r a b \<Longrightarrow> tranclp r b c \<Longrightarrow> tranclp r a c"
bulwahn@39655
   633
  by auto
bulwahn@39655
   634
bulwahn@39655
   635
subsection {* transitive predicate *}
bulwahn@39655
   636
bulwahn@39655
   637
text {* Also look at the tabled transitive closure in the Library *}
bulwahn@39655
   638
bulwahn@39655
   639
code_pred (modes: (i => o => bool) => i => i => bool, (i => o => bool) => i => o => bool as forwards_trancl,
bulwahn@39655
   640
  (o => i => bool) => i => i => bool, (o => i => bool) => o => i => bool as backwards_trancl, (o => o => bool) => i => i => bool, (o => o => bool) => i => o => bool,
bulwahn@39655
   641
  (o => o => bool) => o => i => bool, (o => o => bool) => o => o => bool) tranclp
bulwahn@39655
   642
proof -
bulwahn@39655
   643
  case tranclp
bulwahn@39655
   644
  from this converse_tranclpE[OF tranclp.prems] show thesis by metis
bulwahn@39655
   645
qed
bulwahn@39655
   646
bulwahn@39655
   647
bulwahn@39655
   648
code_pred [dseq] tranclp .
bulwahn@39655
   649
code_pred [random_dseq] tranclp .
bulwahn@39655
   650
thm tranclp.equation
bulwahn@39655
   651
thm tranclp.random_dseq_equation
bulwahn@39655
   652
bulwahn@39655
   653
inductive rtrancl' :: "'a => 'a => ('a => 'a => bool) => bool" 
bulwahn@39655
   654
where
bulwahn@39655
   655
  "rtrancl' x x r"
bulwahn@39655
   656
| "r x y ==> rtrancl' y z r ==> rtrancl' x z r"
bulwahn@39655
   657
bulwahn@39655
   658
code_pred [random_dseq] rtrancl' .
bulwahn@39655
   659
bulwahn@39655
   660
thm rtrancl'.random_dseq_equation
bulwahn@39655
   661
bulwahn@39655
   662
inductive rtrancl'' :: "('a * 'a * ('a \<Rightarrow> 'a \<Rightarrow> bool)) \<Rightarrow> bool"  
bulwahn@39655
   663
where
bulwahn@39655
   664
  "rtrancl'' (x, x, r)"
bulwahn@39655
   665
| "r x y \<Longrightarrow> rtrancl'' (y, z, r) \<Longrightarrow> rtrancl'' (x, z, r)"
bulwahn@39655
   666
bulwahn@39655
   667
code_pred rtrancl'' .
bulwahn@39655
   668
bulwahn@39655
   669
inductive rtrancl''' :: "('a * ('a * 'a) * ('a * 'a => bool)) => bool" 
bulwahn@39655
   670
where
bulwahn@39655
   671
  "rtrancl''' (x, (x, x), r)"
bulwahn@39655
   672
| "r (x, y) ==> rtrancl''' (y, (z, z), r) ==> rtrancl''' (x, (z, z), r)"
bulwahn@39655
   673
bulwahn@39655
   674
code_pred rtrancl''' .
bulwahn@39655
   675
bulwahn@39655
   676
bulwahn@39655
   677
inductive succ :: "nat \<Rightarrow> nat \<Rightarrow> bool" where
bulwahn@39655
   678
    "succ 0 1"
bulwahn@39655
   679
  | "succ m n \<Longrightarrow> succ (Suc m) (Suc n)"
bulwahn@39655
   680
bulwahn@39655
   681
code_pred (modes: i => i => bool, i => o => bool, o => i => bool, o => o => bool) succ .
bulwahn@39655
   682
code_pred [random_dseq] succ .
bulwahn@39655
   683
thm succ.equation
bulwahn@39655
   684
thm succ.random_dseq_equation
bulwahn@39655
   685
bulwahn@39655
   686
values 10 "{(m, n). succ n m}"
bulwahn@39655
   687
values "{m. succ 0 m}"
bulwahn@39655
   688
values "{m. succ m 0}"
bulwahn@39655
   689
bulwahn@39655
   690
text {* values command needs mode annotation of the parameter succ
bulwahn@39655
   691
to disambiguate which mode is to be chosen. *} 
bulwahn@39655
   692
bulwahn@39655
   693
values [mode: i => o => bool] 20 "{n. tranclp succ 10 n}"
bulwahn@39655
   694
values [mode: o => i => bool] 10 "{n. tranclp succ n 10}"
bulwahn@39655
   695
values 20 "{(n, m). tranclp succ n m}"
bulwahn@39655
   696
bulwahn@39655
   697
inductive example_graph :: "int => int => bool"
bulwahn@39655
   698
where
bulwahn@39655
   699
  "example_graph 0 1"
bulwahn@39655
   700
| "example_graph 1 2"
bulwahn@39655
   701
| "example_graph 1 3"
bulwahn@39655
   702
| "example_graph 4 7"
bulwahn@39655
   703
| "example_graph 4 5"
bulwahn@39655
   704
| "example_graph 5 6"
bulwahn@39655
   705
| "example_graph 7 6"
bulwahn@39655
   706
| "example_graph 7 8"
bulwahn@39655
   707
 
bulwahn@39655
   708
inductive not_reachable_in_example_graph :: "int => int => bool"
bulwahn@39655
   709
where "\<not> (tranclp example_graph x y) ==> not_reachable_in_example_graph x y"
bulwahn@39655
   710
bulwahn@39655
   711
code_pred (expected_modes: i => i => bool) not_reachable_in_example_graph .
bulwahn@39655
   712
bulwahn@39655
   713
thm not_reachable_in_example_graph.equation
bulwahn@39655
   714
thm tranclp.equation
bulwahn@39655
   715
value "not_reachable_in_example_graph 0 3"
bulwahn@39655
   716
value "not_reachable_in_example_graph 4 8"
bulwahn@39655
   717
value "not_reachable_in_example_graph 5 6"
bulwahn@39655
   718
text {* rtrancl compilation is strange! *}
bulwahn@39655
   719
(*
bulwahn@39655
   720
value "not_reachable_in_example_graph 0 4"
bulwahn@39655
   721
value "not_reachable_in_example_graph 1 6"
bulwahn@39655
   722
value "not_reachable_in_example_graph 8 4"*)
bulwahn@39655
   723
bulwahn@39655
   724
code_pred [dseq] not_reachable_in_example_graph .
bulwahn@39655
   725
bulwahn@39655
   726
values [dseq 6] "{x. tranclp example_graph 0 3}"
bulwahn@39655
   727
bulwahn@39655
   728
values [dseq 0] "{x. not_reachable_in_example_graph 0 3}"
bulwahn@39655
   729
values [dseq 0] "{x. not_reachable_in_example_graph 0 4}"
bulwahn@39655
   730
values [dseq 20] "{x. not_reachable_in_example_graph 0 4}"
bulwahn@39655
   731
values [dseq 6] "{x. not_reachable_in_example_graph 0 3}"
bulwahn@39655
   732
values [dseq 3] "{x. not_reachable_in_example_graph 4 2}"
bulwahn@39655
   733
values [dseq 6] "{x. not_reachable_in_example_graph 4 2}"
bulwahn@39655
   734
bulwahn@39655
   735
bulwahn@39655
   736
inductive not_reachable_in_example_graph' :: "int => int => bool"
bulwahn@39655
   737
where "\<not> (rtranclp example_graph x y) ==> not_reachable_in_example_graph' x y"
bulwahn@39655
   738
bulwahn@39655
   739
code_pred not_reachable_in_example_graph' .
bulwahn@39655
   740
bulwahn@39655
   741
value "not_reachable_in_example_graph' 0 3"
bulwahn@39655
   742
(* value "not_reachable_in_example_graph' 0 5" would not terminate *)
bulwahn@39655
   743
bulwahn@39655
   744
bulwahn@39655
   745
(*values [depth_limited 0] "{x. not_reachable_in_example_graph' 0 3}"*)
bulwahn@39655
   746
(*values [depth_limited 3] "{x. not_reachable_in_example_graph' 0 3}"*) (* fails with undefined *)
bulwahn@39655
   747
(*values [depth_limited 5] "{x. not_reachable_in_example_graph' 0 3}"*)
bulwahn@39655
   748
(*values [depth_limited 1] "{x. not_reachable_in_example_graph' 0 4}"*)
bulwahn@39655
   749
(*values [depth_limit = 4] "{x. not_reachable_in_example_graph' 0 4}"*) (* fails with undefined *)
bulwahn@39655
   750
(*values [depth_limit = 20] "{x. not_reachable_in_example_graph' 0 4}"*) (* fails with undefined *)
bulwahn@39655
   751
bulwahn@39655
   752
code_pred [dseq] not_reachable_in_example_graph' .
bulwahn@39655
   753
bulwahn@39655
   754
(*thm not_reachable_in_example_graph'.dseq_equation*)
bulwahn@39655
   755
bulwahn@39655
   756
(*values [dseq 0] "{x. not_reachable_in_example_graph' 0 3}"*)
bulwahn@39655
   757
(*values [depth_limited 3] "{x. not_reachable_in_example_graph' 0 3}"*) (* fails with undefined *)
bulwahn@39655
   758
(*values [depth_limited 5] "{x. not_reachable_in_example_graph' 0 3}"
bulwahn@39655
   759
values [depth_limited 1] "{x. not_reachable_in_example_graph' 0 4}"*)
bulwahn@39655
   760
(*values [depth_limit = 4] "{x. not_reachable_in_example_graph' 0 4}"*) (* fails with undefined *)
bulwahn@39655
   761
(*values [depth_limit = 20] "{x. not_reachable_in_example_graph' 0 4}"*) (* fails with undefined *)
bulwahn@39655
   762
bulwahn@39655
   763
subsection {* Free function variable *}
bulwahn@39655
   764
bulwahn@39655
   765
inductive FF :: "nat => nat => bool"
bulwahn@39655
   766
where
bulwahn@39655
   767
  "f x = y ==> FF x y"
bulwahn@39655
   768
bulwahn@39655
   769
code_pred FF .
bulwahn@39655
   770
bulwahn@39655
   771
subsection {* IMP *}
bulwahn@39655
   772
bulwahn@39655
   773
types
bulwahn@39655
   774
  var = nat
bulwahn@39655
   775
  state = "int list"
bulwahn@39655
   776
bulwahn@39655
   777
datatype com =
bulwahn@39655
   778
  Skip |
bulwahn@39655
   779
  Ass var "state => int" |
bulwahn@39655
   780
  Seq com com |
bulwahn@39655
   781
  IF "state => bool" com com |
bulwahn@39655
   782
  While "state => bool" com
bulwahn@39655
   783
bulwahn@39655
   784
inductive tupled_exec :: "(com \<times> state \<times> state) \<Rightarrow> bool" where
bulwahn@39655
   785
"tupled_exec (Skip, s, s)" |
bulwahn@39655
   786
"tupled_exec (Ass x e, s, s[x := e(s)])" |
bulwahn@39655
   787
"tupled_exec (c1, s1, s2) ==> tupled_exec (c2, s2, s3) ==> tupled_exec (Seq c1 c2, s1, s3)" |
bulwahn@39655
   788
"b s ==> tupled_exec (c1, s, t) ==> tupled_exec (IF b c1 c2, s, t)" |
bulwahn@39655
   789
"~b s ==> tupled_exec (c2, s, t) ==> tupled_exec (IF b c1 c2, s, t)" |
bulwahn@39655
   790
"~b s ==> tupled_exec (While b c, s, s)" |
bulwahn@39655
   791
"b s1 ==> tupled_exec (c, s1, s2) ==> tupled_exec (While b c, s2, s3) ==> tupled_exec (While b c, s1, s3)"
bulwahn@39655
   792
bulwahn@39655
   793
code_pred tupled_exec .
bulwahn@39655
   794
bulwahn@39655
   795
values "{s. tupled_exec (While (%s. s!0 > 0) (Seq (Ass 0 (%s. s!0 - 1)) (Ass 1 (%s. s!1 + 1))), [3, 5], s)}"
bulwahn@39655
   796
bulwahn@39655
   797
subsection {* CCS *}
bulwahn@39655
   798
bulwahn@39655
   799
text{* This example formalizes finite CCS processes without communication or
bulwahn@39655
   800
recursion. For simplicity, labels are natural numbers. *}
bulwahn@39655
   801
bulwahn@39655
   802
datatype proc = nil | pre nat proc | or proc proc | par proc proc
bulwahn@39655
   803
bulwahn@39655
   804
inductive tupled_step :: "(proc \<times> nat \<times> proc) \<Rightarrow> bool"
bulwahn@39655
   805
where
bulwahn@39655
   806
"tupled_step (pre n p, n, p)" |
bulwahn@39655
   807
"tupled_step (p1, a, q) \<Longrightarrow> tupled_step (or p1 p2, a, q)" |
bulwahn@39655
   808
"tupled_step (p2, a, q) \<Longrightarrow> tupled_step (or p1 p2, a, q)" |
bulwahn@39655
   809
"tupled_step (p1, a, q) \<Longrightarrow> tupled_step (par p1 p2, a, par q p2)" |
bulwahn@39655
   810
"tupled_step (p2, a, q) \<Longrightarrow> tupled_step (par p1 p2, a, par p1 q)"
bulwahn@39655
   811
bulwahn@39655
   812
code_pred tupled_step .
bulwahn@39655
   813
thm tupled_step.equation
bulwahn@39655
   814
bulwahn@39655
   815
subsection {* divmod *}
bulwahn@39655
   816
bulwahn@39655
   817
inductive divmod_rel :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool" where
bulwahn@39655
   818
    "k < l \<Longrightarrow> divmod_rel k l 0 k"
bulwahn@39655
   819
  | "k \<ge> l \<Longrightarrow> divmod_rel (k - l) l q r \<Longrightarrow> divmod_rel k l (Suc q) r"
bulwahn@39655
   820
bulwahn@39655
   821
code_pred divmod_rel .
bulwahn@39655
   822
thm divmod_rel.equation
bulwahn@39655
   823
value [code] "Predicate.the (divmod_rel_i_i_o_o 1705 42)"
bulwahn@39655
   824
bulwahn@39655
   825
subsection {* Transforming predicate logic into logic programs *}
bulwahn@39655
   826
bulwahn@39655
   827
subsection {* Transforming functions into logic programs *}
bulwahn@39655
   828
definition
bulwahn@39655
   829
  "case_f xs ys = (case (xs @ ys) of [] => [] | (x # xs) => xs)"
bulwahn@39655
   830
bulwahn@39655
   831
code_pred [inductify, skip_proof] case_f .
bulwahn@39655
   832
thm case_fP.equation
bulwahn@39655
   833
bulwahn@39655
   834
fun fold_map_idx where
bulwahn@39655
   835
  "fold_map_idx f i y [] = (y, [])"
bulwahn@39655
   836
| "fold_map_idx f i y (x # xs) =
bulwahn@39655
   837
 (let (y', x') = f i y x; (y'', xs') = fold_map_idx f (Suc i) y' xs
bulwahn@39655
   838
 in (y'', x' # xs'))"
bulwahn@39655
   839
bulwahn@39655
   840
code_pred [inductify] fold_map_idx .
bulwahn@39655
   841
bulwahn@39655
   842
subsection {* Minimum *}
bulwahn@39655
   843
bulwahn@39655
   844
definition Min
bulwahn@39655
   845
where "Min s r x \<equiv> s x \<and> (\<forall>y. r x y \<longrightarrow> x = y)"
bulwahn@39655
   846
bulwahn@39655
   847
code_pred [inductify] Min .
bulwahn@39655
   848
thm Min.equation
bulwahn@39655
   849
bulwahn@39655
   850
subsection {* Lexicographic order *}
bulwahn@39655
   851
bulwahn@39655
   852
declare lexord_def[code_pred_def]
bulwahn@39655
   853
code_pred [inductify] lexord .
bulwahn@39655
   854
code_pred [random_dseq inductify] lexord .
bulwahn@39655
   855
bulwahn@39655
   856
thm lexord.equation
bulwahn@39655
   857
thm lexord.random_dseq_equation
bulwahn@39655
   858
bulwahn@39655
   859
inductive less_than_nat :: "nat * nat => bool"
bulwahn@39655
   860
where
bulwahn@39655
   861
  "less_than_nat (0, x)"
bulwahn@39655
   862
| "less_than_nat (x, y) ==> less_than_nat (Suc x, Suc y)"
bulwahn@39655
   863
 
bulwahn@39655
   864
code_pred less_than_nat .
bulwahn@39655
   865
bulwahn@39655
   866
code_pred [dseq] less_than_nat .
bulwahn@39655
   867
code_pred [random_dseq] less_than_nat .
bulwahn@39655
   868
bulwahn@39655
   869
inductive test_lexord :: "nat list * nat list => bool"
bulwahn@39655
   870
where
bulwahn@39655
   871
  "lexord less_than_nat (xs, ys) ==> test_lexord (xs, ys)"
bulwahn@39655
   872
bulwahn@39655
   873
code_pred test_lexord .
bulwahn@39655
   874
code_pred [dseq] test_lexord .
bulwahn@39655
   875
code_pred [random_dseq] test_lexord .
bulwahn@39655
   876
thm test_lexord.dseq_equation
bulwahn@39655
   877
thm test_lexord.random_dseq_equation
bulwahn@39655
   878
bulwahn@39655
   879
values "{x. test_lexord ([1, 2, 3], [1, 2, 5])}"
bulwahn@39655
   880
(*values [depth_limited 5] "{x. test_lexord ([1, 2, 3], [1, 2, 5])}"*)
bulwahn@39655
   881
bulwahn@39655
   882
lemmas [code_pred_def] = lexn_conv lex_conv lenlex_conv
bulwahn@39655
   883
(*
bulwahn@39655
   884
code_pred [inductify] lexn .
bulwahn@39655
   885
thm lexn.equation
bulwahn@39655
   886
*)
bulwahn@39655
   887
(*
bulwahn@39655
   888
code_pred [random_dseq inductify] lexn .
bulwahn@39655
   889
thm lexn.random_dseq_equation
bulwahn@39655
   890
bulwahn@39655
   891
values [random_dseq 4, 4, 6] 100 "{(n, xs, ys::int list). lexn (%(x, y). x <= y) n (xs, ys)}"
bulwahn@39655
   892
*)
bulwahn@39655
   893
inductive has_length
bulwahn@39655
   894
where
bulwahn@39655
   895
  "has_length [] 0"
bulwahn@39655
   896
| "has_length xs i ==> has_length (x # xs) (Suc i)" 
bulwahn@39655
   897
bulwahn@39655
   898
lemma has_length:
bulwahn@39655
   899
  "has_length xs n = (length xs = n)"
bulwahn@39655
   900
proof (rule iffI)
bulwahn@39655
   901
  assume "has_length xs n"
bulwahn@39655
   902
  from this show "length xs = n"
bulwahn@39655
   903
    by (rule has_length.induct) auto
bulwahn@39655
   904
next
bulwahn@39655
   905
  assume "length xs = n"
bulwahn@39655
   906
  from this show "has_length xs n"
bulwahn@39655
   907
    by (induct xs arbitrary: n) (auto intro: has_length.intros)
bulwahn@39655
   908
qed
bulwahn@39655
   909
bulwahn@39655
   910
lemma lexn_intros [code_pred_intro]:
bulwahn@39655
   911
  "has_length xs i ==> has_length ys i ==> r (x, y) ==> lexn r (Suc i) (x # xs, y # ys)"
bulwahn@39655
   912
  "lexn r i (xs, ys) ==> lexn r (Suc i) (x # xs, x # ys)"
bulwahn@39655
   913
proof -
bulwahn@39655
   914
  assume "has_length xs i" "has_length ys i" "r (x, y)"
bulwahn@39655
   915
  from this has_length show "lexn r (Suc i) (x # xs, y # ys)"
bulwahn@39655
   916
    unfolding lexn_conv Collect_def mem_def
bulwahn@39655
   917
    by fastsimp
bulwahn@39655
   918
next
bulwahn@39655
   919
  assume "lexn r i (xs, ys)"
bulwahn@39655
   920
  thm lexn_conv
bulwahn@39655
   921
  from this show "lexn r (Suc i) (x#xs, x#ys)"
bulwahn@39655
   922
    unfolding Collect_def mem_def lexn_conv
bulwahn@39655
   923
    apply auto
bulwahn@39655
   924
    apply (rule_tac x="x # xys" in exI)
bulwahn@39655
   925
    by auto
bulwahn@39655
   926
qed
bulwahn@39655
   927
bulwahn@39655
   928
code_pred [random_dseq] lexn
bulwahn@39655
   929
proof -
bulwahn@39655
   930
  fix r n xs ys
bulwahn@39655
   931
  assume 1: "lexn r n (xs, ys)"
bulwahn@39655
   932
  assume 2: "\<And>r' i x xs' y ys'. r = r' ==> n = Suc i ==> (xs, ys) = (x # xs', y # ys') ==> has_length xs' i ==> has_length ys' i ==> r' (x, y) ==> thesis"
bulwahn@39655
   933
  assume 3: "\<And>r' i x xs' ys'. r = r' ==> n = Suc i ==> (xs, ys) = (x # xs', x # ys') ==> lexn r' i (xs', ys') ==> thesis"
bulwahn@39655
   934
  from 1 2 3 show thesis
bulwahn@39655
   935
    unfolding lexn_conv Collect_def mem_def
bulwahn@39655
   936
    apply (auto simp add: has_length)
bulwahn@39655
   937
    apply (case_tac xys)
bulwahn@39655
   938
    apply auto
bulwahn@39655
   939
    apply fastsimp
bulwahn@39655
   940
    apply fastsimp done
bulwahn@39655
   941
qed
bulwahn@39655
   942
bulwahn@39655
   943
values [random_dseq 1, 2, 5] 10 "{(n, xs, ys::int list). lexn (%(x, y). x <= y) n (xs, ys)}"
bulwahn@39655
   944
bulwahn@39655
   945
code_pred [inductify, skip_proof] lex .
bulwahn@39655
   946
thm lex.equation
bulwahn@39655
   947
thm lex_def
bulwahn@39655
   948
declare lenlex_conv[code_pred_def]
bulwahn@39655
   949
code_pred [inductify, skip_proof] lenlex .
bulwahn@39655
   950
thm lenlex.equation
bulwahn@39655
   951
bulwahn@39655
   952
code_pred [random_dseq inductify] lenlex .
bulwahn@39655
   953
thm lenlex.random_dseq_equation
bulwahn@39655
   954
bulwahn@39655
   955
values [random_dseq 4, 2, 4] 100 "{(xs, ys::int list). lenlex (%(x, y). x <= y) (xs, ys)}"
bulwahn@39655
   956
bulwahn@39655
   957
thm lists.intros
bulwahn@39655
   958
code_pred [inductify] lists .
bulwahn@39655
   959
thm lists.equation
bulwahn@39655
   960
bulwahn@39655
   961
subsection {* AVL Tree *}
bulwahn@39655
   962
bulwahn@39655
   963
datatype 'a tree = ET | MKT 'a "'a tree" "'a tree" nat
bulwahn@39655
   964
fun height :: "'a tree => nat" where
bulwahn@39655
   965
"height ET = 0"
bulwahn@39655
   966
| "height (MKT x l r h) = max (height l) (height r) + 1"
bulwahn@39655
   967
bulwahn@39655
   968
primrec avl :: "'a tree => bool"
bulwahn@39655
   969
where
bulwahn@39655
   970
  "avl ET = True"
bulwahn@39655
   971
| "avl (MKT x l r h) = ((height l = height r \<or> height l = 1 + height r \<or> height r = 1+height l) \<and> 
bulwahn@39655
   972
  h = max (height l) (height r) + 1 \<and> avl l \<and> avl r)"
bulwahn@39655
   973
(*
bulwahn@39655
   974
code_pred [inductify] avl .
bulwahn@39655
   975
thm avl.equation*)
bulwahn@39655
   976
bulwahn@39655
   977
code_pred [new_random_dseq inductify] avl .
bulwahn@39655
   978
thm avl.new_random_dseq_equation
bulwahn@40137
   979
(* TODO: has highly non-deterministic execution time!
bulwahn@39655
   980
bulwahn@39655
   981
values [new_random_dseq 2, 1, 7] 5 "{t:: int tree. avl t}"
bulwahn@40137
   982
*)
bulwahn@39655
   983
fun set_of
bulwahn@39655
   984
where
bulwahn@39655
   985
"set_of ET = {}"
bulwahn@39655
   986
| "set_of (MKT n l r h) = insert n (set_of l \<union> set_of r)"
bulwahn@39655
   987
bulwahn@39655
   988
fun is_ord :: "nat tree => bool"
bulwahn@39655
   989
where
bulwahn@39655
   990
"is_ord ET = True"
bulwahn@39655
   991
| "is_ord (MKT n l r h) =
bulwahn@39655
   992
 ((\<forall>n' \<in> set_of l. n' < n) \<and> (\<forall>n' \<in> set_of r. n < n') \<and> is_ord l \<and> is_ord r)"
bulwahn@39655
   993
bulwahn@39655
   994
code_pred (expected_modes: i => o => bool, i => i => bool) [inductify] set_of .
bulwahn@39655
   995
thm set_of.equation
bulwahn@39655
   996
bulwahn@39655
   997
code_pred (expected_modes: i => bool) [inductify] is_ord .
bulwahn@39655
   998
thm is_ord_aux.equation
bulwahn@39655
   999
thm is_ord.equation
bulwahn@39655
  1000
bulwahn@39655
  1001
subsection {* Definitions about Relations *}
bulwahn@39655
  1002
bulwahn@39655
  1003
term "converse"
bulwahn@39655
  1004
code_pred (modes:
bulwahn@39655
  1005
  (i * i => bool) => i * i => bool,
bulwahn@39655
  1006
  (i * o => bool) => o * i => bool,
bulwahn@39655
  1007
  (i * o => bool) => i * i => bool,
bulwahn@39655
  1008
  (o * i => bool) => i * o => bool,
bulwahn@39655
  1009
  (o * i => bool) => i * i => bool,
bulwahn@39655
  1010
  (o * o => bool) => o * o => bool,
bulwahn@39655
  1011
  (o * o => bool) => i * o => bool,
bulwahn@39655
  1012
  (o * o => bool) => o * i => bool,
bulwahn@39655
  1013
  (o * o => bool) => i * i => bool) [inductify] converse .
bulwahn@39655
  1014
bulwahn@39655
  1015
thm converse.equation
bulwahn@39655
  1016
code_pred [inductify] rel_comp .
bulwahn@39655
  1017
thm rel_comp.equation
bulwahn@39655
  1018
code_pred [inductify] Image .
bulwahn@39655
  1019
thm Image.equation
bulwahn@39655
  1020
declare singleton_iff[code_pred_inline]
bulwahn@39655
  1021
declare Id_on_def[unfolded Bex_def UNION_def singleton_iff, code_pred_def]
bulwahn@39655
  1022
bulwahn@39655
  1023
code_pred (expected_modes:
bulwahn@39655
  1024
  (o => bool) => o => bool,
bulwahn@39655
  1025
  (o => bool) => i * o => bool,
bulwahn@39655
  1026
  (o => bool) => o * i => bool,
bulwahn@39655
  1027
  (o => bool) => i => bool,
bulwahn@39655
  1028
  (i => bool) => i * o => bool,
bulwahn@39655
  1029
  (i => bool) => o * i => bool,
bulwahn@39655
  1030
  (i => bool) => i => bool) [inductify] Id_on .
bulwahn@39655
  1031
thm Id_on.equation
bulwahn@39655
  1032
thm Domain_def
bulwahn@39655
  1033
code_pred (modes:
bulwahn@39655
  1034
  (o * o => bool) => o => bool,
bulwahn@39655
  1035
  (o * o => bool) => i => bool,
bulwahn@39655
  1036
  (i * o => bool) => i => bool) [inductify] Domain .
bulwahn@39655
  1037
thm Domain.equation
bulwahn@39655
  1038
bulwahn@39655
  1039
thm Range_def
bulwahn@39655
  1040
code_pred (modes:
bulwahn@39655
  1041
  (o * o => bool) => o => bool,
bulwahn@39655
  1042
  (o * o => bool) => i => bool,
bulwahn@39655
  1043
  (o * i => bool) => i => bool) [inductify] Range .
bulwahn@39655
  1044
thm Range.equation
bulwahn@39655
  1045
bulwahn@39655
  1046
code_pred [inductify] Field .
bulwahn@39655
  1047
thm Field.equation
bulwahn@39655
  1048
bulwahn@39655
  1049
thm refl_on_def
bulwahn@39655
  1050
code_pred [inductify] refl_on .
bulwahn@39655
  1051
thm refl_on.equation
bulwahn@39655
  1052
code_pred [inductify] total_on .
bulwahn@39655
  1053
thm total_on.equation
bulwahn@39655
  1054
code_pred [inductify] antisym .
bulwahn@39655
  1055
thm antisym.equation
bulwahn@39655
  1056
code_pred [inductify] trans .
bulwahn@39655
  1057
thm trans.equation
bulwahn@39655
  1058
code_pred [inductify] single_valued .
bulwahn@39655
  1059
thm single_valued.equation
bulwahn@39655
  1060
thm inv_image_def
bulwahn@39655
  1061
code_pred [inductify] inv_image .
bulwahn@39655
  1062
thm inv_image.equation
bulwahn@39655
  1063
bulwahn@39655
  1064
subsection {* Inverting list functions *}
bulwahn@39655
  1065
bulwahn@39655
  1066
code_pred [inductify] size_list .
bulwahn@39655
  1067
code_pred [new_random_dseq inductify] size_list .
bulwahn@39655
  1068
thm size_listP.equation
bulwahn@39655
  1069
thm size_listP.new_random_dseq_equation
bulwahn@39655
  1070
bulwahn@39655
  1071
values [new_random_dseq 2,3,10] 3 "{xs. size_listP (xs::nat list) (5::nat)}"
bulwahn@39655
  1072
bulwahn@39655
  1073
code_pred (expected_modes: i => o => bool, o => i => bool, i => i => bool) [inductify, skip_proof] List.concat .
bulwahn@39655
  1074
thm concatP.equation
bulwahn@39655
  1075
bulwahn@39655
  1076
values "{ys. concatP [[1, 2], [3, (4::int)]] ys}"
bulwahn@39655
  1077
values "{ys. concatP [[1, 2], [3]] [1, 2, (3::nat)]}"
bulwahn@39655
  1078
bulwahn@39655
  1079
code_pred [dseq inductify] List.concat .
bulwahn@39655
  1080
thm concatP.dseq_equation
bulwahn@39655
  1081
bulwahn@39655
  1082
values [dseq 3] 3
bulwahn@39655
  1083
  "{xs. concatP xs ([0] :: nat list)}"
bulwahn@39655
  1084
bulwahn@39655
  1085
values [dseq 5] 3
bulwahn@39655
  1086
  "{xs. concatP xs ([1] :: int list)}"
bulwahn@39655
  1087
bulwahn@39655
  1088
values [dseq 5] 3
bulwahn@39655
  1089
  "{xs. concatP xs ([1] :: nat list)}"
bulwahn@39655
  1090
bulwahn@39655
  1091
values [dseq 5] 3
bulwahn@39655
  1092
  "{xs. concatP xs [(1::int), 2]}"
bulwahn@39655
  1093
bulwahn@39655
  1094
code_pred (expected_modes: i => o => bool, i => i => bool) [inductify] hd .
bulwahn@39655
  1095
thm hdP.equation
bulwahn@39655
  1096
values "{x. hdP [1, 2, (3::int)] x}"
bulwahn@39655
  1097
values "{(xs, x). hdP [1, 2, (3::int)] 1}"
bulwahn@39655
  1098
 
bulwahn@39655
  1099
code_pred (expected_modes: i => o => bool, i => i => bool) [inductify] tl .
bulwahn@39655
  1100
thm tlP.equation
bulwahn@39655
  1101
values "{x. tlP [1, 2, (3::nat)] x}"
bulwahn@39655
  1102
values "{x. tlP [1, 2, (3::int)] [3]}"
bulwahn@39655
  1103
bulwahn@39655
  1104
code_pred [inductify, skip_proof] last .
bulwahn@39655
  1105
thm lastP.equation
bulwahn@39655
  1106
bulwahn@39655
  1107
code_pred [inductify, skip_proof] butlast .
bulwahn@39655
  1108
thm butlastP.equation
bulwahn@39655
  1109
bulwahn@39655
  1110
code_pred [inductify, skip_proof] take .
bulwahn@39655
  1111
thm takeP.equation
bulwahn@39655
  1112
bulwahn@39655
  1113
code_pred [inductify, skip_proof] drop .
bulwahn@39655
  1114
thm dropP.equation
bulwahn@39655
  1115
code_pred [inductify, skip_proof] zip .
bulwahn@39655
  1116
thm zipP.equation
bulwahn@39655
  1117
bulwahn@39655
  1118
code_pred [inductify, skip_proof] upt .
bulwahn@39655
  1119
code_pred [inductify, skip_proof] remdups .
bulwahn@39655
  1120
thm remdupsP.equation
bulwahn@39655
  1121
code_pred [dseq inductify] remdups .
bulwahn@39655
  1122
values [dseq 4] 5 "{xs. remdupsP xs [1, (2::int)]}"
bulwahn@39655
  1123
bulwahn@39655
  1124
code_pred [inductify, skip_proof] remove1 .
bulwahn@39655
  1125
thm remove1P.equation
bulwahn@39655
  1126
values "{xs. remove1P 1 xs [2, (3::int)]}"
bulwahn@39655
  1127
bulwahn@39655
  1128
code_pred [inductify, skip_proof] removeAll .
bulwahn@39655
  1129
thm removeAllP.equation
bulwahn@39655
  1130
code_pred [dseq inductify] removeAll .
bulwahn@39655
  1131
bulwahn@39655
  1132
values [dseq 4] 10 "{xs. removeAllP 1 xs [(2::nat)]}"
bulwahn@39655
  1133
bulwahn@39655
  1134
code_pred [inductify] distinct .
bulwahn@39655
  1135
thm distinct.equation
bulwahn@39655
  1136
code_pred [inductify, skip_proof] replicate .
bulwahn@39655
  1137
thm replicateP.equation
bulwahn@39655
  1138
values 5 "{(n, xs). replicateP n (0::int) xs}"
bulwahn@39655
  1139
bulwahn@39655
  1140
code_pred [inductify, skip_proof] splice .
bulwahn@39655
  1141
thm splice.simps
bulwahn@39655
  1142
thm spliceP.equation
bulwahn@39655
  1143
bulwahn@39655
  1144
values "{xs. spliceP xs [1, 2, 3] [1, 1, 1, 2, 1, (3::nat)]}"
bulwahn@39655
  1145
bulwahn@39655
  1146
code_pred [inductify, skip_proof] List.rev .
bulwahn@39655
  1147
code_pred [inductify] map .
bulwahn@39655
  1148
code_pred [inductify] foldr .
bulwahn@39655
  1149
code_pred [inductify] foldl .
bulwahn@39655
  1150
code_pred [inductify] filter .
bulwahn@39655
  1151
code_pred [random_dseq inductify] filter .
bulwahn@39655
  1152
bulwahn@39655
  1153
section {* Function predicate replacement *}
bulwahn@39655
  1154
bulwahn@39655
  1155
text {*
bulwahn@39655
  1156
If the mode analysis uses the functional mode, we
bulwahn@39655
  1157
replace predicates that resulted from functions again by their functions.
bulwahn@39655
  1158
*}
bulwahn@39655
  1159
bulwahn@39655
  1160
inductive test_append
bulwahn@39655
  1161
where
bulwahn@39655
  1162
  "List.append xs ys = zs ==> test_append xs ys zs"
bulwahn@39655
  1163
bulwahn@39655
  1164
code_pred [inductify, skip_proof] test_append .
bulwahn@39655
  1165
thm test_append.equation
bulwahn@39655
  1166
bulwahn@39655
  1167
text {* If append is not turned into a predicate, then the mode
bulwahn@39655
  1168
  o => o => i => bool could not be inferred. *}
bulwahn@39655
  1169
bulwahn@39655
  1170
values 4 "{(xs::int list, ys). test_append xs ys [1, 2, 3, 4]}"
bulwahn@39655
  1171
bulwahn@39655
  1172
text {* If appendP is not reverted back to a function, then mode i => i => o => bool
bulwahn@39655
  1173
  fails after deleting the predicate equation. *}
bulwahn@39655
  1174
bulwahn@39655
  1175
declare appendP.equation[code del]
bulwahn@39655
  1176
bulwahn@39655
  1177
values "{xs::int list. test_append [1,2] [3,4] xs}"
bulwahn@39655
  1178
values "{xs::int list. test_append (replicate 1000 1) (replicate 1000 2) xs}"
bulwahn@39655
  1179
values "{xs::int list. test_append (replicate 2000 1) (replicate 2000 2) xs}"
bulwahn@39655
  1180
bulwahn@39655
  1181
text {* Redeclaring append.equation as code equation *}
bulwahn@39655
  1182
bulwahn@39655
  1183
declare appendP.equation[code]
bulwahn@39655
  1184
bulwahn@39655
  1185
subsection {* Function with tuples *}
bulwahn@39655
  1186
bulwahn@39655
  1187
fun append'
bulwahn@39655
  1188
where
bulwahn@39655
  1189
  "append' ([], ys) = ys"
bulwahn@39655
  1190
| "append' (x # xs, ys) = x # append' (xs, ys)"
bulwahn@39655
  1191
bulwahn@39655
  1192
inductive test_append'
bulwahn@39655
  1193
where
bulwahn@39655
  1194
  "append' (xs, ys) = zs ==> test_append' xs ys zs"
bulwahn@39655
  1195
bulwahn@39655
  1196
code_pred [inductify, skip_proof] test_append' .
bulwahn@39655
  1197
bulwahn@39655
  1198
thm test_append'.equation
bulwahn@39655
  1199
bulwahn@39655
  1200
values "{(xs::int list, ys). test_append' xs ys [1, 2, 3, 4]}"
bulwahn@39655
  1201
bulwahn@39655
  1202
declare append'P.equation[code del]
bulwahn@39655
  1203
bulwahn@39655
  1204
values "{zs :: int list. test_append' [1,2,3] [4,5] zs}"
bulwahn@39655
  1205
bulwahn@39655
  1206
section {* Arithmetic examples *}
bulwahn@39655
  1207
bulwahn@39655
  1208
subsection {* Examples on nat *}
bulwahn@39655
  1209
bulwahn@39655
  1210
inductive plus_nat_test :: "nat => nat => nat => bool"
bulwahn@39655
  1211
where
bulwahn@39655
  1212
  "x + y = z ==> plus_nat_test x y z"
bulwahn@39655
  1213
bulwahn@39655
  1214
code_pred [inductify, skip_proof] plus_nat_test .
bulwahn@39655
  1215
code_pred [new_random_dseq inductify] plus_nat_test .
bulwahn@39655
  1216
bulwahn@39655
  1217
thm plus_nat_test.equation
bulwahn@39655
  1218
thm plus_nat_test.new_random_dseq_equation
bulwahn@39655
  1219
bulwahn@39655
  1220
values [expected "{9::nat}"] "{z. plus_nat_test 4 5 z}"
bulwahn@39655
  1221
values [expected "{9::nat}"] "{z. plus_nat_test 7 2 z}"
bulwahn@39655
  1222
values [expected "{4::nat}"] "{y. plus_nat_test 5 y 9}"
bulwahn@39655
  1223
values [expected "{}"] "{y. plus_nat_test 9 y 8}"
bulwahn@39655
  1224
values [expected "{6::nat}"] "{y. plus_nat_test 1 y 7}"
bulwahn@39655
  1225
values [expected "{2::nat}"] "{x. plus_nat_test x 7 9}"
bulwahn@39655
  1226
values [expected "{}"] "{x. plus_nat_test x 9 7}"
bulwahn@39655
  1227
values [expected "{(0::nat,0::nat)}"] "{(x, y). plus_nat_test x y 0}"
bulwahn@39655
  1228
values [expected "{(0, Suc 0), (Suc 0, 0)}"] "{(x, y). plus_nat_test x y 1}"
bulwahn@39655
  1229
values [expected "{(0, 5), (4, Suc 0), (3, 2), (2, 3), (Suc 0, 4), (5, 0)}"]
bulwahn@39655
  1230
  "{(x, y). plus_nat_test x y 5}"
bulwahn@39655
  1231
bulwahn@39655
  1232
inductive minus_nat_test :: "nat => nat => nat => bool"
bulwahn@39655
  1233
where
bulwahn@39655
  1234
  "x - y = z ==> minus_nat_test x y z"
bulwahn@39655
  1235
bulwahn@39655
  1236
code_pred [inductify, skip_proof] minus_nat_test .
bulwahn@39655
  1237
code_pred [new_random_dseq inductify] minus_nat_test .
bulwahn@39655
  1238
bulwahn@39655
  1239
thm minus_nat_test.equation
bulwahn@39655
  1240
thm minus_nat_test.new_random_dseq_equation
bulwahn@39655
  1241
bulwahn@39655
  1242
values [expected "{0::nat}"] "{z. minus_nat_test 4 5 z}"
bulwahn@39655
  1243
values [expected "{5::nat}"] "{z. minus_nat_test 7 2 z}"
bulwahn@39655
  1244
values [expected "{16::nat}"] "{x. minus_nat_test x 7 9}"
bulwahn@39655
  1245
values [expected "{16::nat}"] "{x. minus_nat_test x 9 7}"
bulwahn@39655
  1246
values [expected "{0, Suc 0, 2, 3}"] "{x. minus_nat_test x 3 0}"
bulwahn@39655
  1247
values [expected "{0::nat}"] "{x. minus_nat_test x 0 0}"
bulwahn@39655
  1248
bulwahn@39655
  1249
subsection {* Examples on int *}
bulwahn@39655
  1250
bulwahn@39655
  1251
inductive plus_int_test :: "int => int => int => bool"
bulwahn@39655
  1252
where
bulwahn@39655
  1253
  "a + b = c ==> plus_int_test a b c"
bulwahn@39655
  1254
bulwahn@39655
  1255
code_pred [inductify, skip_proof] plus_int_test .
bulwahn@39655
  1256
code_pred [new_random_dseq inductify] plus_int_test .
bulwahn@39655
  1257
bulwahn@39655
  1258
thm plus_int_test.equation
bulwahn@39655
  1259
thm plus_int_test.new_random_dseq_equation
bulwahn@39655
  1260
bulwahn@39655
  1261
values [expected "{1::int}"] "{a. plus_int_test a 6 7}"
bulwahn@39655
  1262
values [expected "{1::int}"] "{b. plus_int_test 6 b 7}"
bulwahn@39655
  1263
values [expected "{11::int}"] "{c. plus_int_test 5 6 c}"
bulwahn@39655
  1264
bulwahn@39655
  1265
inductive minus_int_test :: "int => int => int => bool"
bulwahn@39655
  1266
where
bulwahn@39655
  1267
  "a - b = c ==> minus_int_test a b c"
bulwahn@39655
  1268
bulwahn@39655
  1269
code_pred [inductify, skip_proof] minus_int_test .
bulwahn@39655
  1270
code_pred [new_random_dseq inductify] minus_int_test .
bulwahn@39655
  1271
bulwahn@39655
  1272
thm minus_int_test.equation
bulwahn@39655
  1273
thm minus_int_test.new_random_dseq_equation
bulwahn@39655
  1274
bulwahn@39655
  1275
values [expected "{4::int}"] "{c. minus_int_test 9 5 c}"
bulwahn@39655
  1276
values [expected "{9::int}"] "{a. minus_int_test a 4 5}"
haftmann@40885
  1277
values [expected "{-1::int}"] "{b. minus_int_test 4 b 5}"
bulwahn@39655
  1278
bulwahn@39655
  1279
subsection {* minus on bool *}
bulwahn@39655
  1280
bulwahn@39655
  1281
inductive All :: "nat => bool"
bulwahn@39655
  1282
where
bulwahn@39655
  1283
  "All x"
bulwahn@39655
  1284
bulwahn@39655
  1285
inductive None :: "nat => bool"
bulwahn@39655
  1286
bulwahn@39655
  1287
definition "test_minus_bool x = (None x - All x)"
bulwahn@39655
  1288
bulwahn@39655
  1289
code_pred [inductify] test_minus_bool .
bulwahn@39655
  1290
thm test_minus_bool.equation
bulwahn@39655
  1291
bulwahn@39655
  1292
values "{x. test_minus_bool x}"
bulwahn@39655
  1293
bulwahn@39655
  1294
subsection {* Functions *}
bulwahn@39655
  1295
bulwahn@39655
  1296
fun partial_hd :: "'a list => 'a option"
bulwahn@39655
  1297
where
bulwahn@39655
  1298
  "partial_hd [] = Option.None"
bulwahn@39655
  1299
| "partial_hd (x # xs) = Some x"
bulwahn@39655
  1300
bulwahn@39655
  1301
inductive hd_predicate
bulwahn@39655
  1302
where
bulwahn@39655
  1303
  "partial_hd xs = Some x ==> hd_predicate xs x"
bulwahn@39655
  1304
bulwahn@39655
  1305
code_pred (expected_modes: i => i => bool, i => o => bool) hd_predicate .
bulwahn@39655
  1306
bulwahn@39655
  1307
thm hd_predicate.equation
bulwahn@39655
  1308
bulwahn@39655
  1309
subsection {* Locales *}
bulwahn@39655
  1310
bulwahn@39655
  1311
inductive hd_predicate2 :: "('a list => 'a option) => 'a list => 'a => bool"
bulwahn@39655
  1312
where
bulwahn@39655
  1313
  "partial_hd' xs = Some x ==> hd_predicate2 partial_hd' xs x"
bulwahn@39655
  1314
bulwahn@39655
  1315
bulwahn@39655
  1316
thm hd_predicate2.intros
bulwahn@39655
  1317
bulwahn@39655
  1318
code_pred (expected_modes: i => i => i => bool, i => i => o => bool) hd_predicate2 .
bulwahn@39655
  1319
thm hd_predicate2.equation
bulwahn@39655
  1320
bulwahn@39655
  1321
locale A = fixes partial_hd :: "'a list => 'a option" begin
bulwahn@39655
  1322
bulwahn@39655
  1323
inductive hd_predicate_in_locale :: "'a list => 'a => bool"
bulwahn@39655
  1324
where
bulwahn@39655
  1325
  "partial_hd xs = Some x ==> hd_predicate_in_locale xs x"
bulwahn@39655
  1326
bulwahn@39655
  1327
end
bulwahn@39655
  1328
bulwahn@39655
  1329
text {* The global introduction rules must be redeclared as introduction rules and then 
bulwahn@39655
  1330
  one could invoke code_pred. *}
bulwahn@39655
  1331
bulwahn@39657
  1332
declare A.hd_predicate_in_locale.intros [code_pred_intro]
bulwahn@39655
  1333
bulwahn@39655
  1334
code_pred (expected_modes: i => i => i => bool, i => i => o => bool) A.hd_predicate_in_locale
bulwahn@39657
  1335
by (auto elim: A.hd_predicate_in_locale.cases)
bulwahn@39655
  1336
    
bulwahn@39655
  1337
interpretation A partial_hd .
bulwahn@39655
  1338
thm hd_predicate_in_locale.intros
bulwahn@39655
  1339
text {* A locally compliant solution with a trivial interpretation fails, because
bulwahn@39655
  1340
the predicate compiler has very strict assumptions about the terms and their structure. *}
bulwahn@39655
  1341
 
bulwahn@39655
  1342
(*code_pred hd_predicate_in_locale .*)
bulwahn@39655
  1343
bulwahn@39655
  1344
section {* Integer example *}
bulwahn@39655
  1345
bulwahn@39655
  1346
definition three :: int
bulwahn@39655
  1347
where "three = 3"
bulwahn@39655
  1348
bulwahn@39655
  1349
inductive is_three
bulwahn@39655
  1350
where
bulwahn@39655
  1351
  "is_three three"
bulwahn@39655
  1352
bulwahn@39655
  1353
code_pred is_three .
bulwahn@39655
  1354
bulwahn@39655
  1355
thm is_three.equation
bulwahn@39655
  1356
bulwahn@39655
  1357
section {* String.literal example *}
bulwahn@39655
  1358
bulwahn@39655
  1359
definition Error_1
bulwahn@39655
  1360
where
bulwahn@39655
  1361
  "Error_1 = STR ''Error 1''"
bulwahn@39655
  1362
bulwahn@39655
  1363
definition Error_2
bulwahn@39655
  1364
where
bulwahn@39655
  1365
  "Error_2 = STR ''Error 2''"
bulwahn@39655
  1366
bulwahn@39655
  1367
inductive "is_error" :: "String.literal \<Rightarrow> bool"
bulwahn@39655
  1368
where
bulwahn@39655
  1369
  "is_error Error_1"
bulwahn@39655
  1370
| "is_error Error_2"
bulwahn@39655
  1371
bulwahn@39655
  1372
code_pred is_error .
bulwahn@39655
  1373
bulwahn@39655
  1374
thm is_error.equation
bulwahn@39655
  1375
bulwahn@39655
  1376
inductive is_error' :: "String.literal \<Rightarrow> bool"
bulwahn@39655
  1377
where
bulwahn@39655
  1378
  "is_error' (STR ''Error1'')"
bulwahn@39655
  1379
| "is_error' (STR ''Error2'')"
bulwahn@39655
  1380
bulwahn@39655
  1381
code_pred is_error' .
bulwahn@39655
  1382
bulwahn@39655
  1383
thm is_error'.equation
bulwahn@39655
  1384
bulwahn@39655
  1385
datatype ErrorObject = Error String.literal int
bulwahn@39655
  1386
bulwahn@39655
  1387
inductive is_error'' :: "ErrorObject \<Rightarrow> bool"
bulwahn@39655
  1388
where
bulwahn@39655
  1389
  "is_error'' (Error Error_1 3)"
bulwahn@39655
  1390
| "is_error'' (Error Error_2 4)"
bulwahn@39655
  1391
bulwahn@39655
  1392
code_pred is_error'' .
bulwahn@39655
  1393
bulwahn@39655
  1394
thm is_error''.equation
bulwahn@39655
  1395
bulwahn@39655
  1396
section {* Another function example *}
bulwahn@39655
  1397
bulwahn@39655
  1398
consts f :: "'a \<Rightarrow> 'a"
bulwahn@39655
  1399
bulwahn@39655
  1400
inductive fun_upd :: "(('a * 'b) * ('a \<Rightarrow> 'b)) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> bool"
bulwahn@39655
  1401
where
bulwahn@39655
  1402
  "fun_upd ((x, a), s) (s(x := f a))"
bulwahn@39655
  1403
bulwahn@39655
  1404
code_pred fun_upd .
bulwahn@39655
  1405
bulwahn@39655
  1406
thm fun_upd.equation
bulwahn@39655
  1407
bulwahn@39655
  1408
section {* Examples for detecting switches *}
bulwahn@39655
  1409
bulwahn@39655
  1410
inductive detect_switches1 where
bulwahn@39655
  1411
  "detect_switches1 [] []"
bulwahn@39655
  1412
| "detect_switches1 (x # xs) (y # ys)"
bulwahn@39655
  1413
bulwahn@39655
  1414
code_pred [detect_switches, skip_proof] detect_switches1 .
bulwahn@39655
  1415
bulwahn@39655
  1416
thm detect_switches1.equation
bulwahn@39655
  1417
bulwahn@39655
  1418
inductive detect_switches2 :: "('a => bool) => bool"
bulwahn@39655
  1419
where
bulwahn@39655
  1420
  "detect_switches2 P"
bulwahn@39655
  1421
bulwahn@39655
  1422
code_pred [detect_switches, skip_proof] detect_switches2 .
bulwahn@39655
  1423
thm detect_switches2.equation
bulwahn@39655
  1424
bulwahn@39655
  1425
inductive detect_switches3 :: "('a => bool) => 'a list => bool"
bulwahn@39655
  1426
where
bulwahn@39655
  1427
  "detect_switches3 P []"
bulwahn@39655
  1428
| "detect_switches3 P (x # xs)" 
bulwahn@39655
  1429
bulwahn@39655
  1430
code_pred [detect_switches, skip_proof] detect_switches3 .
bulwahn@39655
  1431
bulwahn@39655
  1432
thm detect_switches3.equation
bulwahn@39655
  1433
bulwahn@39655
  1434
inductive detect_switches4 :: "('a => bool) => 'a list => 'a list => bool"
bulwahn@39655
  1435
where
bulwahn@39655
  1436
  "detect_switches4 P [] []"
bulwahn@39655
  1437
| "detect_switches4 P (x # xs) (y # ys)"
bulwahn@39655
  1438
bulwahn@39655
  1439
code_pred [detect_switches, skip_proof] detect_switches4 .
bulwahn@39655
  1440
thm detect_switches4.equation
bulwahn@39655
  1441
bulwahn@39655
  1442
inductive detect_switches5 :: "('a => 'a => bool) => 'a list => 'a list => bool"
bulwahn@39655
  1443
where
bulwahn@39655
  1444
  "detect_switches5 P [] []"
bulwahn@39655
  1445
| "detect_switches5 P xs ys ==> P x y ==> detect_switches5 P (x # xs) (y # ys)"
bulwahn@39655
  1446
bulwahn@39655
  1447
code_pred [detect_switches, skip_proof] detect_switches5 .
bulwahn@39655
  1448
bulwahn@39655
  1449
thm detect_switches5.equation
bulwahn@39655
  1450
bulwahn@39655
  1451
inductive detect_switches6 :: "(('a => 'b => bool) * 'a list * 'b list) => bool"
bulwahn@39655
  1452
where
bulwahn@39655
  1453
  "detect_switches6 (P, [], [])"
bulwahn@39655
  1454
| "detect_switches6 (P, xs, ys) ==> P x y ==> detect_switches6 (P, x # xs, y # ys)"
bulwahn@39655
  1455
bulwahn@39655
  1456
code_pred [detect_switches, skip_proof] detect_switches6 .
bulwahn@39655
  1457
bulwahn@39655
  1458
inductive detect_switches7 :: "('a => bool) => ('b => bool) => ('a * 'b list) => bool"
bulwahn@39655
  1459
where
bulwahn@39655
  1460
  "detect_switches7 P Q (a, [])"
bulwahn@39655
  1461
| "P a ==> Q x ==> detect_switches7 P Q (a, x#xs)"
bulwahn@39655
  1462
bulwahn@39655
  1463
code_pred [skip_proof] detect_switches7 .
bulwahn@39655
  1464
bulwahn@39655
  1465
thm detect_switches7.equation
bulwahn@39655
  1466
bulwahn@39655
  1467
inductive detect_switches8 :: "nat => bool"
bulwahn@39655
  1468
where
bulwahn@39655
  1469
  "detect_switches8 0"
bulwahn@39655
  1470
| "x mod 2 = 0 ==> detect_switches8 (Suc x)"
bulwahn@39655
  1471
bulwahn@39655
  1472
code_pred [detect_switches, skip_proof] detect_switches8 .
bulwahn@39655
  1473
bulwahn@39655
  1474
thm detect_switches8.equation
bulwahn@39655
  1475
bulwahn@39655
  1476
inductive detect_switches9 :: "nat => nat => bool"
bulwahn@39655
  1477
where
bulwahn@39655
  1478
  "detect_switches9 0 0"
bulwahn@39655
  1479
| "detect_switches9 0 (Suc x)"
bulwahn@39655
  1480
| "detect_switches9 (Suc x) 0"
bulwahn@39655
  1481
| "x = y ==> detect_switches9 (Suc x) (Suc y)"
bulwahn@39655
  1482
| "c1 = c2 ==> detect_switches9 c1 c2"
bulwahn@39655
  1483
bulwahn@39655
  1484
code_pred [detect_switches, skip_proof] detect_switches9 .
bulwahn@39655
  1485
bulwahn@39655
  1486
thm detect_switches9.equation
bulwahn@39655
  1487
bulwahn@39762
  1488
text {* The higher-order predicate r is in an output term *}
bulwahn@39762
  1489
bulwahn@39762
  1490
datatype result = Result bool
bulwahn@39762
  1491
bulwahn@39762
  1492
inductive fixed_relation :: "'a => bool"
bulwahn@39762
  1493
bulwahn@39762
  1494
inductive test_relation_in_output_terms :: "('a => bool) => 'a => result => bool"
bulwahn@39762
  1495
where
bulwahn@39762
  1496
  "test_relation_in_output_terms r x (Result (r x))"
bulwahn@39762
  1497
| "test_relation_in_output_terms r x (Result (fixed_relation x))"
bulwahn@39762
  1498
bulwahn@39762
  1499
code_pred test_relation_in_output_terms .
bulwahn@39762
  1500
bulwahn@39762
  1501
thm test_relation_in_output_terms.equation
bulwahn@39655
  1502
bulwahn@39655
  1503
bulwahn@39765
  1504
text {*
bulwahn@39765
  1505
  We want that the argument r is not treated as a higher-order relation, but simply as input.
bulwahn@39765
  1506
*}
bulwahn@39765
  1507
bulwahn@39765
  1508
inductive test_uninterpreted_relation :: "('a => bool) => 'a list => bool"
bulwahn@39765
  1509
where
bulwahn@39765
  1510
  "list_all r xs ==> test_uninterpreted_relation r xs"
bulwahn@39765
  1511
bulwahn@39765
  1512
code_pred (modes: i => i => bool) test_uninterpreted_relation .
bulwahn@39765
  1513
bulwahn@39765
  1514
thm test_uninterpreted_relation.equation
bulwahn@39765
  1515
bulwahn@39786
  1516
inductive list_ex'
bulwahn@39786
  1517
where
bulwahn@39786
  1518
  "P x ==> list_ex' P (x#xs)"
bulwahn@39786
  1519
| "list_ex' P xs ==> list_ex' P (x#xs)"
bulwahn@39786
  1520
bulwahn@39786
  1521
code_pred list_ex' .
bulwahn@39786
  1522
bulwahn@39786
  1523
inductive test_uninterpreted_relation2 :: "('a => bool) => 'a list => bool"
bulwahn@39786
  1524
where
bulwahn@39786
  1525
  "list_ex r xs ==> test_uninterpreted_relation2 r xs"
bulwahn@39786
  1526
| "list_ex' r xs ==> test_uninterpreted_relation2 r xs"
bulwahn@39786
  1527
bulwahn@39786
  1528
text {* Proof procedure cannot handle this situation yet. *}
bulwahn@39786
  1529
bulwahn@39786
  1530
code_pred (modes: i => i => bool) [skip_proof] test_uninterpreted_relation2 .
bulwahn@39786
  1531
bulwahn@39786
  1532
thm test_uninterpreted_relation2.equation
bulwahn@39786
  1533
bulwahn@39786
  1534
bulwahn@39784
  1535
text {* Trivial predicate *}
bulwahn@39784
  1536
bulwahn@39784
  1537
inductive implies_itself :: "'a => bool"
bulwahn@39784
  1538
where
bulwahn@39784
  1539
  "implies_itself x ==> implies_itself x"
bulwahn@39784
  1540
bulwahn@39784
  1541
code_pred implies_itself .
bulwahn@39765
  1542
bulwahn@39803
  1543
text {* Case expressions *}
bulwahn@39803
  1544
bulwahn@39803
  1545
definition
bulwahn@39803
  1546
  "map_pairs xs ys = (map (%((a, b), c). (a, b, c)) xs = ys)"
bulwahn@39803
  1547
bulwahn@39803
  1548
code_pred [inductify] map_pairs .
bulwahn@39765
  1549
bulwahn@39655
  1550
end