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