src/HOL/Predicate_Compile_Examples/Context_Free_Grammar_Example.thy
author bulwahn
Thu Sep 16 13:49:08 2010 +0200 (2010-09-16)
changeset 39463 7ce0ed8dc4d6
parent 39189 d183bf90dabd
child 39800 17e29ddd538e
permissions -rw-r--r--
adapting examples
bulwahn@38962
     1
theory Context_Free_Grammar_Example
bulwahn@38962
     2
imports Code_Prolog
bulwahn@38962
     3
begin
bulwahn@38962
     4
bulwahn@38962
     5
declare mem_def[code_pred_inline]
bulwahn@38962
     6
bulwahn@38962
     7
bulwahn@38962
     8
subsection {* Alternative rules for length *}
bulwahn@38962
     9
bulwahn@38962
    10
definition size_list :: "'a list => nat"
bulwahn@38962
    11
where "size_list = size"
bulwahn@38962
    12
bulwahn@38962
    13
lemma size_list_simps:
bulwahn@38962
    14
  "size_list [] = 0"
bulwahn@38962
    15
  "size_list (x # xs) = Suc (size_list xs)"
bulwahn@38962
    16
by (auto simp add: size_list_def)
bulwahn@38962
    17
bulwahn@38962
    18
declare size_list_simps[code_pred_def]
bulwahn@38962
    19
declare size_list_def[symmetric, code_pred_inline]
bulwahn@38962
    20
bulwahn@38962
    21
bulwahn@39463
    22
setup {* Context.theory_map (Quickcheck.add_generator ("prolog", Code_Prolog.quickcheck)) *}
bulwahn@38962
    23
bulwahn@38962
    24
datatype alphabet = a | b
bulwahn@38962
    25
bulwahn@38962
    26
inductive_set S\<^isub>1 and A\<^isub>1 and B\<^isub>1 where
bulwahn@38962
    27
  "[] \<in> S\<^isub>1"
bulwahn@38962
    28
| "w \<in> A\<^isub>1 \<Longrightarrow> b # w \<in> S\<^isub>1"
bulwahn@38962
    29
| "w \<in> B\<^isub>1 \<Longrightarrow> a # w \<in> S\<^isub>1"
bulwahn@38962
    30
| "w \<in> S\<^isub>1 \<Longrightarrow> a # w \<in> A\<^isub>1"
bulwahn@38962
    31
| "w \<in> S\<^isub>1 \<Longrightarrow> b # w \<in> S\<^isub>1"
bulwahn@38962
    32
| "\<lbrakk>v \<in> B\<^isub>1; v \<in> B\<^isub>1\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>1"
bulwahn@38962
    33
bulwahn@38962
    34
lemma
bulwahn@38962
    35
  "w \<in> S\<^isub>1 \<Longrightarrow> w = []"
bulwahn@38962
    36
quickcheck[generator = prolog, iterations=1, expect = counterexample]
bulwahn@38962
    37
oops
bulwahn@38962
    38
bulwahn@38962
    39
definition "filter_a = filter (\<lambda>x. x = a)"
bulwahn@38962
    40
bulwahn@38962
    41
lemma [code_pred_def]: "filter_a [] = []"
bulwahn@38962
    42
unfolding filter_a_def by simp
bulwahn@38962
    43
bulwahn@38962
    44
lemma [code_pred_def]: "filter_a (x#xs) = (if x = a then x # filter_a xs else filter_a xs)"
bulwahn@38962
    45
unfolding filter_a_def by simp
bulwahn@38962
    46
bulwahn@38962
    47
declare filter_a_def[symmetric, code_pred_inline]
bulwahn@38962
    48
bulwahn@38962
    49
definition "filter_b = filter (\<lambda>x. x = b)"
bulwahn@38962
    50
bulwahn@38962
    51
lemma [code_pred_def]: "filter_b [] = []"
bulwahn@38962
    52
unfolding filter_b_def by simp
bulwahn@38962
    53
bulwahn@38962
    54
lemma [code_pred_def]: "filter_b (x#xs) = (if x = b then x # filter_b xs else filter_b xs)"
bulwahn@38962
    55
unfolding filter_b_def by simp
bulwahn@38962
    56
bulwahn@38962
    57
declare filter_b_def[symmetric, code_pred_inline]
bulwahn@38962
    58
bulwahn@38962
    59
setup {* Code_Prolog.map_code_options (K
bulwahn@38962
    60
  {ensure_groundness = true,
bulwahn@38962
    61
  limited_types = [],
bulwahn@38962
    62
  limited_predicates = [(["s1", "a1", "b1"], 2)],
bulwahn@38962
    63
  replacing = [(("s1", "limited_s1"), "quickcheck")],
bulwahn@39463
    64
  manual_reorder = [(("quickcheck", 1), [0,2,1,4,3,5])]}) *}
bulwahn@38962
    65
bulwahn@38962
    66
bulwahn@38962
    67
theorem S\<^isub>1_sound:
bulwahn@38962
    68
"w \<in> S\<^isub>1 \<Longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
bulwahn@38962
    69
quickcheck[generator = prolog, iterations=1, expect = counterexample]
bulwahn@38962
    70
oops
bulwahn@38962
    71
bulwahn@38962
    72
bulwahn@38962
    73
inductive_set S\<^isub>2 and A\<^isub>2 and B\<^isub>2 where
bulwahn@38962
    74
  "[] \<in> S\<^isub>2"
bulwahn@38962
    75
| "w \<in> A\<^isub>2 \<Longrightarrow> b # w \<in> S\<^isub>2"
bulwahn@38962
    76
| "w \<in> B\<^isub>2 \<Longrightarrow> a # w \<in> S\<^isub>2"
bulwahn@38962
    77
| "w \<in> S\<^isub>2 \<Longrightarrow> a # w \<in> A\<^isub>2"
bulwahn@38962
    78
| "w \<in> S\<^isub>2 \<Longrightarrow> b # w \<in> B\<^isub>2"
bulwahn@38962
    79
| "\<lbrakk>v \<in> B\<^isub>2; v \<in> B\<^isub>2\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>2"
bulwahn@38962
    80
bulwahn@38962
    81
bulwahn@38962
    82
setup {* Code_Prolog.map_code_options (K
bulwahn@38962
    83
  {ensure_groundness = true,
bulwahn@38962
    84
  limited_types = [],
bulwahn@38962
    85
  limited_predicates = [(["s2", "a2", "b2"], 3)],
bulwahn@38962
    86
  replacing = [(("s2", "limited_s2"), "quickcheck")],
bulwahn@39463
    87
  manual_reorder = [(("quickcheck", 1), [0,2,1,4,3,5])]}) *}
bulwahn@38962
    88
bulwahn@38962
    89
bulwahn@38962
    90
theorem S\<^isub>2_sound:
bulwahn@38962
    91
"w \<in> S\<^isub>2 \<longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
bulwahn@38962
    92
quickcheck[generator=prolog, iterations=1, expect = counterexample]
bulwahn@38962
    93
oops
bulwahn@38962
    94
bulwahn@38962
    95
inductive_set S\<^isub>3 and A\<^isub>3 and B\<^isub>3 where
bulwahn@38962
    96
  "[] \<in> S\<^isub>3"
bulwahn@38962
    97
| "w \<in> A\<^isub>3 \<Longrightarrow> b # w \<in> S\<^isub>3"
bulwahn@38962
    98
| "w \<in> B\<^isub>3 \<Longrightarrow> a # w \<in> S\<^isub>3"
bulwahn@38962
    99
| "w \<in> S\<^isub>3 \<Longrightarrow> a # w \<in> A\<^isub>3"
bulwahn@38962
   100
| "w \<in> S\<^isub>3 \<Longrightarrow> b # w \<in> B\<^isub>3"
bulwahn@38962
   101
| "\<lbrakk>v \<in> B\<^isub>3; w \<in> B\<^isub>3\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>3"
bulwahn@38962
   102
bulwahn@38962
   103
bulwahn@38962
   104
setup {* Code_Prolog.map_code_options (K
bulwahn@38962
   105
  {ensure_groundness = true,
bulwahn@38962
   106
  limited_types = [],
bulwahn@38962
   107
  limited_predicates = [(["s3", "a3", "b3"], 6)],
bulwahn@38962
   108
  replacing = [(("s3", "limited_s3"), "quickcheck")],
bulwahn@39463
   109
  manual_reorder = [(("quickcheck", 1), [0,2,1,4,3,5])]}) *}
bulwahn@38962
   110
bulwahn@38962
   111
lemma S\<^isub>3_sound:
bulwahn@38962
   112
"w \<in> S\<^isub>3 \<longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
bulwahn@38962
   113
quickcheck[generator=prolog, iterations=1, size=1, expect = no_counterexample]
bulwahn@38962
   114
oops
bulwahn@38962
   115
bulwahn@38962
   116
bulwahn@38962
   117
(*
bulwahn@38962
   118
setup {* Code_Prolog.map_code_options (K
bulwahn@38962
   119
  {ensure_groundness = true,
bulwahn@38962
   120
  limited_types = [],
bulwahn@38962
   121
  limited_predicates = [],
bulwahn@38962
   122
  replacing = [],
bulwahn@38962
   123
  manual_reorder = [],
bulwahn@39189
   124
  timeout = Time.fromSeconds 10,
bulwahn@38962
   125
  prolog_system = Code_Prolog.SWI_PROLOG}) *}
bulwahn@38962
   126
bulwahn@38962
   127
bulwahn@38962
   128
theorem S\<^isub>3_complete:
bulwahn@38962
   129
"length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b] \<longrightarrow> w \<in> S\<^isub>3"
bulwahn@38962
   130
quickcheck[generator = prolog, size=1, iterations=1]
bulwahn@38962
   131
oops
bulwahn@38962
   132
*)
bulwahn@38962
   133
bulwahn@38962
   134
inductive_set S\<^isub>4 and A\<^isub>4 and B\<^isub>4 where
bulwahn@38962
   135
  "[] \<in> S\<^isub>4"
bulwahn@38962
   136
| "w \<in> A\<^isub>4 \<Longrightarrow> b # w \<in> S\<^isub>4"
bulwahn@38962
   137
| "w \<in> B\<^isub>4 \<Longrightarrow> a # w \<in> S\<^isub>4"
bulwahn@38962
   138
| "w \<in> S\<^isub>4 \<Longrightarrow> a # w \<in> A\<^isub>4"
bulwahn@38962
   139
| "\<lbrakk>v \<in> A\<^isub>4; w \<in> A\<^isub>4\<rbrakk> \<Longrightarrow> b # v @ w \<in> A\<^isub>4"
bulwahn@38962
   140
| "w \<in> S\<^isub>4 \<Longrightarrow> b # w \<in> B\<^isub>4"
bulwahn@38962
   141
| "\<lbrakk>v \<in> B\<^isub>4; w \<in> B\<^isub>4\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>4"
bulwahn@38962
   142
bulwahn@38962
   143
bulwahn@38962
   144
setup {* Code_Prolog.map_code_options (K
bulwahn@38962
   145
  {ensure_groundness = true,
bulwahn@38962
   146
  limited_types = [],
bulwahn@38962
   147
  limited_predicates = [(["s4", "a4", "b4"], 6)],
bulwahn@38962
   148
  replacing = [(("s4", "limited_s4"), "quickcheck")],
bulwahn@39463
   149
  manual_reorder = [(("quickcheck", 1), [0,2,1,4,3,5])]}) *}
bulwahn@38962
   150
bulwahn@38962
   151
bulwahn@38962
   152
theorem S\<^isub>4_sound:
bulwahn@38962
   153
"w \<in> S\<^isub>4 \<longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
bulwahn@38962
   154
quickcheck[generator = prolog, size=1, iterations=1, expect = no_counterexample]
bulwahn@38962
   155
oops
bulwahn@38962
   156
bulwahn@38962
   157
(*
bulwahn@38962
   158
theorem S\<^isub>4_complete:
bulwahn@38962
   159
"length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b] \<longrightarrow> w \<in> S\<^isub>4"
bulwahn@38962
   160
oops
bulwahn@38962
   161
*)
bulwahn@38962
   162
bulwahn@38962
   163
hide_const a b
bulwahn@38962
   164
bulwahn@38962
   165
bulwahn@38962
   166
end