Tests/Rec_Def.thy
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Tue, 05 Mar 2013 15:23:10 +0000
changeset 213 30d81499766b
parent 212 203d50aebb1c
child 215 12f278bd67aa
permissions -rwxr-xr-x
added a version with partial_function
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
212
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
header {* Definition of Recursive Functions *}
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
theory Rec_Def
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
imports Main "~~/src/HOL/Library/Monad_Syntax"
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
begin
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
datatype recf = 
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
  Zero 
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
| Succ 
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
| Id nat nat              --"Projection"
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
| Cn nat recf "recf list" --"Composition"
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
| Pr nat recf recf        --"Primitive recursion"
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
| Mn nat recf             --"Minimisation"
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
213
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    15
partial_function (option) 
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    16
  eval :: "recf \<Rightarrow> nat option \<Rightarrow> (nat list) option => nat list \<Rightarrow> nat option"
212
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
where
213
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    18
 "eval f i gs ns = (case (i, gs, f, ns) of
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    19
                  (None, None, Zero, [n]) \<Rightarrow> Some 0
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    20
                | (None, None, Succ, [n]) \<Rightarrow> Some (n + 1)
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    21
                | (None, None, Id i j, ns) \<Rightarrow> if (j < i) then Some (ns ! j) else None               
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    22
                | (None, None, Pr n f g, 0 # ns) \<Rightarrow> eval f None None ns
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    23
                | (None, None, Pr n f g, Suc k # ns) \<Rightarrow>
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    24
                    do { r \<leftarrow> eval (Pr n f g) None None (k # ns); eval g None None (r # k # ns) }            
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    25
                | (None, None, Mn n f, ns) \<Rightarrow> eval f (Some 0) None ns
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    26
                | (Some n, None, f, ns) \<Rightarrow> 
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    27
                    do { r \<leftarrow> eval f None None (n # ns); 
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    28
                         if r = 0 then Some n else eval f (Some (Suc n)) None ns }
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    29
                | (None, None, Cn n f [], ns) \<Rightarrow> eval f None None []
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    30
                | (None, None, Cn n f (g#gs), ns) \<Rightarrow> 
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    31
                    do { r \<leftarrow> eval g None None ns; eval (Cn n f gs) None (Some [r]) ns }
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    32
                | (None, Some rs, Cn n f [], ns) \<Rightarrow> eval f None None rs  
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    33
                | (None, Some rs, Cn n f (g#gs), ns) \<Rightarrow> 
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    34
                    do { r \<leftarrow> eval g None None ns; eval (Cn n f gs) None (Some (r#rs)) ns }  
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    35
                | (_, _) \<Rightarrow> None)"
212
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
213
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    37
abbreviation
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    38
  "eval0 f ns \<equiv> eval f None None ns"
212
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
213
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    40
lemma "eval0 Zero [n] = Some 0"
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    41
apply(subst eval.simps)
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    42
apply(simp)
212
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
done
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
213
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    45
lemma "eval0 Succ [n] = Some (n + 1)"
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    46
apply(subst eval.simps)
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    47
apply(simp)
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    48
done
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    49
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    50
lemma "eval0 (Id i j) ns = (if (j < i) then Some (ns ! j) else None)" 
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    51
apply(subst eval.simps)
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    52
apply(simp)
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    53
done
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    54
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    55
lemma "eval0 (Pr n f g) (0 # ns) = eval0 f ns"
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    56
apply(subst eval.simps)
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    57
apply(simp)
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    58
done
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    59
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    60
lemma "eval0 (Pr n f g) (Suc k # ns) = 
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    61
  do { r \<leftarrow> eval0 (Pr n f g) (k # ns); eval0 g (r # k # ns) }" 
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    62
apply(subst eval.simps)
30d81499766b added a version with partial_function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
    63
apply(simp)
212
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
done
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
203d50aebb1c partial_function test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
end