Tests/Rec_def2.thy
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 07 Mar 2013 11:52:08 +0000
changeset 216 38ed0ed6de3d
child 219 a3ea0b0f8bad
permissions -rw-r--r--
added definition of termination for rec_exec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
216
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
theory Rec_def2
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
imports Main
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
begin
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
datatype recf =  z
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
              |  s
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
              |  id nat nat
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
              |  Cn nat recf "recf list"
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
              |  Pr nat recf recf
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
              |  Mn nat recf 
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
function rec_exec :: "recf \<Rightarrow> nat list \<Rightarrow> nat"
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
  where
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
  "rec_exec z xs = 0" |
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
  "rec_exec s xs = (Suc (xs ! 0))" |
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
  "rec_exec (id m n) xs = (xs ! n)" |
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
  "rec_exec (Cn n f gs) xs = 
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
             (let ys = (map (\<lambda> a. rec_exec a xs) gs) in 
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
                                  rec_exec f ys)" | 
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
  "rec_exec (Pr n f g) xs = 
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
     (if hd xs = 0 then 
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
                  rec_exec f (tl xs)
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
      else rec_exec g ((hd xs - 1) # tl xs @ [rec_exec (Pr n f g) ((hd xs - 1) # tl xs)]))" |
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
  "rec_exec (Mn n f) xs = (LEAST x. rec_exec f (x # xs) = 0)"
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
by pat_completeness auto
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
termination
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
apply(relation "measures [\<lambda> (r, xs). size r, (\<lambda> (r, xs). hd xs)]")
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
apply(auto simp add: less_Suc_eq_le intro: trans_le_add2 list_size_estimation')
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
done
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
inductive terminate :: "recf \<Rightarrow> nat list \<Rightarrow> bool"
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  where
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  termi_z: "terminate z [n]"
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
| termi_s: "terminate s [n]"
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
| termi_id: "\<lbrakk>n < m; length xs = m\<rbrakk> \<Longrightarrow> terminate (id m n) xs"
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
| termi_cn: "\<lbrakk>terminate f (map (\<lambda>g. rec_exec g xs) gs); 
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
              \<forall>g \<in> set gs. terminate g xs; length xs = n\<rbrakk> \<Longrightarrow> terminate (Cn n f gs) xs"
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
| termi_pr_0: "\<lbrakk>terminate f xs; length xs = n\<rbrakk> \<Longrightarrow> terminate (Pr n f g) (0 # xs)"
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
| termi_pr_suc: "\<lbrakk>terminate (Pr n f gs) (x # xs);
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
                  terminate g (x # rec_exec (Pr n f gs) (x # xs) # xs)\<rbrakk> 
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
                  \<Longrightarrow> terminate (Pr n f gs) (Suc x # xs)"
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
| termi_mn: "\<lbrakk>length xs = n; rec_exec f (r # xs) = 0;
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
              \<forall> i < r. terminate f (i # xs) \<and> rec_exec f (i # xs) > 0\<rbrakk> \<Longrightarrow> terminate (Mn n f) xs"
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
38ed0ed6de3d added definition of termination for rec_exec
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
end