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