thys/Abacus.thy
changeset 181 4d54702229fd
parent 180 8f443f2ed1f6
child 190 f1ecb4a68a54
equal deleted inserted replaced
180:8f443f2ed1f6 181:4d54702229fd
    16      Inc nat
    16      Inc nat
    17    | Dec nat nat
    17    | Dec nat nat
    18    | Goto nat
    18    | Goto nat
    19   
    19   
    20 type_synonym abc_prog = "abc_inst list"
    20 type_synonym abc_prog = "abc_inst list"
    21 
       
    22 section {* Some Sample Abacus programs *}
       
    23 
       
    24 (* Abacus for addition and clearance *)
       
    25 fun plus_clear :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> abc_prog"  
       
    26   where
       
    27   "plus_clear m n e = [Dec m e, Inc n, Goto 0]"
       
    28 
       
    29 (* Abacus for clearing memory untis *)
       
    30 fun clear :: "nat \<Rightarrow> nat \<Rightarrow> abc_prog"
       
    31   where
       
    32   "clear n e = [Dec n e, Goto 0]"
       
    33 
       
    34 fun plus:: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> abc_prog"
       
    35   where
       
    36   "plus m n p e = [Dec m 4, Inc n, Inc p,
       
    37                    Goto 0, Dec p e, Inc m, Goto 4]"
       
    38 
       
    39 fun mult :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> abc_prog"
       
    40   where
       
    41   "mult m1 m2 n p e = [Dec m1 e]@ plus m2 n p 1"
       
    42 
       
    43 fun expo :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> abc_prog"
       
    44   where
       
    45   "expo n m1 m2 p e = [Inc n, Dec m1 e] @ mult m2 n n p 2"
       
    46 
    21 
    47 type_synonym abc_state = nat
    22 type_synonym abc_state = nat
    48 
    23 
    49 text {*
    24 text {*
    50   The memory of Abacus machine is defined as a list of contents, with 
    25   The memory of Abacus machine is defined as a list of contents, with