src/HOL/Predicate_Compile_Examples/Predicate_Compile_Tests.thy
author bulwahn
Wed Sep 29 10:33:15 2010 +0200 (2010-09-29)
changeset 39784 cfd06840f477
parent 39765 19cb8d558166
child 39786 30c077288dfe
permissions -rw-r--r--
added a test case to Predicate_Compile_Tests
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@39655
    73
code_pred (expected_modes: []) 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@39655
    79
code_pred (expected_modes: [1]) EmptySet'' .
bulwahn@39655
    80
code_pred (expected_modes: [], [1]) [inductify] EmptySet'' .
bulwahn@39655
    81
*)
bulwahn@39655
    82
bulwahn@39655
    83
consts a' :: 'a
bulwahn@39655
    84
bulwahn@39655
    85
inductive Fact :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
bulwahn@39655
    86
where
bulwahn@39655
    87
"Fact a' a'"
bulwahn@39655
    88
bulwahn@39655
    89
code_pred (expected_modes: o => o => bool, i => o => bool, o => i => bool, i => i => bool) Fact .
bulwahn@39655
    90
bulwahn@39655
    91
inductive zerozero :: "nat * nat => bool"
bulwahn@39655
    92
where
bulwahn@39655
    93
  "zerozero (0, 0)"
bulwahn@39655
    94
bulwahn@39655
    95
code_pred (expected_modes: i => bool, i * o => bool, o * i => bool, o => bool) zerozero .
bulwahn@39655
    96
code_pred [dseq] zerozero .
bulwahn@39655
    97
code_pred [random_dseq] zerozero .
bulwahn@39655
    98
bulwahn@39655
    99
thm zerozero.equation
bulwahn@39655
   100
thm zerozero.dseq_equation
bulwahn@39655
   101
thm zerozero.random_dseq_equation
bulwahn@39655
   102
bulwahn@39655
   103
text {* We expect the user to expand the tuples in the values command.
bulwahn@39655
   104
The following values command is not supported. *}
bulwahn@39655
   105
(*values "{x. zerozero x}" *)
bulwahn@39655
   106
text {* Instead, the user must type *}
bulwahn@39655
   107
values "{(x, y). zerozero (x, y)}"
bulwahn@39655
   108
bulwahn@39655
   109
values [expected "{}" dseq 0] "{(x, y). zerozero (x, y)}"
bulwahn@39655
   110
values [expected "{(0::nat, 0::nat)}" dseq 1] "{(x, y). zerozero (x, y)}"
bulwahn@39655
   111
values [expected "{(0::nat, 0::nat)}" dseq 2] "{(x, y). zerozero (x, y)}"
bulwahn@39655
   112
values [expected "{}" random_dseq 1, 1, 2] "{(x, y). zerozero (x, y)}"
bulwahn@39655
   113
values [expected "{(0::nat, 0:: nat)}" random_dseq 1, 1, 3] "{(x, y). zerozero (x, y)}"
bulwahn@39655
   114
bulwahn@39655
   115
inductive nested_tuples :: "((int * int) * int * int) => bool"
bulwahn@39655
   116
where
bulwahn@39655
   117
  "nested_tuples ((0, 1), 2, 3)"
bulwahn@39655
   118
bulwahn@39655
   119
code_pred nested_tuples .
bulwahn@39655
   120
bulwahn@39655
   121
inductive JamesBond :: "nat => int => code_numeral => bool"
bulwahn@39655
   122
where
bulwahn@39655
   123
  "JamesBond 0 0 7"
bulwahn@39655
   124
bulwahn@39655
   125
code_pred JamesBond .
bulwahn@39655
   126
bulwahn@39655
   127
values [expected "{(0::nat, 0::int , 7::code_numeral)}"] "{(a, b, c). JamesBond a b c}"
bulwahn@39655
   128
values [expected "{(0::nat, 7::code_numeral, 0:: int)}"] "{(a, c, b). JamesBond a b c}"
bulwahn@39655
   129
values [expected "{(0::int, 0::nat, 7::code_numeral)}"] "{(b, a, c). JamesBond a b c}"
bulwahn@39655
   130
values [expected "{(0::int, 7::code_numeral, 0::nat)}"] "{(b, c, a). JamesBond a b c}"
bulwahn@39655
   131
values [expected "{(7::code_numeral, 0::nat, 0::int)}"] "{(c, a, b). JamesBond a b c}"
bulwahn@39655
   132
values [expected "{(7::code_numeral, 0::int, 0::nat)}"] "{(c, b, a). JamesBond a b c}"
bulwahn@39655
   133
bulwahn@39655
   134
values [expected "{(7::code_numeral, 0::int)}"] "{(a, b). JamesBond 0 b a}"
bulwahn@39655
   135
values [expected "{(7::code_numeral, 0::nat)}"] "{(c, a). JamesBond a 0 c}"
bulwahn@39655
   136
values [expected "{(0::nat, 7::code_numeral)}"] "{(a, c). JamesBond a 0 c}"
bulwahn@39655
   137
bulwahn@39655
   138
bulwahn@39655
   139
subsection {* Alternative Rules *}
bulwahn@39655
   140
bulwahn@39655
   141
datatype char = C | D | E | F | G | H
bulwahn@39655
   142
bulwahn@39655
   143
inductive is_C_or_D
bulwahn@39655
   144
where
bulwahn@39655
   145
  "(x = C) \<or> (x = D) ==> is_C_or_D x"
bulwahn@39655
   146
bulwahn@39655
   147
code_pred (expected_modes: i => bool) is_C_or_D .
bulwahn@39655
   148
thm is_C_or_D.equation
bulwahn@39655
   149
bulwahn@39655
   150
inductive is_D_or_E
bulwahn@39655
   151
where
bulwahn@39655
   152
  "(x = D) \<or> (x = E) ==> is_D_or_E x"
bulwahn@39655
   153
bulwahn@39655
   154
lemma [code_pred_intro]:
bulwahn@39655
   155
  "is_D_or_E D"
bulwahn@39655
   156
by (auto intro: is_D_or_E.intros)
bulwahn@39655
   157
bulwahn@39655
   158
lemma [code_pred_intro]:
bulwahn@39655
   159
  "is_D_or_E E"
bulwahn@39655
   160
by (auto intro: is_D_or_E.intros)
bulwahn@39655
   161
bulwahn@39655
   162
code_pred (expected_modes: o => bool, i => bool) is_D_or_E
bulwahn@39655
   163
proof -
bulwahn@39655
   164
  case is_D_or_E
bulwahn@39655
   165
  from is_D_or_E.prems show thesis
bulwahn@39655
   166
  proof 
bulwahn@39655
   167
    fix xa
bulwahn@39655
   168
    assume x: "x = xa"
bulwahn@39655
   169
    assume "xa = D \<or> xa = E"
bulwahn@39655
   170
    from this show thesis
bulwahn@39655
   171
    proof
bulwahn@39655
   172
      assume "xa = D" from this x is_D_or_E(1) show thesis by simp
bulwahn@39655
   173
    next
bulwahn@39655
   174
      assume "xa = E" from this x is_D_or_E(2) show thesis by simp
bulwahn@39655
   175
    qed
bulwahn@39655
   176
  qed
bulwahn@39655
   177
qed
bulwahn@39655
   178
bulwahn@39655
   179
thm is_D_or_E.equation
bulwahn@39655
   180
bulwahn@39655
   181
inductive is_F_or_G
bulwahn@39655
   182
where
bulwahn@39655
   183
  "x = F \<or> x = G ==> is_F_or_G x"
bulwahn@39655
   184
bulwahn@39655
   185
lemma [code_pred_intro]:
bulwahn@39655
   186
  "is_F_or_G F"
bulwahn@39655
   187
by (auto intro: is_F_or_G.intros)
bulwahn@39655
   188
bulwahn@39655
   189
lemma [code_pred_intro]:
bulwahn@39655
   190
  "is_F_or_G G"
bulwahn@39655
   191
by (auto intro: is_F_or_G.intros)
bulwahn@39655
   192
bulwahn@39655
   193
inductive is_FGH
bulwahn@39655
   194
where
bulwahn@39655
   195
  "is_F_or_G x ==> is_FGH x"
bulwahn@39655
   196
| "is_FGH H"
bulwahn@39655
   197
bulwahn@39655
   198
text {* Compilation of is_FGH requires elimination rule for is_F_or_G *}
bulwahn@39655
   199
bulwahn@39655
   200
code_pred (expected_modes: o => bool, i => bool) is_FGH
bulwahn@39655
   201
proof -
bulwahn@39655
   202
  case is_F_or_G
bulwahn@39655
   203
  from is_F_or_G.prems show thesis
bulwahn@39655
   204
  proof
bulwahn@39655
   205
    fix xa
bulwahn@39655
   206
    assume x: "x = xa"
bulwahn@39655
   207
    assume "xa = F \<or> xa = G"
bulwahn@39655
   208
    from this show thesis
bulwahn@39655
   209
    proof
bulwahn@39655
   210
      assume "xa = F"
bulwahn@39655
   211
      from this x is_F_or_G(1) show thesis by simp
bulwahn@39655
   212
    next
bulwahn@39655
   213
      assume "xa = G"
bulwahn@39655
   214
      from this x is_F_or_G(2) show thesis by simp
bulwahn@39655
   215
    qed
bulwahn@39655
   216
  qed
bulwahn@39655
   217
qed
bulwahn@39655
   218
bulwahn@39655
   219
subsection {* Named alternative rules *}
bulwahn@39655
   220
bulwahn@39655
   221
inductive appending
bulwahn@39655
   222
where
bulwahn@39655
   223
  nil: "appending [] ys ys"
bulwahn@39655
   224
| cons: "appending xs ys zs \<Longrightarrow> appending (x#xs) ys (x#zs)"
bulwahn@39655
   225
bulwahn@39655
   226
lemma appending_alt_nil: "ys = zs \<Longrightarrow> appending [] ys zs"
bulwahn@39655
   227
by (auto intro: appending.intros)
bulwahn@39655
   228
bulwahn@39655
   229
lemma appending_alt_cons: "xs' = x # xs \<Longrightarrow> appending xs ys zs \<Longrightarrow> zs' = x # zs \<Longrightarrow> appending xs' ys zs'"
bulwahn@39655
   230
by (auto intro: appending.intros)
bulwahn@39655
   231
bulwahn@39655
   232
text {* With code_pred_intro, we can give fact names to the alternative rules,
bulwahn@39655
   233
  which are used for the code_pred command. *}
bulwahn@39655
   234
bulwahn@39655
   235
declare appending_alt_nil[code_pred_intro alt_nil] appending_alt_cons[code_pred_intro alt_cons]
bulwahn@39655
   236
 
bulwahn@39655
   237
code_pred appending
bulwahn@39655
   238
proof -
bulwahn@39655
   239
  case appending
bulwahn@39655
   240
  from appending.prems show thesis
bulwahn@39655
   241
  proof(cases)
bulwahn@39655
   242
    case nil
bulwahn@39655
   243
    from alt_nil nil show thesis by auto
bulwahn@39655
   244
  next
bulwahn@39655
   245
    case cons
bulwahn@39655
   246
    from alt_cons cons show thesis by fastsimp
bulwahn@39655
   247
  qed
bulwahn@39655
   248
qed
bulwahn@39655
   249
bulwahn@39655
   250
bulwahn@39655
   251
inductive ya_even and ya_odd :: "nat => bool"
bulwahn@39655
   252
where
bulwahn@39655
   253
  even_zero: "ya_even 0"
bulwahn@39655
   254
| odd_plus1: "ya_even x ==> ya_odd (x + 1)"
bulwahn@39655
   255
| even_plus1: "ya_odd x ==> ya_even (x + 1)"
bulwahn@39655
   256
bulwahn@39655
   257
bulwahn@39655
   258
declare even_zero[code_pred_intro even_0]
bulwahn@39655
   259
bulwahn@39655
   260
lemma [code_pred_intro odd_Suc]: "ya_even x ==> ya_odd (Suc x)"
bulwahn@39655
   261
by (auto simp only: Suc_eq_plus1 intro: ya_even_ya_odd.intros)
bulwahn@39655
   262
bulwahn@39655
   263
lemma [code_pred_intro even_Suc]:"ya_odd x ==> ya_even (Suc x)"
bulwahn@39655
   264
by (auto simp only: Suc_eq_plus1 intro: ya_even_ya_odd.intros)
bulwahn@39655
   265
bulwahn@39655
   266
code_pred ya_even
bulwahn@39655
   267
proof -
bulwahn@39655
   268
  case ya_even
bulwahn@39655
   269
  from ya_even.prems show thesis
bulwahn@39655
   270
  proof (cases)
bulwahn@39655
   271
    case even_zero
bulwahn@39655
   272
    from even_zero even_0 show thesis by simp
bulwahn@39655
   273
  next
bulwahn@39655
   274
    case even_plus1
bulwahn@39655
   275
    from even_plus1 even_Suc show thesis by simp
bulwahn@39655
   276
  qed
bulwahn@39655
   277
next
bulwahn@39655
   278
  case ya_odd
bulwahn@39655
   279
  from ya_odd.prems show thesis
bulwahn@39655
   280
  proof (cases)
bulwahn@39655
   281
    case odd_plus1
bulwahn@39655
   282
    from odd_plus1 odd_Suc show thesis by simp
bulwahn@39655
   283
  qed
bulwahn@39655
   284
qed
bulwahn@39655
   285
bulwahn@39655
   286
subsection {* Preprocessor Inlining  *}
bulwahn@39655
   287
bulwahn@39655
   288
definition "equals == (op =)"
bulwahn@39655
   289
 
bulwahn@39655
   290
inductive zerozero' :: "nat * nat => bool" where
bulwahn@39655
   291
  "equals (x, y) (0, 0) ==> zerozero' (x, y)"
bulwahn@39655
   292
bulwahn@39655
   293
code_pred (expected_modes: i => bool) zerozero' .
bulwahn@39655
   294
bulwahn@39655
   295
lemma zerozero'_eq: "zerozero' x == zerozero x"
bulwahn@39655
   296
proof -
bulwahn@39655
   297
  have "zerozero' = zerozero"
bulwahn@39655
   298
    apply (auto simp add: mem_def)
bulwahn@39655
   299
    apply (cases rule: zerozero'.cases)
bulwahn@39655
   300
    apply (auto simp add: equals_def intro: zerozero.intros)
bulwahn@39655
   301
    apply (cases rule: zerozero.cases)
bulwahn@39655
   302
    apply (auto simp add: equals_def intro: zerozero'.intros)
bulwahn@39655
   303
    done
bulwahn@39655
   304
  from this show "zerozero' x == zerozero x" by auto
bulwahn@39655
   305
qed
bulwahn@39655
   306
bulwahn@39655
   307
declare zerozero'_eq [code_pred_inline]
bulwahn@39655
   308
bulwahn@39655
   309
definition "zerozero'' x == zerozero' x"
bulwahn@39655
   310
bulwahn@39655
   311
text {* if preprocessing fails, zerozero'' will not have all modes. *}
bulwahn@39655
   312
bulwahn@39655
   313
code_pred (expected_modes: i * i => bool, i * o => bool, o * i => bool, o => bool) [inductify] zerozero'' .
bulwahn@39655
   314
bulwahn@39655
   315
subsection {* Sets and Numerals *}
bulwahn@39655
   316
bulwahn@39655
   317
definition
bulwahn@39655
   318
  "one_or_two = {Suc 0, (Suc (Suc 0))}"
bulwahn@39655
   319
bulwahn@39655
   320
code_pred [inductify] one_or_two .
bulwahn@39655
   321
bulwahn@39655
   322
code_pred [dseq] one_or_two .
bulwahn@39655
   323
code_pred [random_dseq] one_or_two .
bulwahn@39655
   324
thm one_or_two.dseq_equation
bulwahn@39655
   325
values [expected "{Suc 0::nat, 2::nat}"] "{x. one_or_two x}"
bulwahn@39655
   326
values [random_dseq 0,0,10] 3 "{x. one_or_two x}"
bulwahn@39655
   327
bulwahn@39655
   328
inductive one_or_two' :: "nat => bool"
bulwahn@39655
   329
where
bulwahn@39655
   330
  "one_or_two' 1"
bulwahn@39655
   331
| "one_or_two' 2"
bulwahn@39655
   332
bulwahn@39655
   333
code_pred one_or_two' .
bulwahn@39655
   334
thm one_or_two'.equation
bulwahn@39655
   335
bulwahn@39655
   336
values "{x. one_or_two' x}"
bulwahn@39655
   337
bulwahn@39655
   338
definition one_or_two'':
bulwahn@39655
   339
  "one_or_two'' == {1, (2::nat)}"
bulwahn@39655
   340
bulwahn@39655
   341
code_pred [inductify] one_or_two'' .
bulwahn@39655
   342
thm one_or_two''.equation
bulwahn@39655
   343
bulwahn@39655
   344
values "{x. one_or_two'' x}"
bulwahn@39655
   345
bulwahn@39655
   346
subsection {* even predicate *}
bulwahn@39655
   347
bulwahn@39655
   348
inductive even :: "nat \<Rightarrow> bool" and odd :: "nat \<Rightarrow> bool" where
bulwahn@39655
   349
    "even 0"
bulwahn@39655
   350
  | "even n \<Longrightarrow> odd (Suc n)"
bulwahn@39655
   351
  | "odd n \<Longrightarrow> even (Suc n)"
bulwahn@39655
   352
bulwahn@39655
   353
code_pred (expected_modes: i => bool, o => bool) even .
bulwahn@39655
   354
code_pred [dseq] even .
bulwahn@39655
   355
code_pred [random_dseq] even .
bulwahn@39655
   356
bulwahn@39655
   357
thm odd.equation
bulwahn@39655
   358
thm even.equation
bulwahn@39655
   359
thm odd.dseq_equation
bulwahn@39655
   360
thm even.dseq_equation
bulwahn@39655
   361
thm odd.random_dseq_equation
bulwahn@39655
   362
thm even.random_dseq_equation
bulwahn@39655
   363
bulwahn@39655
   364
values "{x. even 2}"
bulwahn@39655
   365
values "{x. odd 2}"
bulwahn@39655
   366
values 10 "{n. even n}"
bulwahn@39655
   367
values 10 "{n. odd n}"
bulwahn@39655
   368
values [expected "{}" dseq 2] "{x. even 6}"
bulwahn@39655
   369
values [expected "{}" dseq 6] "{x. even 6}"
bulwahn@39655
   370
values [expected "{()}" dseq 7] "{x. even 6}"
bulwahn@39655
   371
values [dseq 2] "{x. odd 7}"
bulwahn@39655
   372
values [dseq 6] "{x. odd 7}"
bulwahn@39655
   373
values [dseq 7] "{x. odd 7}"
bulwahn@39655
   374
values [expected "{()}" dseq 8] "{x. odd 7}"
bulwahn@39655
   375
bulwahn@39655
   376
values [expected "{}" dseq 0] 8 "{x. even x}"
bulwahn@39655
   377
values [expected "{0::nat}" dseq 1] 8 "{x. even x}"
bulwahn@39655
   378
values [expected "{0::nat, 2}" dseq 3] 8 "{x. even x}"
bulwahn@39655
   379
values [expected "{0::nat, 2}" dseq 4] 8 "{x. even x}"
bulwahn@39655
   380
values [expected "{0::nat, 2, 4}" dseq 6] 8 "{x. even x}"
bulwahn@39655
   381
bulwahn@39655
   382
values [random_dseq 1, 1, 0] 8 "{x. even x}"
bulwahn@39655
   383
values [random_dseq 1, 1, 1] 8 "{x. even x}"
bulwahn@39655
   384
values [random_dseq 1, 1, 2] 8 "{x. even x}"
bulwahn@39655
   385
values [random_dseq 1, 1, 3] 8 "{x. even x}"
bulwahn@39655
   386
values [random_dseq 1, 1, 6] 8 "{x. even x}"
bulwahn@39655
   387
bulwahn@39655
   388
values [expected "{}" random_dseq 1, 1, 7] "{x. odd 7}"
bulwahn@39655
   389
values [random_dseq 1, 1, 8] "{x. odd 7}"
bulwahn@39655
   390
values [random_dseq 1, 1, 9] "{x. odd 7}"
bulwahn@39655
   391
bulwahn@39655
   392
definition odd' where "odd' x == \<not> even x"
bulwahn@39655
   393
bulwahn@39655
   394
code_pred (expected_modes: i => bool) [inductify] odd' .
bulwahn@39655
   395
code_pred [dseq inductify] odd' .
bulwahn@39655
   396
code_pred [random_dseq inductify] odd' .
bulwahn@39655
   397
bulwahn@39655
   398
values [expected "{}" dseq 2] "{x. odd' 7}"
bulwahn@39655
   399
values [expected "{()}" dseq 9] "{x. odd' 7}"
bulwahn@39655
   400
values [expected "{}" dseq 2] "{x. odd' 8}"
bulwahn@39655
   401
values [expected "{}" dseq 10] "{x. odd' 8}"
bulwahn@39655
   402
bulwahn@39655
   403
bulwahn@39655
   404
inductive is_even :: "nat \<Rightarrow> bool"
bulwahn@39655
   405
where
bulwahn@39655
   406
  "n mod 2 = 0 \<Longrightarrow> is_even n"
bulwahn@39655
   407
bulwahn@39655
   408
code_pred (expected_modes: i => bool) is_even .
bulwahn@39655
   409
bulwahn@39655
   410
subsection {* append predicate *}
bulwahn@39655
   411
bulwahn@39655
   412
inductive append :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" where
bulwahn@39655
   413
    "append [] xs xs"
bulwahn@39655
   414
  | "append xs ys zs \<Longrightarrow> append (x # xs) ys (x # zs)"
bulwahn@39655
   415
bulwahn@39655
   416
code_pred (modes: i => i => o => bool as "concat", o => o => i => bool as "slice", o => i => i => bool as prefix,
bulwahn@39655
   417
  i => o => i => bool as suffix, i => i => i => bool) append .
bulwahn@39655
   418
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
   419
  i \<Rightarrow> o \<Rightarrow> i \<Rightarrow> bool as suffix, i \<Rightarrow> i \<Rightarrow> i \<Rightarrow> bool) append .
bulwahn@39655
   420
bulwahn@39655
   421
code_pred [dseq] append .
bulwahn@39655
   422
code_pred [random_dseq] append .
bulwahn@39655
   423
bulwahn@39655
   424
thm append.equation
bulwahn@39655
   425
thm append.dseq_equation
bulwahn@39655
   426
thm append.random_dseq_equation
bulwahn@39655
   427
bulwahn@39655
   428
values "{(ys, xs). append xs ys [0, Suc 0, 2]}"
bulwahn@39655
   429
values "{zs. append [0, Suc 0, 2] [17, 8] zs}"
bulwahn@39655
   430
values "{ys. append [0, Suc 0, 2] ys [0, Suc 0, 2, 17, 0, 5]}"
bulwahn@39655
   431
bulwahn@39655
   432
values [expected "{}" dseq 0] 10 "{(xs, ys). append xs ys [1, 2, 3, 4, (5::nat)]}"
bulwahn@39655
   433
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
   434
values [dseq 4] 10 "{(xs, ys). append xs ys [1, 2, 3, 4, (5::nat)]}"
bulwahn@39655
   435
values [dseq 6] 10 "{(xs, ys). append xs ys [1, 2, 3, 4, (5::nat)]}"
bulwahn@39655
   436
values [random_dseq 1, 1, 4] 10 "{(xs, ys). append xs ys [1, 2, 3, 4, (5::nat)]}"
bulwahn@39655
   437
values [random_dseq 1, 1, 1] 10 "{(xs, ys, zs::int list). append xs ys zs}"
bulwahn@39655
   438
values [random_dseq 1, 1, 3] 10 "{(xs, ys, zs::int list). append xs ys zs}"
bulwahn@39655
   439
values [random_dseq 3, 1, 3] 10 "{(xs, ys, zs::int list). append xs ys zs}"
bulwahn@39655
   440
values [random_dseq 1, 3, 3] 10 "{(xs, ys, zs::int list). append xs ys zs}"
bulwahn@39655
   441
values [random_dseq 1, 1, 4] 10 "{(xs, ys, zs::int list). append xs ys zs}"
bulwahn@39655
   442
bulwahn@39655
   443
value [code] "Predicate.the (concat [0::int, 1, 2] [3, 4, 5])"
bulwahn@39655
   444
value [code] "Predicate.the (slice ([]::int list))"
bulwahn@39655
   445
bulwahn@39655
   446
bulwahn@39655
   447
text {* tricky case with alternative rules *}
bulwahn@39655
   448
bulwahn@39655
   449
inductive append2
bulwahn@39655
   450
where
bulwahn@39655
   451
  "append2 [] xs xs"
bulwahn@39655
   452
| "append2 xs ys zs \<Longrightarrow> append2 (x # xs) ys (x # zs)"
bulwahn@39655
   453
bulwahn@39655
   454
lemma append2_Nil: "append2 [] (xs::'b list) xs"
bulwahn@39655
   455
  by (simp add: append2.intros(1))
bulwahn@39655
   456
bulwahn@39655
   457
lemmas [code_pred_intro] = append2_Nil append2.intros(2)
bulwahn@39655
   458
bulwahn@39655
   459
code_pred (expected_modes: i => i => o => bool, o => o => i => bool, o => i => i => bool,
bulwahn@39655
   460
  i => o => i => bool, i => i => i => bool) append2
bulwahn@39655
   461
proof -
bulwahn@39655
   462
  case append2
bulwahn@39655
   463
  from append2.prems show thesis
bulwahn@39655
   464
  proof
bulwahn@39655
   465
    fix xs
bulwahn@39655
   466
    assume "xa = []" "xb = xs" "xc = xs"
bulwahn@39655
   467
    from this append2(1) show thesis by simp
bulwahn@39655
   468
  next
bulwahn@39655
   469
    fix xs ys zs x
bulwahn@39655
   470
    assume "xa = x # xs" "xb = ys" "xc = x # zs" "append2 xs ys zs"
bulwahn@39655
   471
    from this append2(2) show thesis by fastsimp
bulwahn@39655
   472
  qed
bulwahn@39655
   473
qed
bulwahn@39655
   474
bulwahn@39655
   475
inductive tupled_append :: "'a list \<times> 'a list \<times> 'a list \<Rightarrow> bool"
bulwahn@39655
   476
where
bulwahn@39655
   477
  "tupled_append ([], xs, xs)"
bulwahn@39655
   478
| "tupled_append (xs, ys, zs) \<Longrightarrow> tupled_append (x # xs, ys, x # zs)"
bulwahn@39655
   479
bulwahn@39655
   480
code_pred (expected_modes: i * i * o => bool, o * o * i => bool, o * i * i => bool,
bulwahn@39655
   481
  i * o * i => bool, i * i * i => bool) tupled_append .
bulwahn@39655
   482
bulwahn@39655
   483
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
   484
  i \<times> o \<times> i \<Rightarrow> bool, i \<times> i \<times> i \<Rightarrow> bool) tupled_append .
bulwahn@39655
   485
bulwahn@39655
   486
code_pred [random_dseq] tupled_append .
bulwahn@39655
   487
thm tupled_append.equation
bulwahn@39655
   488
bulwahn@39655
   489
values "{xs. tupled_append ([(1::nat), 2, 3], [4, 5], xs)}"
bulwahn@39655
   490
bulwahn@39655
   491
inductive tupled_append'
bulwahn@39655
   492
where
bulwahn@39655
   493
"tupled_append' ([], xs, xs)"
bulwahn@39655
   494
| "[| ys = fst (xa, y); x # zs = snd (xa, y);
bulwahn@39655
   495
 tupled_append' (xs, ys, zs) |] ==> tupled_append' (x # xs, xa, y)"
bulwahn@39655
   496
bulwahn@39655
   497
code_pred (expected_modes: i * i * o => bool, o * o * i => bool, o * i * i => bool,
bulwahn@39655
   498
  i * o * i => bool, i * i * i => bool) tupled_append' .
bulwahn@39655
   499
thm tupled_append'.equation
bulwahn@39655
   500
bulwahn@39655
   501
inductive tupled_append'' :: "'a list \<times> 'a list \<times> 'a list \<Rightarrow> bool"
bulwahn@39655
   502
where
bulwahn@39655
   503
  "tupled_append'' ([], xs, xs)"
bulwahn@39655
   504
| "ys = fst yszs ==> x # zs = snd yszs ==> tupled_append'' (xs, ys, zs) \<Longrightarrow> tupled_append'' (x # xs, yszs)"
bulwahn@39655
   505
bulwahn@39655
   506
code_pred (expected_modes: i * i * o => bool, o * o * i => bool, o * i * i => bool,
bulwahn@39655
   507
  i * o * i => bool, i * i * i => bool) tupled_append'' .
bulwahn@39655
   508
thm tupled_append''.equation
bulwahn@39655
   509
bulwahn@39655
   510
inductive tupled_append''' :: "'a list \<times> 'a list \<times> 'a list \<Rightarrow> bool"
bulwahn@39655
   511
where
bulwahn@39655
   512
  "tupled_append''' ([], xs, xs)"
bulwahn@39655
   513
| "yszs = (ys, zs) ==> tupled_append''' (xs, yszs) \<Longrightarrow> tupled_append''' (x # xs, ys, x # zs)"
bulwahn@39655
   514
bulwahn@39655
   515
code_pred (expected_modes: i * i * o => bool, o * o * i => bool, o * i * i => bool,
bulwahn@39655
   516
  i * o * i => bool, i * i * i => bool) tupled_append''' .
bulwahn@39655
   517
thm tupled_append'''.equation
bulwahn@39655
   518
bulwahn@39655
   519
subsection {* map_ofP predicate *}
bulwahn@39655
   520
bulwahn@39655
   521
inductive map_ofP :: "('a \<times> 'b) list \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
bulwahn@39655
   522
where
bulwahn@39655
   523
  "map_ofP ((a, b)#xs) a b"
bulwahn@39655
   524
| "map_ofP xs a b \<Longrightarrow> map_ofP (x#xs) a b"
bulwahn@39655
   525
bulwahn@39655
   526
code_pred (expected_modes: i => o => o => bool, i => i => o => bool, i => o => i => bool, i => i => i => bool) map_ofP .
bulwahn@39655
   527
thm map_ofP.equation
bulwahn@39655
   528
bulwahn@39655
   529
subsection {* filter predicate *}
bulwahn@39655
   530
bulwahn@39655
   531
inductive filter1
bulwahn@39655
   532
for P
bulwahn@39655
   533
where
bulwahn@39655
   534
  "filter1 P [] []"
bulwahn@39655
   535
| "P x ==> filter1 P xs ys ==> filter1 P (x#xs) (x#ys)"
bulwahn@39655
   536
| "\<not> P x ==> filter1 P xs ys ==> filter1 P (x#xs) ys"
bulwahn@39655
   537
bulwahn@39655
   538
code_pred (expected_modes: (i => bool) => i => o => bool, (i => bool) => i => i => bool) filter1 .
bulwahn@39655
   539
code_pred [dseq] filter1 .
bulwahn@39655
   540
code_pred [random_dseq] filter1 .
bulwahn@39655
   541
bulwahn@39655
   542
thm filter1.equation
bulwahn@39655
   543
bulwahn@39655
   544
values [expected "{[0::nat, 2, 4]}"] "{xs. filter1 even [0, 1, 2, 3, 4] xs}"
bulwahn@39655
   545
values [expected "{}" dseq 9] "{xs. filter1 even [0, 1, 2, 3, 4] xs}"
bulwahn@39655
   546
values [expected "{[0::nat, 2, 4]}" dseq 10] "{xs. filter1 even [0, 1, 2, 3, 4] xs}"
bulwahn@39655
   547
bulwahn@39655
   548
inductive filter2
bulwahn@39655
   549
where
bulwahn@39655
   550
  "filter2 P [] []"
bulwahn@39655
   551
| "P x ==> filter2 P xs ys ==> filter2 P (x#xs) (x#ys)"
bulwahn@39655
   552
| "\<not> P x ==> filter2 P xs ys ==> filter2 P (x#xs) ys"
bulwahn@39655
   553
bulwahn@39655
   554
code_pred (expected_modes: (i => bool) => i => i => bool, (i => bool) => i => o => bool) filter2 .
bulwahn@39655
   555
code_pred [dseq] filter2 .
bulwahn@39655
   556
code_pred [random_dseq] filter2 .
bulwahn@39655
   557
bulwahn@39655
   558
thm filter2.equation
bulwahn@39655
   559
thm filter2.random_dseq_equation
bulwahn@39655
   560
bulwahn@39655
   561
inductive filter3
bulwahn@39655
   562
for P
bulwahn@39655
   563
where
bulwahn@39655
   564
  "List.filter P xs = ys ==> filter3 P xs ys"
bulwahn@39655
   565
bulwahn@39655
   566
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
   567
bulwahn@39655
   568
code_pred filter3 .
bulwahn@39655
   569
thm filter3.equation
bulwahn@39655
   570
bulwahn@39655
   571
(*
bulwahn@39655
   572
inductive filter4
bulwahn@39655
   573
where
bulwahn@39655
   574
  "List.filter P xs = ys ==> filter4 P xs ys"
bulwahn@39655
   575
bulwahn@39655
   576
code_pred (expected_modes: i => i => o => bool, i => i => i => bool) filter4 .
bulwahn@39655
   577
(*code_pred [depth_limited] filter4 .*)
bulwahn@39655
   578
(*code_pred [random] filter4 .*)
bulwahn@39655
   579
*)
bulwahn@39655
   580
subsection {* reverse predicate *}
bulwahn@39655
   581
bulwahn@39655
   582
inductive rev where
bulwahn@39655
   583
    "rev [] []"
bulwahn@39655
   584
  | "rev xs xs' ==> append xs' [x] ys ==> rev (x#xs) ys"
bulwahn@39655
   585
bulwahn@39655
   586
code_pred (expected_modes: i => o => bool, o => i => bool, i => i => bool) rev .
bulwahn@39655
   587
bulwahn@39655
   588
thm rev.equation
bulwahn@39655
   589
bulwahn@39655
   590
values "{xs. rev [0, 1, 2, 3::nat] xs}"
bulwahn@39655
   591
bulwahn@39655
   592
inductive tupled_rev where
bulwahn@39655
   593
  "tupled_rev ([], [])"
bulwahn@39655
   594
| "tupled_rev (xs, xs') \<Longrightarrow> tupled_append (xs', [x], ys) \<Longrightarrow> tupled_rev (x#xs, ys)"
bulwahn@39655
   595
bulwahn@39655
   596
code_pred (expected_modes: i * o => bool, o * i => bool, i * i => bool) tupled_rev .
bulwahn@39655
   597
thm tupled_rev.equation
bulwahn@39655
   598
bulwahn@39655
   599
subsection {* partition predicate *}
bulwahn@39655
   600
bulwahn@39655
   601
inductive partition :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool"
bulwahn@39655
   602
  for f where
bulwahn@39655
   603
    "partition f [] [] []"
bulwahn@39655
   604
  | "f x \<Longrightarrow> partition f xs ys zs \<Longrightarrow> partition f (x # xs) (x # ys) zs"
bulwahn@39655
   605
  | "\<not> f x \<Longrightarrow> partition f xs ys zs \<Longrightarrow> partition f (x # xs) ys (x # zs)"
bulwahn@39655
   606
bulwahn@39655
   607
code_pred (expected_modes: (i => bool) => i => o => o => bool, (i => bool) => o => i => i => bool,
bulwahn@39655
   608
  (i => bool) => i => i => o => bool, (i => bool) => i => o => i => bool, (i => bool) => i => i => i => bool)
bulwahn@39655
   609
  partition .
bulwahn@39655
   610
code_pred [dseq] partition .
bulwahn@39655
   611
code_pred [random_dseq] partition .
bulwahn@39655
   612
bulwahn@39655
   613
values 10 "{(ys, zs). partition is_even
bulwahn@39655
   614
  [0, Suc 0, 2, 3, 4, 5, 6, 7] ys zs}"
bulwahn@39655
   615
values 10 "{zs. partition is_even zs [0, 2] [3, 5]}"
bulwahn@39655
   616
values 10 "{zs. partition is_even zs [0, 7] [3, 5]}"
bulwahn@39655
   617
bulwahn@39655
   618
inductive tupled_partition :: "('a \<Rightarrow> bool) \<Rightarrow> ('a list \<times> 'a list \<times> 'a list) \<Rightarrow> bool"
bulwahn@39655
   619
  for f where
bulwahn@39655
   620
   "tupled_partition f ([], [], [])"
bulwahn@39655
   621
  | "f x \<Longrightarrow> tupled_partition f (xs, ys, zs) \<Longrightarrow> tupled_partition f (x # xs, x # ys, zs)"
bulwahn@39655
   622
  | "\<not> f x \<Longrightarrow> tupled_partition f (xs, ys, zs) \<Longrightarrow> tupled_partition f (x # xs, ys, x # zs)"
bulwahn@39655
   623
bulwahn@39655
   624
code_pred (expected_modes: (i => bool) => i => bool, (i => bool) => (i * i * o) => bool, (i => bool) => (i * o * i) => bool,
bulwahn@39655
   625
  (i => bool) => (o * i * i) => bool, (i => bool) => (i * o * o) => bool) tupled_partition .
bulwahn@39655
   626
bulwahn@39655
   627
thm tupled_partition.equation
bulwahn@39655
   628
bulwahn@39655
   629
lemma [code_pred_intro]:
bulwahn@39655
   630
  "r a b \<Longrightarrow> tranclp r a b"
bulwahn@39655
   631
  "r a b \<Longrightarrow> tranclp r b c \<Longrightarrow> tranclp r a c"
bulwahn@39655
   632
  by auto
bulwahn@39655
   633
bulwahn@39655
   634
subsection {* transitive predicate *}
bulwahn@39655
   635
bulwahn@39655
   636
text {* Also look at the tabled transitive closure in the Library *}
bulwahn@39655
   637
bulwahn@39655
   638
code_pred (modes: (i => o => bool) => i => i => bool, (i => o => bool) => i => o => bool as forwards_trancl,
bulwahn@39655
   639
  (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
   640
  (o => o => bool) => o => i => bool, (o => o => bool) => o => o => bool) tranclp
bulwahn@39655
   641
proof -
bulwahn@39655
   642
  case tranclp
bulwahn@39655
   643
  from this converse_tranclpE[OF tranclp.prems] show thesis by metis
bulwahn@39655
   644
qed
bulwahn@39655
   645
bulwahn@39655
   646
bulwahn@39655
   647
code_pred [dseq] tranclp .
bulwahn@39655
   648
code_pred [random_dseq] tranclp .
bulwahn@39655
   649
thm tranclp.equation
bulwahn@39655
   650
thm tranclp.random_dseq_equation
bulwahn@39655
   651
bulwahn@39655
   652
inductive rtrancl' :: "'a => 'a => ('a => 'a => bool) => bool" 
bulwahn@39655
   653
where
bulwahn@39655
   654
  "rtrancl' x x r"
bulwahn@39655
   655
| "r x y ==> rtrancl' y z r ==> rtrancl' x z r"
bulwahn@39655
   656
bulwahn@39655
   657
code_pred [random_dseq] rtrancl' .
bulwahn@39655
   658
bulwahn@39655
   659
thm rtrancl'.random_dseq_equation
bulwahn@39655
   660
bulwahn@39655
   661
inductive rtrancl'' :: "('a * 'a * ('a \<Rightarrow> 'a \<Rightarrow> bool)) \<Rightarrow> bool"  
bulwahn@39655
   662
where
bulwahn@39655
   663
  "rtrancl'' (x, x, r)"
bulwahn@39655
   664
| "r x y \<Longrightarrow> rtrancl'' (y, z, r) \<Longrightarrow> rtrancl'' (x, z, r)"
bulwahn@39655
   665
bulwahn@39655
   666
code_pred rtrancl'' .
bulwahn@39655
   667
bulwahn@39655
   668
inductive rtrancl''' :: "('a * ('a * 'a) * ('a * 'a => bool)) => bool" 
bulwahn@39655
   669
where
bulwahn@39655
   670
  "rtrancl''' (x, (x, x), r)"
bulwahn@39655
   671
| "r (x, y) ==> rtrancl''' (y, (z, z), r) ==> rtrancl''' (x, (z, z), r)"
bulwahn@39655
   672
bulwahn@39655
   673
code_pred rtrancl''' .
bulwahn@39655
   674
bulwahn@39655
   675
bulwahn@39655
   676
inductive succ :: "nat \<Rightarrow> nat \<Rightarrow> bool" where
bulwahn@39655
   677
    "succ 0 1"
bulwahn@39655
   678
  | "succ m n \<Longrightarrow> succ (Suc m) (Suc n)"
bulwahn@39655
   679
bulwahn@39655
   680
code_pred (modes: i => i => bool, i => o => bool, o => i => bool, o => o => bool) succ .
bulwahn@39655
   681
code_pred [random_dseq] succ .
bulwahn@39655
   682
thm succ.equation
bulwahn@39655
   683
thm succ.random_dseq_equation
bulwahn@39655
   684
bulwahn@39655
   685
values 10 "{(m, n). succ n m}"
bulwahn@39655
   686
values "{m. succ 0 m}"
bulwahn@39655
   687
values "{m. succ m 0}"
bulwahn@39655
   688
bulwahn@39655
   689
text {* values command needs mode annotation of the parameter succ
bulwahn@39655
   690
to disambiguate which mode is to be chosen. *} 
bulwahn@39655
   691
bulwahn@39655
   692
values [mode: i => o => bool] 20 "{n. tranclp succ 10 n}"
bulwahn@39655
   693
values [mode: o => i => bool] 10 "{n. tranclp succ n 10}"
bulwahn@39655
   694
values 20 "{(n, m). tranclp succ n m}"
bulwahn@39655
   695
bulwahn@39655
   696
inductive example_graph :: "int => int => bool"
bulwahn@39655
   697
where
bulwahn@39655
   698
  "example_graph 0 1"
bulwahn@39655
   699
| "example_graph 1 2"
bulwahn@39655
   700
| "example_graph 1 3"
bulwahn@39655
   701
| "example_graph 4 7"
bulwahn@39655
   702
| "example_graph 4 5"
bulwahn@39655
   703
| "example_graph 5 6"
bulwahn@39655
   704
| "example_graph 7 6"
bulwahn@39655
   705
| "example_graph 7 8"
bulwahn@39655
   706
 
bulwahn@39655
   707
inductive not_reachable_in_example_graph :: "int => int => bool"
bulwahn@39655
   708
where "\<not> (tranclp example_graph x y) ==> not_reachable_in_example_graph x y"
bulwahn@39655
   709
bulwahn@39655
   710
code_pred (expected_modes: i => i => bool) not_reachable_in_example_graph .
bulwahn@39655
   711
bulwahn@39655
   712
thm not_reachable_in_example_graph.equation
bulwahn@39655
   713
thm tranclp.equation
bulwahn@39655
   714
value "not_reachable_in_example_graph 0 3"
bulwahn@39655
   715
value "not_reachable_in_example_graph 4 8"
bulwahn@39655
   716
value "not_reachable_in_example_graph 5 6"
bulwahn@39655
   717
text {* rtrancl compilation is strange! *}
bulwahn@39655
   718
(*
bulwahn@39655
   719
value "not_reachable_in_example_graph 0 4"
bulwahn@39655
   720
value "not_reachable_in_example_graph 1 6"
bulwahn@39655
   721
value "not_reachable_in_example_graph 8 4"*)
bulwahn@39655
   722
bulwahn@39655
   723
code_pred [dseq] not_reachable_in_example_graph .
bulwahn@39655
   724
bulwahn@39655
   725
values [dseq 6] "{x. tranclp example_graph 0 3}"
bulwahn@39655
   726
bulwahn@39655
   727
values [dseq 0] "{x. not_reachable_in_example_graph 0 3}"
bulwahn@39655
   728
values [dseq 0] "{x. not_reachable_in_example_graph 0 4}"
bulwahn@39655
   729
values [dseq 20] "{x. not_reachable_in_example_graph 0 4}"
bulwahn@39655
   730
values [dseq 6] "{x. not_reachable_in_example_graph 0 3}"
bulwahn@39655
   731
values [dseq 3] "{x. not_reachable_in_example_graph 4 2}"
bulwahn@39655
   732
values [dseq 6] "{x. not_reachable_in_example_graph 4 2}"
bulwahn@39655
   733
bulwahn@39655
   734
bulwahn@39655
   735
inductive not_reachable_in_example_graph' :: "int => int => bool"
bulwahn@39655
   736
where "\<not> (rtranclp example_graph x y) ==> not_reachable_in_example_graph' x y"
bulwahn@39655
   737
bulwahn@39655
   738
code_pred not_reachable_in_example_graph' .
bulwahn@39655
   739
bulwahn@39655
   740
value "not_reachable_in_example_graph' 0 3"
bulwahn@39655
   741
(* value "not_reachable_in_example_graph' 0 5" would not terminate *)
bulwahn@39655
   742
bulwahn@39655
   743
bulwahn@39655
   744
(*values [depth_limited 0] "{x. not_reachable_in_example_graph' 0 3}"*)
bulwahn@39655
   745
(*values [depth_limited 3] "{x. not_reachable_in_example_graph' 0 3}"*) (* fails with undefined *)
bulwahn@39655
   746
(*values [depth_limited 5] "{x. not_reachable_in_example_graph' 0 3}"*)
bulwahn@39655
   747
(*values [depth_limited 1] "{x. not_reachable_in_example_graph' 0 4}"*)
bulwahn@39655
   748
(*values [depth_limit = 4] "{x. not_reachable_in_example_graph' 0 4}"*) (* fails with undefined *)
bulwahn@39655
   749
(*values [depth_limit = 20] "{x. not_reachable_in_example_graph' 0 4}"*) (* fails with undefined *)
bulwahn@39655
   750
bulwahn@39655
   751
code_pred [dseq] not_reachable_in_example_graph' .
bulwahn@39655
   752
bulwahn@39655
   753
(*thm not_reachable_in_example_graph'.dseq_equation*)
bulwahn@39655
   754
bulwahn@39655
   755
(*values [dseq 0] "{x. not_reachable_in_example_graph' 0 3}"*)
bulwahn@39655
   756
(*values [depth_limited 3] "{x. not_reachable_in_example_graph' 0 3}"*) (* fails with undefined *)
bulwahn@39655
   757
(*values [depth_limited 5] "{x. not_reachable_in_example_graph' 0 3}"
bulwahn@39655
   758
values [depth_limited 1] "{x. not_reachable_in_example_graph' 0 4}"*)
bulwahn@39655
   759
(*values [depth_limit = 4] "{x. not_reachable_in_example_graph' 0 4}"*) (* fails with undefined *)
bulwahn@39655
   760
(*values [depth_limit = 20] "{x. not_reachable_in_example_graph' 0 4}"*) (* fails with undefined *)
bulwahn@39655
   761
bulwahn@39655
   762
subsection {* Free function variable *}
bulwahn@39655
   763
bulwahn@39655
   764
inductive FF :: "nat => nat => bool"
bulwahn@39655
   765
where
bulwahn@39655
   766
  "f x = y ==> FF x y"
bulwahn@39655
   767
bulwahn@39655
   768
code_pred FF .
bulwahn@39655
   769
bulwahn@39655
   770
subsection {* IMP *}
bulwahn@39655
   771
bulwahn@39655
   772
types
bulwahn@39655
   773
  var = nat
bulwahn@39655
   774
  state = "int list"
bulwahn@39655
   775
bulwahn@39655
   776
datatype com =
bulwahn@39655
   777
  Skip |
bulwahn@39655
   778
  Ass var "state => int" |
bulwahn@39655
   779
  Seq com com |
bulwahn@39655
   780
  IF "state => bool" com com |
bulwahn@39655
   781
  While "state => bool" com
bulwahn@39655
   782
bulwahn@39655
   783
inductive tupled_exec :: "(com \<times> state \<times> state) \<Rightarrow> bool" where
bulwahn@39655
   784
"tupled_exec (Skip, s, s)" |
bulwahn@39655
   785
"tupled_exec (Ass x e, s, s[x := e(s)])" |
bulwahn@39655
   786
"tupled_exec (c1, s1, s2) ==> tupled_exec (c2, s2, s3) ==> tupled_exec (Seq c1 c2, s1, s3)" |
bulwahn@39655
   787
"b s ==> tupled_exec (c1, s, t) ==> tupled_exec (IF b c1 c2, s, t)" |
bulwahn@39655
   788
"~b s ==> tupled_exec (c2, s, t) ==> tupled_exec (IF b c1 c2, s, t)" |
bulwahn@39655
   789
"~b s ==> tupled_exec (While b c, s, s)" |
bulwahn@39655
   790
"b s1 ==> tupled_exec (c, s1, s2) ==> tupled_exec (While b c, s2, s3) ==> tupled_exec (While b c, s1, s3)"
bulwahn@39655
   791
bulwahn@39655
   792
code_pred tupled_exec .
bulwahn@39655
   793
bulwahn@39655
   794
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
   795
bulwahn@39655
   796
subsection {* CCS *}
bulwahn@39655
   797
bulwahn@39655
   798
text{* This example formalizes finite CCS processes without communication or
bulwahn@39655
   799
recursion. For simplicity, labels are natural numbers. *}
bulwahn@39655
   800
bulwahn@39655
   801
datatype proc = nil | pre nat proc | or proc proc | par proc proc
bulwahn@39655
   802
bulwahn@39655
   803
inductive tupled_step :: "(proc \<times> nat \<times> proc) \<Rightarrow> bool"
bulwahn@39655
   804
where
bulwahn@39655
   805
"tupled_step (pre n p, n, p)" |
bulwahn@39655
   806
"tupled_step (p1, a, q) \<Longrightarrow> tupled_step (or p1 p2, a, q)" |
bulwahn@39655
   807
"tupled_step (p2, a, q) \<Longrightarrow> tupled_step (or p1 p2, a, q)" |
bulwahn@39655
   808
"tupled_step (p1, a, q) \<Longrightarrow> tupled_step (par p1 p2, a, par q p2)" |
bulwahn@39655
   809
"tupled_step (p2, a, q) \<Longrightarrow> tupled_step (par p1 p2, a, par p1 q)"
bulwahn@39655
   810
bulwahn@39655
   811
code_pred tupled_step .
bulwahn@39655
   812
thm tupled_step.equation
bulwahn@39655
   813
bulwahn@39655
   814
subsection {* divmod *}
bulwahn@39655
   815
bulwahn@39655
   816
inductive divmod_rel :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool" where
bulwahn@39655
   817
    "k < l \<Longrightarrow> divmod_rel k l 0 k"
bulwahn@39655
   818
  | "k \<ge> l \<Longrightarrow> divmod_rel (k - l) l q r \<Longrightarrow> divmod_rel k l (Suc q) r"
bulwahn@39655
   819
bulwahn@39655
   820
code_pred divmod_rel .
bulwahn@39655
   821
thm divmod_rel.equation
bulwahn@39655
   822
value [code] "Predicate.the (divmod_rel_i_i_o_o 1705 42)"
bulwahn@39655
   823
bulwahn@39655
   824
subsection {* Transforming predicate logic into logic programs *}
bulwahn@39655
   825
bulwahn@39655
   826
subsection {* Transforming functions into logic programs *}
bulwahn@39655
   827
definition
bulwahn@39655
   828
  "case_f xs ys = (case (xs @ ys) of [] => [] | (x # xs) => xs)"
bulwahn@39655
   829
bulwahn@39655
   830
code_pred [inductify, skip_proof] case_f .
bulwahn@39655
   831
thm case_fP.equation
bulwahn@39655
   832
bulwahn@39655
   833
fun fold_map_idx where
bulwahn@39655
   834
  "fold_map_idx f i y [] = (y, [])"
bulwahn@39655
   835
| "fold_map_idx f i y (x # xs) =
bulwahn@39655
   836
 (let (y', x') = f i y x; (y'', xs') = fold_map_idx f (Suc i) y' xs
bulwahn@39655
   837
 in (y'', x' # xs'))"
bulwahn@39655
   838
bulwahn@39655
   839
code_pred [inductify] fold_map_idx .
bulwahn@39655
   840
bulwahn@39655
   841
subsection {* Minimum *}
bulwahn@39655
   842
bulwahn@39655
   843
definition Min
bulwahn@39655
   844
where "Min s r x \<equiv> s x \<and> (\<forall>y. r x y \<longrightarrow> x = y)"
bulwahn@39655
   845
bulwahn@39655
   846
code_pred [inductify] Min .
bulwahn@39655
   847
thm Min.equation
bulwahn@39655
   848
bulwahn@39655
   849
subsection {* Lexicographic order *}
bulwahn@39655
   850
bulwahn@39655
   851
declare lexord_def[code_pred_def]
bulwahn@39655
   852
code_pred [inductify] lexord .
bulwahn@39655
   853
code_pred [random_dseq inductify] lexord .
bulwahn@39655
   854
bulwahn@39655
   855
thm lexord.equation
bulwahn@39655
   856
thm lexord.random_dseq_equation
bulwahn@39655
   857
bulwahn@39655
   858
inductive less_than_nat :: "nat * nat => bool"
bulwahn@39655
   859
where
bulwahn@39655
   860
  "less_than_nat (0, x)"
bulwahn@39655
   861
| "less_than_nat (x, y) ==> less_than_nat (Suc x, Suc y)"
bulwahn@39655
   862
 
bulwahn@39655
   863
code_pred less_than_nat .
bulwahn@39655
   864
bulwahn@39655
   865
code_pred [dseq] less_than_nat .
bulwahn@39655
   866
code_pred [random_dseq] less_than_nat .
bulwahn@39655
   867
bulwahn@39655
   868
inductive test_lexord :: "nat list * nat list => bool"
bulwahn@39655
   869
where
bulwahn@39655
   870
  "lexord less_than_nat (xs, ys) ==> test_lexord (xs, ys)"
bulwahn@39655
   871
bulwahn@39655
   872
code_pred test_lexord .
bulwahn@39655
   873
code_pred [dseq] test_lexord .
bulwahn@39655
   874
code_pred [random_dseq] test_lexord .
bulwahn@39655
   875
thm test_lexord.dseq_equation
bulwahn@39655
   876
thm test_lexord.random_dseq_equation
bulwahn@39655
   877
bulwahn@39655
   878
values "{x. test_lexord ([1, 2, 3], [1, 2, 5])}"
bulwahn@39655
   879
(*values [depth_limited 5] "{x. test_lexord ([1, 2, 3], [1, 2, 5])}"*)
bulwahn@39655
   880
bulwahn@39655
   881
lemmas [code_pred_def] = lexn_conv lex_conv lenlex_conv
bulwahn@39655
   882
(*
bulwahn@39655
   883
code_pred [inductify] lexn .
bulwahn@39655
   884
thm lexn.equation
bulwahn@39655
   885
*)
bulwahn@39655
   886
(*
bulwahn@39655
   887
code_pred [random_dseq inductify] lexn .
bulwahn@39655
   888
thm lexn.random_dseq_equation
bulwahn@39655
   889
bulwahn@39655
   890
values [random_dseq 4, 4, 6] 100 "{(n, xs, ys::int list). lexn (%(x, y). x <= y) n (xs, ys)}"
bulwahn@39655
   891
*)
bulwahn@39655
   892
inductive has_length
bulwahn@39655
   893
where
bulwahn@39655
   894
  "has_length [] 0"
bulwahn@39655
   895
| "has_length xs i ==> has_length (x # xs) (Suc i)" 
bulwahn@39655
   896
bulwahn@39655
   897
lemma has_length:
bulwahn@39655
   898
  "has_length xs n = (length xs = n)"
bulwahn@39655
   899
proof (rule iffI)
bulwahn@39655
   900
  assume "has_length xs n"
bulwahn@39655
   901
  from this show "length xs = n"
bulwahn@39655
   902
    by (rule has_length.induct) auto
bulwahn@39655
   903
next
bulwahn@39655
   904
  assume "length xs = n"
bulwahn@39655
   905
  from this show "has_length xs n"
bulwahn@39655
   906
    by (induct xs arbitrary: n) (auto intro: has_length.intros)
bulwahn@39655
   907
qed
bulwahn@39655
   908
bulwahn@39655
   909
lemma lexn_intros [code_pred_intro]:
bulwahn@39655
   910
  "has_length xs i ==> has_length ys i ==> r (x, y) ==> lexn r (Suc i) (x # xs, y # ys)"
bulwahn@39655
   911
  "lexn r i (xs, ys) ==> lexn r (Suc i) (x # xs, x # ys)"
bulwahn@39655
   912
proof -
bulwahn@39655
   913
  assume "has_length xs i" "has_length ys i" "r (x, y)"
bulwahn@39655
   914
  from this has_length show "lexn r (Suc i) (x # xs, y # ys)"
bulwahn@39655
   915
    unfolding lexn_conv Collect_def mem_def
bulwahn@39655
   916
    by fastsimp
bulwahn@39655
   917
next
bulwahn@39655
   918
  assume "lexn r i (xs, ys)"
bulwahn@39655
   919
  thm lexn_conv
bulwahn@39655
   920
  from this show "lexn r (Suc i) (x#xs, x#ys)"
bulwahn@39655
   921
    unfolding Collect_def mem_def lexn_conv
bulwahn@39655
   922
    apply auto
bulwahn@39655
   923
    apply (rule_tac x="x # xys" in exI)
bulwahn@39655
   924
    by auto
bulwahn@39655
   925
qed
bulwahn@39655
   926
bulwahn@39655
   927
code_pred [random_dseq] lexn
bulwahn@39655
   928
proof -
bulwahn@39655
   929
  fix r n xs ys
bulwahn@39655
   930
  assume 1: "lexn r n (xs, ys)"
bulwahn@39655
   931
  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
   932
  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
   933
  from 1 2 3 show thesis
bulwahn@39655
   934
    unfolding lexn_conv Collect_def mem_def
bulwahn@39655
   935
    apply (auto simp add: has_length)
bulwahn@39655
   936
    apply (case_tac xys)
bulwahn@39655
   937
    apply auto
bulwahn@39655
   938
    apply fastsimp
bulwahn@39655
   939
    apply fastsimp done
bulwahn@39655
   940
qed
bulwahn@39655
   941
bulwahn@39655
   942
values [random_dseq 1, 2, 5] 10 "{(n, xs, ys::int list). lexn (%(x, y). x <= y) n (xs, ys)}"
bulwahn@39655
   943
bulwahn@39655
   944
code_pred [inductify, skip_proof] lex .
bulwahn@39655
   945
thm lex.equation
bulwahn@39655
   946
thm lex_def
bulwahn@39655
   947
declare lenlex_conv[code_pred_def]
bulwahn@39655
   948
code_pred [inductify, skip_proof] lenlex .
bulwahn@39655
   949
thm lenlex.equation
bulwahn@39655
   950
bulwahn@39655
   951
code_pred [random_dseq inductify] lenlex .
bulwahn@39655
   952
thm lenlex.random_dseq_equation
bulwahn@39655
   953
bulwahn@39655
   954
values [random_dseq 4, 2, 4] 100 "{(xs, ys::int list). lenlex (%(x, y). x <= y) (xs, ys)}"
bulwahn@39655
   955
bulwahn@39655
   956
thm lists.intros
bulwahn@39655
   957
code_pred [inductify] lists .
bulwahn@39655
   958
thm lists.equation
bulwahn@39655
   959
bulwahn@39655
   960
subsection {* AVL Tree *}
bulwahn@39655
   961
bulwahn@39655
   962
datatype 'a tree = ET | MKT 'a "'a tree" "'a tree" nat
bulwahn@39655
   963
fun height :: "'a tree => nat" where
bulwahn@39655
   964
"height ET = 0"
bulwahn@39655
   965
| "height (MKT x l r h) = max (height l) (height r) + 1"
bulwahn@39655
   966
bulwahn@39655
   967
primrec avl :: "'a tree => bool"
bulwahn@39655
   968
where
bulwahn@39655
   969
  "avl ET = True"
bulwahn@39655
   970
| "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
   971
  h = max (height l) (height r) + 1 \<and> avl l \<and> avl r)"
bulwahn@39655
   972
(*
bulwahn@39655
   973
code_pred [inductify] avl .
bulwahn@39655
   974
thm avl.equation*)
bulwahn@39655
   975
bulwahn@39655
   976
code_pred [new_random_dseq inductify] avl .
bulwahn@39655
   977
thm avl.new_random_dseq_equation
bulwahn@39655
   978
bulwahn@39655
   979
values [new_random_dseq 2, 1, 7] 5 "{t:: int tree. avl t}"
bulwahn@39655
   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}"
bulwahn@39655
  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@39784
  1514
text {* Trivial predicate *}
bulwahn@39784
  1515
bulwahn@39784
  1516
inductive implies_itself :: "'a => bool"
bulwahn@39784
  1517
where
bulwahn@39784
  1518
  "implies_itself x ==> implies_itself x"
bulwahn@39784
  1519
bulwahn@39784
  1520
code_pred implies_itself .
bulwahn@39765
  1521
bulwahn@39765
  1522
bulwahn@39655
  1523
end