src/HOL/Extraction/Higman.thy
author haftmann
Thu, 25 Oct 2007 19:27:52 +0200
changeset 25194 37a1743f0fc3
parent 24348 c708ea5b109a
child 25417 ddb060d37ca8
permissions -rw-r--r--
dropped redundancy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     1
(*  Title:      HOL/Extraction/Higman.thy
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     2
    ID:         $Id$
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     4
                Monika Seisenberger, LMU Muenchen
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     5
*)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     6
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     7
header {* Higman's lemma *}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     8
24221
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
     9
theory Higman
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
    10
imports Main (*"~~/src/HOL/ex/Random"*)
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
    11
begin
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    12
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    13
text {*
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    14
  Formalization by Stefan Berghofer and Monika Seisenberger,
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    15
  based on Coquand and Fridlender \cite{Coquand93}.
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    16
*}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    17
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    18
datatype letter = A | B
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    19
23747
b07cff284683 Renamed inductive2 to inductive.
berghofe
parents: 23373
diff changeset
    20
inductive emb :: "letter list \<Rightarrow> letter list \<Rightarrow> bool"
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    21
where
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    22
   emb0 [Pure.intro]: "emb [] bs"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    23
 | emb1 [Pure.intro]: "emb as bs \<Longrightarrow> emb as (b # bs)"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    24
 | emb2 [Pure.intro]: "emb as bs \<Longrightarrow> emb (a # as) (a # bs)"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    25
23747
b07cff284683 Renamed inductive2 to inductive.
berghofe
parents: 23373
diff changeset
    26
inductive L :: "letter list \<Rightarrow> letter list list \<Rightarrow> bool"
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    27
  for v :: "letter list"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    28
where
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    29
   L0 [Pure.intro]: "emb w v \<Longrightarrow> L v (w # ws)"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    30
 | L1 [Pure.intro]: "L v ws \<Longrightarrow> L v (w # ws)"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    31
23747
b07cff284683 Renamed inductive2 to inductive.
berghofe
parents: 23373
diff changeset
    32
inductive good :: "letter list list \<Rightarrow> bool"
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    33
where
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    34
    good0 [Pure.intro]: "L w ws \<Longrightarrow> good (w # ws)"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    35
  | good1 [Pure.intro]: "good ws \<Longrightarrow> good (w # ws)"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    36
23747
b07cff284683 Renamed inductive2 to inductive.
berghofe
parents: 23373
diff changeset
    37
inductive R :: "letter \<Rightarrow> letter list list \<Rightarrow> letter list list \<Rightarrow> bool"
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    38
  for a :: letter
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    39
where
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    40
    R0 [Pure.intro]: "R a [] []"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    41
  | R1 [Pure.intro]: "R a vs ws \<Longrightarrow> R a (w # vs) ((a # w) # ws)"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    42
23747
b07cff284683 Renamed inductive2 to inductive.
berghofe
parents: 23373
diff changeset
    43
inductive T :: "letter \<Rightarrow> letter list list \<Rightarrow> letter list list \<Rightarrow> bool"
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    44
  for a :: letter
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    45
where
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    46
    T0 [Pure.intro]: "a \<noteq> b \<Longrightarrow> R b ws zs \<Longrightarrow> T a (w # zs) ((a # w) # zs)"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    47
  | T1 [Pure.intro]: "T a ws zs \<Longrightarrow> T a (w # ws) ((a # w) # zs)"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    48
  | T2 [Pure.intro]: "a \<noteq> b \<Longrightarrow> T a ws zs \<Longrightarrow> T a ws ((b # w) # zs)"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    49
23747
b07cff284683 Renamed inductive2 to inductive.
berghofe
parents: 23373
diff changeset
    50
inductive bar :: "letter list list \<Rightarrow> bool"
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    51
where
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    52
    bar1 [Pure.intro]: "good ws \<Longrightarrow> bar ws"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    53
  | bar2 [Pure.intro]: "(\<And>w. bar (w # ws)) \<Longrightarrow> bar ws"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    54
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    55
theorem prop1: "bar ([] # ws)" by iprover
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    56
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    57
theorem lemma1: "L as ws \<Longrightarrow> L (a # as) ws"
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
    58
  by (erule L.induct, iprover+)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    59
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    60
lemma lemma2': "R a vs ws \<Longrightarrow> L as vs \<Longrightarrow> L (a # as) ws"
13969
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
    61
  apply (induct set: R)
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    62
  apply (erule L.cases)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    63
  apply simp+
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    64
  apply (erule L.cases)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    65
  apply simp_all
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    66
  apply (rule L0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    67
  apply (erule emb2)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    68
  apply (erule L1)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    69
  done
13969
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
    70
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    71
lemma lemma2: "R a vs ws \<Longrightarrow> good vs \<Longrightarrow> good ws"
13969
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
    72
  apply (induct set: R)
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
    73
  apply iprover
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    74
  apply (erule good.cases)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    75
  apply simp_all
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    76
  apply (rule good0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    77
  apply (erule lemma2')
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    78
  apply assumption
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    79
  apply (erule good1)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    80
  done
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    81
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    82
lemma lemma3': "T a vs ws \<Longrightarrow> L as vs \<Longrightarrow> L (a # as) ws"
13969
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
    83
  apply (induct set: T)
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    84
  apply (erule L.cases)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    85
  apply simp_all
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    86
  apply (rule L0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    87
  apply (erule emb2)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    88
  apply (rule L1)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    89
  apply (erule lemma1)
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    90
  apply (erule L.cases)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    91
  apply simp_all
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
    92
  apply iprover+
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    93
  done
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    94
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    95
lemma lemma3: "T a ws zs \<Longrightarrow> good ws \<Longrightarrow> good zs"
13969
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
    96
  apply (induct set: T)
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
    97
  apply (erule good.cases)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    98
  apply simp_all
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    99
  apply (rule good0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   100
  apply (erule lemma1)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   101
  apply (erule good1)
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   102
  apply (erule good.cases)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   103
  apply simp_all
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   104
  apply (rule good0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   105
  apply (erule lemma3')
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
   106
  apply iprover+
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   107
  done
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   108
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   109
lemma lemma4: "R a ws zs \<Longrightarrow> ws \<noteq> [] \<Longrightarrow> T a ws zs"
13969
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
   110
  apply (induct set: R)
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
   111
  apply iprover
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   112
  apply (case_tac vs)
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   113
  apply (erule R.cases)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   114
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   115
  apply (case_tac a)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   116
  apply (rule_tac b=B in T0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   117
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   118
  apply (rule R0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   119
  apply (rule_tac b=A in T0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   120
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   121
  apply (rule R0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   122
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   123
  apply (rule T1)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   124
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   125
  done
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   126
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   127
lemma letter_neq: "(a::letter) \<noteq> b \<Longrightarrow> c \<noteq> a \<Longrightarrow> c = b"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   128
  apply (case_tac a)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   129
  apply (case_tac b)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   130
  apply (case_tac c, simp, simp)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   131
  apply (case_tac c, simp, simp)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   132
  apply (case_tac b)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   133
  apply (case_tac c, simp, simp)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   134
  apply (case_tac c, simp, simp)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   135
  done
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   136
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   137
lemma letter_eq_dec: "(a::letter) = b \<or> a \<noteq> b"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   138
  apply (case_tac a)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   139
  apply (case_tac b)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   140
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   141
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   142
  apply (case_tac b)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   143
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   144
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   145
  done
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   146
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   147
theorem prop2:
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   148
  assumes ab: "a \<noteq> b" and bar: "bar xs"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   149
  shows "\<And>ys zs. bar ys \<Longrightarrow> T a xs zs \<Longrightarrow> T b ys zs \<Longrightarrow> bar zs" using bar
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   150
proof induct
23373
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 22921
diff changeset
   151
  fix xs zs assume "T a xs zs" and "good xs"
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 22921
diff changeset
   152
  hence "good zs" by (rule lemma3)
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 22921
diff changeset
   153
  then show "bar zs" by (rule bar1)
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   154
next
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   155
  fix xs ys
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   156
  assume I: "\<And>w ys zs. bar ys \<Longrightarrow> T a (w # xs) zs \<Longrightarrow> T b ys zs \<Longrightarrow> bar zs"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   157
  assume "bar ys"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   158
  thus "\<And>zs. T a xs zs \<Longrightarrow> T b ys zs \<Longrightarrow> bar zs"
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   159
  proof induct
23373
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 22921
diff changeset
   160
    fix ys zs assume "T b ys zs" and "good ys"
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 22921
diff changeset
   161
    then have "good zs" by (rule lemma3)
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 22921
diff changeset
   162
    then show "bar zs" by (rule bar1)
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   163
  next
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   164
    fix ys zs assume I': "\<And>w zs. T a xs zs \<Longrightarrow> T b (w # ys) zs \<Longrightarrow> bar zs"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   165
    and ys: "\<And>w. bar (w # ys)" and Ta: "T a xs zs" and Tb: "T b ys zs"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   166
    show "bar zs"
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   167
    proof (rule bar2)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   168
      fix w
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   169
      show "bar (w # zs)"
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   170
      proof (cases w)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   171
	case Nil
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   172
	thus ?thesis by simp (rule prop1)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   173
      next
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   174
	case (Cons c cs)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   175
	from letter_eq_dec show ?thesis
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   176
	proof
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   177
	  assume ca: "c = a"
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   178
	  from ab have "bar ((a # cs) # zs)" by (iprover intro: I ys Ta Tb)
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   179
	  thus ?thesis by (simp add: Cons ca)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   180
	next
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   181
	  assume "c \<noteq> a"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   182
	  with ab have cb: "c = b" by (rule letter_neq)
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   183
	  from ab have "bar ((b # cs) # zs)" by (iprover intro: I' Ta Tb)
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   184
	  thus ?thesis by (simp add: Cons cb)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   185
	qed
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   186
      qed
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   187
    qed
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   188
  qed
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   189
qed
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   190
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   191
theorem prop3:
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   192
  assumes bar: "bar xs"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   193
  shows "\<And>zs. xs \<noteq> [] \<Longrightarrow> R a xs zs \<Longrightarrow> bar zs" using bar
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   194
proof induct
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   195
  fix xs zs
23373
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 22921
diff changeset
   196
  assume "R a xs zs" and "good xs"
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 22921
diff changeset
   197
  then have "good zs" by (rule lemma2)
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 22921
diff changeset
   198
  then show "bar zs" by (rule bar1)
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   199
next
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   200
  fix xs zs
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   201
  assume I: "\<And>w zs. w # xs \<noteq> [] \<Longrightarrow> R a (w # xs) zs \<Longrightarrow> bar zs"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   202
  and xsb: "\<And>w. bar (w # xs)" and xsn: "xs \<noteq> []" and R: "R a xs zs"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   203
  show "bar zs"
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   204
  proof (rule bar2)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   205
    fix w
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   206
    show "bar (w # zs)"
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   207
    proof (induct w)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   208
      case Nil
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   209
      show ?case by (rule prop1)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   210
    next
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   211
      case (Cons c cs)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   212
      from letter_eq_dec show ?case
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   213
      proof
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   214
	assume "c = a"
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
   215
	thus ?thesis by (iprover intro: I [simplified] R)
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   216
      next
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   217
	from R xsn have T: "T a xs zs" by (rule lemma4)
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   218
	assume "c \<noteq> a"
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
   219
	thus ?thesis by (iprover intro: prop2 Cons xsb xsn R T)
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   220
      qed
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   221
    qed
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   222
  qed
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   223
qed
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   224
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   225
theorem higman: "bar []"
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   226
proof (rule bar2)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   227
  fix w
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   228
  show "bar [w]"
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   229
  proof (induct w)
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   230
    show "bar [[]]" by (rule prop1)
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   231
  next
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   232
    fix c cs assume "bar [cs]"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   233
    thus "bar [c # cs]" by (rule prop3) (simp, iprover)
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   234
  qed
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   235
qed
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   236
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   237
consts
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   238
  is_prefix :: "'a list \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> bool"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   239
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   240
primrec
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   241
  "is_prefix [] f = True"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   242
  "is_prefix (x # xs) f = (x = f (length xs) \<and> is_prefix xs f)"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   243
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   244
theorem L_idx:
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   245
  assumes L: "L w ws"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   246
  shows "is_prefix ws f \<Longrightarrow> \<exists>i. emb (f i) w \<and> i < length ws" using L
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   247
proof induct
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   248
  case (L0 v ws)
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   249
  hence "emb (f (length ws)) w" by simp
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   250
  moreover have "length ws < length (v # ws)" by simp
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   251
  ultimately show ?case by iprover
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   252
next
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   253
  case (L1 ws v)
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   254
  then obtain i where emb: "emb (f i) w" and "i < length ws"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   255
    by simp iprover
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   256
  hence "i < length (v # ws)" by simp
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   257
  with emb show ?case by iprover
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   258
qed
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   259
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   260
theorem good_idx:
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   261
  assumes good: "good ws"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   262
  shows "is_prefix ws f \<Longrightarrow> \<exists>i j. emb (f i) (f j) \<and> i < j" using good
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   263
proof induct
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   264
  case (good0 w ws)
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   265
  hence "w = f (length ws)" and "is_prefix ws f" by simp_all
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   266
  with good0 show ?case by (iprover dest: L_idx)
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   267
next
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   268
  case (good1 ws w)
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   269
  thus ?case by simp
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   270
qed
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   271
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   272
theorem bar_idx:
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   273
  assumes bar: "bar ws"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   274
  shows "is_prefix ws f \<Longrightarrow> \<exists>i j. emb (f i) (f j) \<and> i < j" using bar
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   275
proof induct
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   276
  case (bar1 ws)
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   277
  thus ?case by (rule good_idx)
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   278
next
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   279
  case (bar2 ws)
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   280
  hence "is_prefix (f (length ws) # ws) f" by simp
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   281
  thus ?case by (rule bar2)
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   282
qed
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   283
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   284
text {*
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   285
Strong version: yields indices of words that can be embedded into each other.
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   286
*}
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   287
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   288
theorem higman_idx: "\<exists>(i::nat) j. emb (f i) (f j) \<and> i < j"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   289
proof (rule bar_idx)
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   290
  show "bar []" by (rule higman)
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   291
  show "is_prefix [] f" by simp
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   292
qed
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   293
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   294
text {*
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   295
Weak version: only yield sequence containing words
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   296
that can be embedded into each other.
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   297
*}
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   298
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   299
theorem good_prefix_lemma:
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   300
  assumes bar: "bar ws"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   301
  shows "is_prefix ws f \<Longrightarrow> \<exists>vs. is_prefix vs f \<and> good vs" using bar
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   302
proof induct
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   303
  case bar1
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
   304
  thus ?case by iprover
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   305
next
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   306
  case (bar2 ws)
23373
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 22921
diff changeset
   307
  from bar2.prems have "is_prefix (f (length ws) # ws) f" by simp
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
   308
  thus ?case by (iprover intro: bar2)
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   309
qed
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   310
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   311
theorem good_prefix: "\<exists>vs. is_prefix vs f \<and> good vs"
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   312
  using higman
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   313
  by (rule good_prefix_lemma) simp+
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   314
13711
5ace1cccb612 Removed (now unneeded) declarations of realizers for bar induction.
berghofe
parents: 13470
diff changeset
   315
subsection {* Extracting the program *}
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   316
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   317
declare R.induct [ind_realizer]
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   318
declare T.induct [ind_realizer]
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   319
declare L.induct [ind_realizer]
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   320
declare good.induct [ind_realizer]
13711
5ace1cccb612 Removed (now unneeded) declarations of realizers for bar induction.
berghofe
parents: 13470
diff changeset
   321
declare bar.induct [ind_realizer]
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   322
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   323
extract higman_idx
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   324
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   325
text {*
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   326
  Program extracted from the proof of @{text higman_idx}:
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   327
  @{thm [display] higman_idx_def [no_vars]}
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   328
  Corresponding correctness theorem:
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   329
  @{thm [display] higman_idx_correctness [no_vars]}
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   330
  Program extracted from the proof of @{text higman}:
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   331
  @{thm [display] higman_def [no_vars]}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   332
  Program extracted from the proof of @{text prop1}:
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   333
  @{thm [display] prop1_def [no_vars]}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   334
  Program extracted from the proof of @{text prop2}:
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   335
  @{thm [display] prop2_def [no_vars]}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   336
  Program extracted from the proof of @{text prop3}:
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   337
  @{thm [display] prop3_def [no_vars]}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   338
*}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   339
24221
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   340
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   341
subsection {* Some examples *}
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   342
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   343
(* an attempt to express examples in HOL -- function
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   344
  mk_word :: "nat \<Rightarrow> randseed \<Rightarrow> letter list \<times> randseed"
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   345
where
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   346
  "mk_word k = (do
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   347
     i \<leftarrow> random 10;
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   348
     (if i > 7 \<and> k > 2 \<or> k > 1000 then return []
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   349
     else do
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   350
       let l = (if i mod 2 = 0 then A else B);
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   351
       ls \<leftarrow> mk_word (Suc k);
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   352
       return (l # ls)
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   353
     done)
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   354
   done)"
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   355
by pat_completeness auto
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   356
termination by (relation "measure ((op -) 1001)") auto
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   357
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   358
fun
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   359
  mk_word' :: "nat \<Rightarrow> randseed \<Rightarrow> letter list \<times> randseed"
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   360
where
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   361
  "mk_word' 0 = mk_word 0"
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   362
  | "mk_word' (Suc n) = (do _ \<leftarrow> mk_word 0; mk_word' n done)"*)
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   363
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   364
consts_code
22921
475ff421a6a3 consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents: 22845
diff changeset
   365
  "arbitrary :: LT"  ("({* L0 [] [] *})")
475ff421a6a3 consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents: 22845
diff changeset
   366
  "arbitrary :: TT"  ("({* T0 A [] [] [] R0 *})")
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   367
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   368
code_module Higman
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   369
contains
24221
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   370
  higman = higman_idx
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   371
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   372
ML {*
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   373
local open Higman in
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   374
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   375
val a = 16807.0;
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   376
val m = 2147483647.0;
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   377
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   378
fun nextRand seed =
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   379
  let val t = a*seed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   380
  in  t - m * real (Real.floor(t/m)) end;
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   381
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   382
fun mk_word seed l =
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   383
  let
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   384
    val r = nextRand seed;
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   385
    val i = Real.round (r / m * 10.0);
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   386
  in if i > 7 andalso l > 2 then (r, []) else
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   387
    apsnd (cons (if i mod 2 = 0 then A else B)) (mk_word r (l+1))
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   388
  end;
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   389
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   390
fun f s zero = mk_word s 0
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   391
  | f s (Suc n) = f (fst (mk_word s 0)) n;
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   392
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   393
val g1 = snd o (f 20000.0);
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   394
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   395
val g2 = snd o (f 50000.0);
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   396
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   397
fun f1 zero = [A,A]
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   398
  | f1 (Suc zero) = [B]
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   399
  | f1 (Suc (Suc zero)) = [A,B]
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   400
  | f1 _ = [];
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   401
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   402
fun f2 zero = [A,A]
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   403
  | f2 (Suc zero) = [B]
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   404
  | f2 (Suc (Suc zero)) = [B,A]
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   405
  | f2 _ = [];
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   406
24221
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   407
val (i1, j1) = higman g1;
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   408
val (v1, w1) = (g1 i1, g1 j1);
24221
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   409
val (i2, j2) = higman g2;
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   410
val (v2, w2) = (g2 i2, g2 j2);
24221
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   411
val (i3, j3) = higman f1;
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   412
val (v3, w3) = (f1 i3, f1 j3);
24221
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   413
val (i4, j4) = higman f2;
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   414
val (v4, w4) = (f2 i4, f2 j4);
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   415
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   416
end;
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   417
*}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   418
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   419
definition
24221
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   420
  arbitrary_LT :: LT where
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   421
  [symmetric, code inline]: "arbitrary_LT = arbitrary"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   422
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   423
definition
24221
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   424
  arbitrary_TT :: TT where
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   425
  [symmetric, code inline]: "arbitrary_TT = arbitrary"
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   426
24221
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   427
code_datatype L0 L1 arbitrary_LT
dd4a7ea0e64a code generator setup improved
haftmann
parents: 23810
diff changeset
   428
code_datatype T0 T1 T2 arbitrary_TT
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   429
24348
c708ea5b109a renamed code_gen to export_code
haftmann
parents: 24249
diff changeset
   430
export_code higman_idx in SML module_name Higman
20837
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   431
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   432
ML {*
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   433
local
23810
f5e6932d0500 dropped outer ROOT structure for generated code
haftmann
parents: 23747
diff changeset
   434
  open Higman
20837
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   435
in
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   436
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   437
val a = 16807.0;
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   438
val m = 2147483647.0;
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   439
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   440
fun nextRand seed =
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   441
  let val t = a*seed
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   442
  in  t - m * real (Real.floor(t/m)) end;
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   443
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   444
fun mk_word seed l =
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   445
  let
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   446
    val r = nextRand seed;
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   447
    val i = Real.round (r / m * 10.0);
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   448
  in if i > 7 andalso l > 2 then (r, []) else
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   449
    apsnd (cons (if i mod 2 = 0 then A else B)) (mk_word r (l+1))
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   450
  end;
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   451
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   452
fun f s Zero_nat = mk_word s 0
21196
42ee69856dd0 (adjustions)
haftmann
parents: 21152
diff changeset
   453
  | f s (Suc n) = f (fst (mk_word s 0)) n;
20837
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   454
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   455
val g1 = snd o (f 20000.0);
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   456
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   457
val g2 = snd o (f 50000.0);
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   458
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   459
fun f1 Zero_nat = [A,A]
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   460
  | f1 (Suc Zero_nat) = [B]
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   461
  | f1 (Suc (Suc Zero_nat)) = [A,B]
20837
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   462
  | f1 _ = [];
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   463
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   464
fun f2 Zero_nat = [A,A]
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   465
  | f2 (Suc Zero_nat) = [B]
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   466
  | f2 (Suc (Suc Zero_nat)) = [B,A]
20837
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   467
  | f2 _ = [];
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   468
22266
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   469
val (i1, j1) = higman_idx g1;
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   470
val (v1, w1) = (g1 i1, g1 j1);
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   471
val (i2, j2) = higman_idx g2;
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   472
val (v2, w2) = (g2 i2, g2 j2);
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   473
val (i3, j3) = higman_idx f1;
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   474
val (v3, w3) = (f1 i3, f1 j3);
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   475
val (i4, j4) = higman_idx f2;
9f3198585c89 Adapted to new inductive definition package.
berghofe
parents: 21545
diff changeset
   476
val (v4, w4) = (f2 i4, f2 j4);
20837
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   477
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   478
end;
21152
e97992896170 added code gen II
haftmann
parents: 21114
diff changeset
   479
*}
20837
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   480
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   481
end