src/HOL/Extraction/Higman.thy
author haftmann
Mon, 02 Oct 2006 23:00:53 +0200
changeset 20837 099877d83d2b
parent 20637 d883e0fc1c51
child 21114 3c09ec7565ed
permissions -rw-r--r--
added example for code_gen
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
16417
9bc16273c2d4 migrated theory headers to new format
haftmann
parents: 15801
diff changeset
     9
theory Higman imports Main begin
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    10
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    11
text {*
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    12
  Formalization by Stefan Berghofer and Monika Seisenberger,
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    13
  based on Coquand and Fridlender \cite{Coquand93}.
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    14
*}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    15
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    16
datatype letter = A | B
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    17
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    18
consts
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    19
  emb :: "(letter list \<times> letter list) set"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    20
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    21
inductive emb
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    22
intros
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 13969
diff changeset
    23
  emb0 [Pure.intro]: "([], bs) \<in> emb"
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 13969
diff changeset
    24
  emb1 [Pure.intro]: "(as, bs) \<in> emb \<Longrightarrow> (as, b # bs) \<in> emb"
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 13969
diff changeset
    25
  emb2 [Pure.intro]: "(as, bs) \<in> emb \<Longrightarrow> (a # as, a # bs) \<in> emb"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    26
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    27
consts
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    28
  L :: "letter list \<Rightarrow> letter list list set"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    29
13969
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
    30
inductive "L v"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    31
intros
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 13969
diff changeset
    32
  L0 [Pure.intro]: "(w, v) \<in> emb \<Longrightarrow> w # ws \<in> L v"
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 13969
diff changeset
    33
  L1 [Pure.intro]: "ws \<in> L v \<Longrightarrow> w # ws \<in> L v"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    34
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    35
consts
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    36
  good :: "letter list list set"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    37
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    38
inductive good
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    39
intros
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 13969
diff changeset
    40
  good0 [Pure.intro]: "ws \<in> L w \<Longrightarrow> w # ws \<in> good"
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 13969
diff changeset
    41
  good1 [Pure.intro]: "ws \<in> good \<Longrightarrow> w # ws \<in> good"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    42
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    43
consts
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    44
  R :: "letter \<Rightarrow> (letter list list \<times> letter list list) set"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    45
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    46
inductive "R a"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    47
intros
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 13969
diff changeset
    48
  R0 [Pure.intro]: "([], []) \<in> R a"
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 13969
diff changeset
    49
  R1 [Pure.intro]: "(vs, ws) \<in> R a \<Longrightarrow> (w # vs, (a # w) # ws) \<in> R a"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    50
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    51
consts
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    52
  T :: "letter \<Rightarrow> (letter list list \<times> letter list list) set"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    53
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    54
inductive "T a"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    55
intros
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 13969
diff changeset
    56
  T0 [Pure.intro]: "a \<noteq> b \<Longrightarrow> (ws, zs) \<in> R b \<Longrightarrow> (w # zs, (a # w) # zs) \<in> T a"
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 13969
diff changeset
    57
  T1 [Pure.intro]: "(ws, zs) \<in> T a \<Longrightarrow> (w # ws, (a # w) # zs) \<in> T a"
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 13969
diff changeset
    58
  T2 [Pure.intro]: "a \<noteq> b \<Longrightarrow> (ws, zs) \<in> T a \<Longrightarrow> (ws, (b # w) # zs) \<in> T a"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    59
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    60
consts
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    61
  bar :: "letter list list set"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    62
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    63
inductive bar
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    64
intros
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 13969
diff changeset
    65
  bar1 [Pure.intro]: "ws \<in> good \<Longrightarrow> ws \<in> bar"
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 13969
diff changeset
    66
  bar2 [Pure.intro]: "(\<And>w. w # ws \<in> bar) \<Longrightarrow> ws \<in> bar"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    67
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
    68
theorem prop1: "([] # ws) \<in> bar" by iprover
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    69
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    70
theorem lemma1: "ws \<in> L as \<Longrightarrow> ws \<in> L (a # as)"
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
    71
  by (erule L.induct, iprover+)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    72
13969
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
    73
lemma lemma2': "(vs, ws) \<in> R a \<Longrightarrow> vs \<in> L as \<Longrightarrow> ws \<in> L (a # as)"
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
    74
  apply (induct set: R)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    75
  apply (erule L.elims)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    76
  apply simp+
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    77
  apply (erule L.elims)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    78
  apply simp_all
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    79
  apply (rule L0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    80
  apply (erule emb2)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    81
  apply (erule L1)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    82
  done
13969
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
    83
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
    84
lemma lemma2: "(vs, ws) \<in> R a \<Longrightarrow> vs \<in> good \<Longrightarrow> ws \<in> good"
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
    85
  apply (induct set: R)
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
    86
  apply iprover
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    87
  apply (erule good.elims)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    88
  apply simp_all
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    89
  apply (rule good0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    90
  apply (erule lemma2')
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    91
  apply assumption
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    92
  apply (erule good1)
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
13969
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
    95
lemma lemma3': "(vs, ws) \<in> T a \<Longrightarrow> vs \<in> L as \<Longrightarrow> ws \<in> L (a # as)"
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
    96
  apply (induct set: T)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    97
  apply (erule L.elims)
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 L0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   100
  apply (erule emb2)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   101
  apply (rule L1)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   102
  apply (erule lemma1)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   103
  apply (erule L.elims)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   104
  apply simp_all
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
   105
  apply iprover+
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   106
  done
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   107
13969
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
   108
lemma lemma3: "(ws, zs) \<in> T a \<Longrightarrow> ws \<in> good \<Longrightarrow> zs \<in> good"
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
   109
  apply (induct set: T)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   110
  apply (erule good.elims)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   111
  apply simp_all
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   112
  apply (rule good0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   113
  apply (erule lemma1)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   114
  apply (erule good1)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   115
  apply (erule good.elims)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   116
  apply simp_all
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   117
  apply (rule good0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   118
  apply (erule lemma3')
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
   119
  apply iprover+
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   120
  done
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   121
13969
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
   122
lemma lemma4: "(ws, zs) \<in> R a \<Longrightarrow> ws \<noteq> [] \<Longrightarrow> (ws, zs) \<in> T a"
3aa8c0bb3080 Some tuning:
berghofe
parents: 13930
diff changeset
   123
  apply (induct set: R)
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
   124
  apply iprover
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   125
  apply (case_tac vs)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   126
  apply (erule R.elims)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   127
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   128
  apply (case_tac a)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   129
  apply (rule_tac b=B in T0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   130
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   131
  apply (rule R0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   132
  apply (rule_tac b=A in T0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   133
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   134
  apply (rule R0)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   135
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   136
  apply (rule T1)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   137
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   138
  done
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   139
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   140
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
   141
  apply (case_tac a)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   142
  apply (case_tac b)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   143
  apply (case_tac c, simp, simp)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   144
  apply (case_tac c, simp, simp)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   145
  apply (case_tac b)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   146
  apply (case_tac c, simp, simp)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   147
  apply (case_tac c, simp, simp)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   148
  done
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   149
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   150
lemma letter_eq_dec: "(a::letter) = b \<or> a \<noteq> b"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   151
  apply (case_tac a)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   152
  apply (case_tac b)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   153
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   154
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   155
  apply (case_tac b)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   156
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   157
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   158
  done
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   159
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   160
theorem prop2:
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   161
  assumes ab: "a \<noteq> b" and bar: "xs \<in> bar"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   162
  shows "\<And>ys zs. ys \<in> bar \<Longrightarrow> (xs, zs) \<in> T a \<Longrightarrow> (ys, zs) \<in> T b \<Longrightarrow> zs \<in> bar" using bar
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   163
proof induct
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   164
  fix xs zs assume "xs \<in> good" and "(xs, zs) \<in> T a"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   165
  show "zs \<in> bar" by (rule bar1) (rule lemma3)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   166
next
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   167
  fix xs ys
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   168
  assume I: "\<And>w ys zs. ys \<in> bar \<Longrightarrow> (w # xs, zs) \<in> T a \<Longrightarrow> (ys, zs) \<in> T b \<Longrightarrow> zs \<in> bar"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   169
  assume "ys \<in> bar"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   170
  thus "\<And>zs. (xs, zs) \<in> T a \<Longrightarrow> (ys, zs) \<in> T b \<Longrightarrow> zs \<in> bar"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   171
  proof induct
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   172
    fix ys zs assume "ys \<in> good" and "(ys, zs) \<in> T b"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   173
    show "zs \<in> bar" by (rule bar1) (rule lemma3)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   174
  next
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   175
    fix ys zs assume I': "\<And>w zs. (xs, zs) \<in> T a \<Longrightarrow> (w # ys, zs) \<in> T b \<Longrightarrow> zs \<in> bar"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   176
    and ys: "\<And>w. w # ys \<in> bar" and Ta: "(xs, zs) \<in> T a" and Tb: "(ys, zs) \<in> T b"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   177
    show "zs \<in> bar"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   178
    proof (rule bar2)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   179
      fix w
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   180
      show "w # zs \<in> bar"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   181
      proof (cases w)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   182
	case Nil
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   183
	thus ?thesis by simp (rule prop1)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   184
      next
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   185
	case (Cons c cs)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   186
	from letter_eq_dec show ?thesis
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   187
	proof
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   188
	  assume ca: "c = a"
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
   189
	  from ab have "(a # cs) # zs \<in> bar" by (iprover intro: I ys Ta Tb)
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   190
	  thus ?thesis by (simp add: Cons ca)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   191
	next
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   192
	  assume "c \<noteq> a"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   193
	  with ab have cb: "c = b" by (rule letter_neq)
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
   194
	  from ab have "(b # cs) # zs \<in> bar" by (iprover intro: I' Ta Tb)
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   195
	  thus ?thesis by (simp add: Cons cb)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   196
	qed
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   197
      qed
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   198
    qed
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   199
  qed
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   200
qed
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   201
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   202
theorem prop3:
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   203
  assumes bar: "xs \<in> bar"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   204
  shows "\<And>zs. xs \<noteq> [] \<Longrightarrow> (xs, zs) \<in> R a \<Longrightarrow> zs \<in> bar" using bar
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   205
proof induct
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   206
  fix xs zs
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   207
  assume "xs \<in> good" and "(xs, zs) \<in> R a"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   208
  show "zs \<in> bar" by (rule bar1) (rule lemma2)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   209
next
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   210
  fix xs zs
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   211
  assume I: "\<And>w zs. w # xs \<noteq> [] \<Longrightarrow> (w # xs, zs) \<in> R a \<Longrightarrow> zs \<in> bar"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   212
  and xsb: "\<And>w. w # xs \<in> bar" and xsn: "xs \<noteq> []" and R: "(xs, zs) \<in> R a"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   213
  show "zs \<in> bar"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   214
  proof (rule bar2)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   215
    fix w
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   216
    show "w # zs \<in> bar"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   217
    proof (induct w)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   218
      case Nil
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   219
      show ?case by (rule prop1)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   220
    next
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   221
      case (Cons c cs)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   222
      from letter_eq_dec show ?case
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   223
      proof
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   224
	assume "c = a"
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
   225
	thus ?thesis by (iprover intro: I [simplified] R)
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   226
      next
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   227
	from R xsn have T: "(xs, zs) \<in> T a" by (rule lemma4)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   228
	assume "c \<noteq> a"
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
   229
	thus ?thesis by (iprover intro: prop2 Cons xsb xsn R T)
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   230
      qed
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   231
    qed
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   232
  qed
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   233
qed
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   234
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   235
theorem higman: "[] \<in> bar"
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   236
proof (rule bar2)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   237
  fix w
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   238
  show "[w] \<in> bar"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   239
  proof (induct w)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   240
    show "[[]] \<in> bar" by (rule prop1)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   241
  next
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   242
    fix c cs assume "[cs] \<in> bar"
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
   243
    thus "[c # cs] \<in> bar" by (rule prop3) (simp, iprover)
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   244
  qed
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   245
qed
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   246
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   247
consts
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   248
  is_prefix :: "'a list \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> bool"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   249
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   250
primrec
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   251
  "is_prefix [] f = True"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   252
  "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
   253
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   254
theorem good_prefix_lemma:
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   255
  assumes bar: "ws \<in> bar"
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   256
  shows "is_prefix ws f \<Longrightarrow> \<exists>vs. is_prefix vs f \<and> vs \<in> good" using bar
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   257
proof induct
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   258
  case bar1
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
   259
  thus ?case by iprover
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   260
next
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   261
  case (bar2 ws)
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   262
  have "is_prefix (f (length ws) # ws) f" by simp
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 17145
diff changeset
   263
  thus ?case by (iprover intro: bar2)
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   264
qed
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   265
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   266
theorem good_prefix: "\<exists>vs. is_prefix vs f \<and> vs \<in> good"
13930
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   267
  using higman
562fd03b266d Converted main proofs to Isar.
berghofe
parents: 13711
diff changeset
   268
  by (rule good_prefix_lemma) simp+
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   269
13711
5ace1cccb612 Removed (now unneeded) declarations of realizers for bar induction.
berghofe
parents: 13470
diff changeset
   270
subsection {* Extracting the program *}
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   271
13711
5ace1cccb612 Removed (now unneeded) declarations of realizers for bar induction.
berghofe
parents: 13470
diff changeset
   272
declare bar.induct [ind_realizer]
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   273
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   274
extract good_prefix
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   275
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   276
text {*
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   277
  Program extracted from the proof of @{text good_prefix}:
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   278
  @{thm [display] good_prefix_def [no_vars]}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   279
  Corresponding correctness theorem:
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   280
  @{thm [display] good_prefix_correctness [no_vars]}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   281
  Program extracted from the proof of @{text good_prefix_lemma}:
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   282
  @{thm [display] good_prefix_lemma_def [no_vars]}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   283
  Program extracted from the proof of @{text higman}:
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   284
  @{thm [display] higman_def [no_vars]}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   285
  Program extracted from the proof of @{text prop1}:
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   286
  @{thm [display] prop1_def [no_vars]}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   287
  Program extracted from the proof of @{text prop2}:
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   288
  @{thm [display] prop2_def [no_vars]}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   289
  Program extracted from the proof of @{text prop3}:
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   290
  @{thm [display] prop3_def [no_vars]}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   291
*}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   292
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   293
code_module Higman
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   294
contains
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   295
  test = good_prefix
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   296
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   297
ML {*
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   298
local open Higman in
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   299
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   300
val a = 16807.0;
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   301
val m = 2147483647.0;
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   302
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   303
fun nextRand seed =
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   304
  let val t = a*seed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   305
  in  t - m * real (Real.floor(t/m)) end;
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   306
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   307
fun mk_word seed l =
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   308
  let
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   309
    val r = nextRand seed;
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   310
    val i = Real.round (r / m * 10.0);
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   311
  in if i > 7 andalso l > 2 then (r, []) else
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   312
    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
   313
  end;
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   314
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   315
fun f s id_0 = mk_word s 0
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   316
  | f s (Suc n) = f (fst (mk_word s 0)) n;
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   317
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   318
val g1 = snd o (f 20000.0);
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   319
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   320
val g2 = snd o (f 50000.0);
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   321
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   322
fun f1 id_0 = [A,A]
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   323
  | f1 (Suc id_0) = [B]
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   324
  | f1 (Suc (Suc id_0)) = [A,B]
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   325
  | f1 _ = [];
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   326
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   327
fun f2 id_0 = [A,A]
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   328
  | f2 (Suc id_0) = [B]
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   329
  | f2 (Suc (Suc id_0)) = [B,A]
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   330
  | f2 _ = [];
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   331
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   332
val xs1 = test g1;
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   333
val xs2 = test g2;
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   334
val xs3 = test f1;
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   335
val xs4 = test f2;
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   336
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   337
end;
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   338
*}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   339
20837
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   340
code_gen good_prefix (SML -)
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   341
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   342
ML {*
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   343
local
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   344
  open ROOT.Higman
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   345
  open ROOT.IntDef
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   346
in
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   347
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   348
val a = 16807.0;
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   349
val m = 2147483647.0;
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   350
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   351
fun nextRand seed =
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   352
  let val t = a*seed
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   353
  in  t - m * real (Real.floor(t/m)) end;
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   354
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   355
fun mk_word seed l =
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   356
  let
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   357
    val r = nextRand seed;
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   358
    val i = Real.round (r / m * 10.0);
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   359
  in if i > 7 andalso l > 2 then (r, []) else
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   360
    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
   361
  end;
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   362
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   363
fun f s id_0 = mk_word s 0
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   364
  | f s (Succ_nat n) = f (fst (mk_word s 0)) n;
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   365
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   366
val g1 = snd o (f 20000.0);
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   367
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   368
val g2 = snd o (f 50000.0);
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   369
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   370
fun f1 id_0 = [A,A]
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   371
  | f1 (Succ_nat id_0) = [B]
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   372
  | f1 (Succ_nat (Succ_nat id_0)) = [A,B]
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   373
  | f1 _ = [];
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   374
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   375
fun f2 id_0 = [A,A]
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   376
  | f2 (Succ_nat id_0) = [B]
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   377
  | f2 (Succ_nat (Succ_nat id_0)) = [B,A]
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   378
  | f2 _ = [];
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   379
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   380
val xs1 = good_prefix g1;
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   381
val xs2 = good_prefix g2;
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   382
val xs3 = good_prefix f1;
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   383
val xs4 = good_prefix f2;
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   384
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   385
end;
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   386
*}
099877d83d2b added example for code_gen
haftmann
parents: 20637
diff changeset
   387
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   388
end