Tests/Test1.thy
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 22 Feb 2024 13:38:10 +0000
changeset 298 ac5461882f3e
parent 211 1d6a0fd9f7f4
permissions -rwxr-xr-x
test
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
206
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
theory Test1
211
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
     2
imports "../thys/Uncomputable"
206
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
begin
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
ML {*
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
fun timing_wrapper tac st =
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
let
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
  val t_start = Timing.start ();
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
  val res = tac st;
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
  val t_end = Timing.result t_start;
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
in
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
  (tracing (Timing.message t_end); res)
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
end
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
*}
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
datatype abc_inst =
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
     Inc nat
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
   | Dec nat nat
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
   | Goto nat
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
  
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
type_synonym abc_prog = "abc_inst list"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
datatype recf = 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
  z 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
| s 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
| id nat nat              --"Projection"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
| Cn nat recf "recf list" --"Composition"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
| Pr nat recf recf        --"Primitive recursion"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
| Mn nat recf             --"Minimisation"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
fun addition :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> abc_prog"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  where
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
  "addition m n p = [Dec m 4, Inc n, Inc p, Goto 0, Dec p 7, Inc m, Goto 4]"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
fun mv_box :: "nat \<Rightarrow> nat \<Rightarrow> abc_prog"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  where
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  "mv_box m n = [Dec m 3, Inc n, Goto 0]"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
fun abc_inst_shift :: "abc_inst \<Rightarrow> nat \<Rightarrow> abc_inst"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
  where
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  "abc_inst_shift (Inc m) n = Inc m" |
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
  "abc_inst_shift (Dec m e) n = Dec m (e + n)" |
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  "abc_inst_shift (Goto m) n = Goto (m + n)"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
fun abc_shift :: "abc_inst list \<Rightarrow> nat \<Rightarrow> abc_inst list" 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  where
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
  "abc_shift [] n = []"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
| "abc_shift (x#xs) n = (abc_inst_shift x n) # (abc_shift xs n)" 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
fun abc_append :: "abc_inst list \<Rightarrow> abc_inst list \<Rightarrow> abc_inst list" (infixl "[+]" 60)
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  where
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
  "abc_append al bl = al @ abc_shift bl (length al)"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
lemma [simp]: 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
  "length (pa [+] pb) = length pa + length pb"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
by (induct pb) (auto)
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
text {* The compilation of @{text "z"}-operator. *}
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
definition rec_ci_z :: "abc_inst list"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
  where
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
   [simp]: "rec_ci_z = [Goto 1]"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
text {* The compilation of @{text "s"}-operator. *}
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
definition rec_ci_s :: "abc_inst list"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
  where
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
  "rec_ci_s \<equiv> (addition 0 1 2 [+] [Inc 1])"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
lemma [simp]:
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
  "rec_ci_s = [Dec 0 4, Inc 1, Inc 2, Goto 0, Dec 2 7, Inc 0, Goto 4, Inc 1] "
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
by (simp add: rec_ci_s_def)
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
text {* The compilation of @{text "id i j"}-operator *}
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
fun rec_ci_id :: "nat \<Rightarrow> nat \<Rightarrow> abc_inst list"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
  where
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
  "rec_ci_id i j = addition j i (i + 1)"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
fun mv_boxes :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> abc_inst list"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
  where
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
  "mv_boxes ab bb n = (case n of 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
      0 => [] 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
    | Suc n => mv_boxes ab bb n [+] mv_box (ab + n) (bb + n))"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
fun empty_boxes :: "nat \<Rightarrow> abc_inst list"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
  where
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
  "empty_boxes n = (case n of
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
      0 => []
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
    | Suc n => empty_boxes n [+] [Dec n 2, Goto 0])"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
fun cn_merge_gs ::
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
  "(abc_inst list \<times> nat \<times> nat) list \<Rightarrow> nat \<Rightarrow> abc_inst list"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
  where
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
  "cn_merge_gs [] p = []" |
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
  "cn_merge_gs (g # gs) p = 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
      (let (gprog, gpara, gn) = g in 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
         gprog [+] mv_box gpara p [+] cn_merge_gs gs (Suc p))"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
fun list_max :: "nat list \<Rightarrow> nat"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
  where
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
  "list_max [] = 0"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
| "list_max (x#xs) = (let y = list_max xs in
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
                      if (y < x) then x else y)"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
fun rec_ci :: "recf \<Rightarrow> abc_inst list \<times> nat \<times> nat"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
  where
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
  "rec_ci z = (rec_ci_z, 1, 2)" |
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
  "rec_ci s = (rec_ci_s, 1, 3)" |
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
  "rec_ci (id m n) = (rec_ci_id m n, m, m + 2)" |
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
  "rec_ci (Cn n f gs) = 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
      (let cied_gs = map rec_ci gs in
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
       let (fprog, fpara, fn) = rec_ci f in 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
       let pstr = list_max (Suc n # fn # (map (\<lambda> (aprog, p, n). n) cied_gs)) in
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
       let qstr = pstr + Suc (length gs) in 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
       (cn_merge_gs cied_gs pstr [+] mv_boxes 0 qstr n [+] 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
          mv_boxes pstr 0 (length gs) [+] fprog [+] 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
            mv_box fpara pstr [+] empty_boxes (length gs) [+] 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
             mv_box pstr n [+] mv_boxes qstr 0 n, n,  qstr + n))" | 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
  "rec_ci (Pr n f g) = 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
         (let (fprog, fpara, fn) = rec_ci f in 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
          let (gprog, gpara, gn) = rec_ci g in 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
          let p = list_max [n + 3, fn, gn] in 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
          let e = length gprog + 7 in 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
           (mv_box n p [+] fprog [+] mv_box n (Suc n) [+] 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
               (([Dec p e] [+] gprog [+] 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
                 [Inc n, Dec (Suc n) 3, Goto 1]) @
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
                     [Dec (Suc (Suc n)) 0, Inc (Suc n), Goto (length gprog + 4)]),
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
             Suc n, p + 1))" |
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
  "rec_ci (Mn n f) =
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
         (let (fprog, fpara, fn) = rec_ci f in 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
          let len = length (fprog) in 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
            (fprog @ [Dec (Suc n) (len + 5), Dec (Suc n) (len + 3),
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
             Goto (len + 1), Inc n, Goto 0], n, max (Suc n) fn))"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
definition rec_add :: "recf"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
  where
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
   [simp]: "rec_add \<equiv>  Pr 1 (id 1 0) (Cn 3 s [id 3 2])"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
fun constn :: "nat \<Rightarrow> recf" where
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
  "constn n = (case n of
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
      0 => z  |
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
      Suc n => Cn 1 s [constn n])"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
ML {*
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
fun dest_suc_trm @{term "Suc 0"} = raise TERM ("Suc 0", [])
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
  | dest_suc_trm @{term "Suc (Suc 0)"} = 2
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
  | dest_suc_trm (@{term "Suc"} $ t) = 1 + dest_suc_trm t
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
  | dest_suc_trm t = snd (HOLogic.dest_number t)
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
fun get_thm ctxt (t, n) =
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
let
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
  val num = HOLogic.mk_number @{typ "nat"} n
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
  val goal = Logic.mk_equals (t, num)
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
  val num_ss = HOL_ss addsimps @{thms semiring_norm}
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
in
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
  Goal.prove ctxt [] [] goal (K (simp_tac num_ss 1))
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
end
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
fun nat_number_simproc ss ctrm =
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
let
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
  val trm = term_of ctrm
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
  val ctxt = Simplifier.the_context ss
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
in
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
  SOME (get_thm ctxt (trm, dest_suc_trm trm))
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
  handle TERM _ => NONE
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
end
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
*}
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
simproc_setup nat_number ("Suc n") = {* K nat_number_simproc*}
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
declare Nat.One_nat_def[simp del]
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
lemma [simp]: 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
  shows "nat_case g f (1::nat) = f (0::nat)"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
by (metis One_nat_def nat_case_Suc)
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
lemma 
211
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   181
  "rec_ci (constn 0) = ([Goto 1], 1, 2)"
206
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
apply(tactic {*
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
  timing_wrapper (asm_full_simp_tac @{simpset} 1)
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
*})
211
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   185
done
206
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
lemma 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
  "rec_ci (constn 10) = XXX"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
apply(tactic {*
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
  timing_wrapper (asm_full_simp_tac @{simpset} 1)
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
*})
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
oops
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   194
lemma 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   195
  "rec_ci (constn 3) = XXX"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   196
apply(tactic {*
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
  timing_wrapper (asm_full_simp_tac @{simpset} 1)
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   198
*})
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
oops
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
lemma 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   202
  "rec_ci (rec_add) = XXX"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   203
apply(simp del: abc_append.simps)
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   204
apply(simp)
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   205
oops
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   206
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   207
definition rec_mult :: "recf"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   208
  where
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   209
  [simp]: "rec_mult = Pr 1 z (Cn 3 rec_add [id 3 0, id 3 2])"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   210
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   211
lemma 
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
  "rec_ci (rec_mult) = XXX"
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
apply(simp)
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   214
oops
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   215
211
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   216
fun mopup_a :: "nat \<Rightarrow> instr list"
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   217
  where
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   218
  "mopup_a 0 = []" |
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   219
  "mopup_a (Suc n) = mopup_a n @ 
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   220
       [(R, 2*n + 3), (W0, 2*n + 2), (R, 2*n + 1), (W1, 2*n + 2)]"
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   221
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   222
definition mopup_b :: "instr list"
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   223
  where
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   224
  "mopup_b \<equiv> [(R, 2), (R, 1), (L, 5), (W0, 3), (R, 4), (W0, 3),
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   225
            (R, 2), (W0, 3), (L, 5), (L, 6), (R, 0), (L, 6)]"
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   226
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   227
fun mopup :: "nat \<Rightarrow> instr list"
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   228
  where 
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   229
  "mopup n = mopup_a n @ shift mopup_b (2*n)"
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   230
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   231
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   232
lemma
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   233
  "tcopy |+| mopup 0 = XXX"
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   234
apply(simp add: tm_comp.simps tcopy_def tcopy_begin_def tcopy_loop_def tcopy_end_def mopup_b_def)
1d6a0fd9f7f4 added factorial as an example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
   235
apply(simp add: adjust.simps shift.simps)
206
17d80924af53 added a test to make the simplifier be fast enough to do actual compilations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   236
end