| 1 |
-- FILE: /home/diacon/LANG/Cafe/prog/debt-red.mod |
| 2 |
-- CONTENTS: RWL specification of a debt reduction algorithm featuring |
| 3 |
-- execution of algorithms in RWL, |
| 4 |
-- execution modulo axioms, and |
| 5 |
-- formal verification of algorithm in RWL |
| 6 |
-- AUTHOR: Razvan Diaconescu with some proofs by Michihiro Matsumoto |
| 7 |
-- corrected by [email protected] |
| 8 |
-- DIFFICULTY: ** |
| 9 |
|
| 10 |
set tram path brute . |
| 11 |
|
| 12 |
in integer |
| 13 |
|
| 14 |
-- specification of a system of debts data type |
| 15 |
mod! DEBT (X :: DINT) { |
| 16 |
protecting(QID * { sort Qid -> Agent }) |
| 17 |
|
| 18 |
[ Debt < Debt* ] |
| 19 |
|
| 20 |
op ___ : Agent DInt Agent -> Debt |
| 21 |
op nil : -> Debt* |
| 22 |
op __ : Debt* Debt* -> Debt* { assoc comm } |
| 23 |
|
| 24 |
vars a b c : Agent |
| 25 |
var n : DInt |
| 26 |
|
| 27 |
-- the whole debt in the system |
| 28 |
op global-debt : Debt* -> DInt |
| 29 |
|
| 30 |
eq global-debt(nil) = 0 . |
| 31 |
ceq global-debt(a n b) = n if 0 <= n . |
| 32 |
ceq global-debt(a n b) = - n if n <= 0 . |
| 33 |
eq global-debt(d1:Debt* d2:Debt*) = global-debt(d1) + global-debt(d2) . |
| 34 |
|
| 35 |
-- individual balance for each agent |
| 36 |
op balance : Agent Debt* -> DInt |
| 37 |
|
| 38 |
eq balance(a, nil) = 0 . |
| 39 |
ceq balance(a, (b n c)) = 0 if ((b =/= a) and (c =/= a)) or |
| 40 |
( (b == a) and (c == a) ) . |
| 41 |
ceq balance(a, (a n b)) = - n if b =/= a . |
| 42 |
ceq balance(a, (b n a)) = n if b =/= a . |
| 43 |
eq balance(a, d1:Debt* d2:Debt*) = balance(a, d1) + balance(a, d2) . |
| 44 |
} |
| 45 |
|
| 46 |
-- specification of the reduction algorithm |
| 47 |
mod! DEBT-REDUCE { |
| 48 |
protecting(DEBT) |
| 49 |
|
| 50 |
var S : Debt* |
| 51 |
vars m n : DInt |
| 52 |
vars !A !B !C : Agent |
| 53 |
|
| 54 |
eq nil S = S . |
| 55 |
|
| 56 |
ceq !A m !B = !B - m !A if (m < 0) . |
| 57 |
eq !A 0 !B = nil . |
| 58 |
eq (!A m !B) (!A n !B) = !A (m + n) !B . |
| 59 |
eq !A m !A = nil . |
| 60 |
|
| 61 |
ctrans (!A m !B) (!B n !C) => (!A m !C) (!B (n - m) !C) |
| 62 |
if (m <= n) and (0 < m) and (0 < n) . |
| 63 |
ctrans (!A m !B) (!B n !C) => (!A (m - n) !B) (!A n !C) |
| 64 |
if (n <= m) and (0 < m) and (0 < n) . |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
-- ---------------------------------------------- |
| 69 |
-- some testing |
| 70 |
-- ---------------------------------------------- |
| 71 |
view dint-int from DINT to INT { |
| 72 |
sort DInt -> Int, |
| 73 |
sort DNat -> Nat, |
| 74 |
op s X:DNat -> X:Nat + 1, |
| 75 |
op p Y:DInt -> Y:Int - 1, |
| 76 |
op 0 -> 0 |
| 77 |
} |
| 78 |
|
| 79 |
** need normalizations, setting `exec normalize on'. <- now this is obsolete switch |
| 80 |
** system does normalize always. |
| 81 |
-- set exec normalize on |
| 82 |
|
| 83 |
open DEBT-REDUCE(dint-int) . |
| 84 |
-- ------------------------------- |
| 85 |
let A = ('a -2 'b) ('a 3 'c) ('c 4 'b) . |
| 86 |
red global-debt(A) . |
| 87 |
red balance('b, A) . |
| 88 |
exec A . |
| 89 |
red A ==> ('a 1 'b) ('c 1 'b) . |
| 90 |
-- ------------------------------- |
| 91 |
let B = ('a 4 'b) ('b 2 'c) ('d -4 'c) ('d 5 'b) ('a -1 'e) |
| 92 |
('b 4 'e) ('b 3 'a) ('e 3 'd) . |
| 93 |
set always memo on |
| 94 |
-- tram exec B . |
| 95 |
exec B . |
| 96 |
--> this needs much time to perform ... please be patient |
| 97 |
clean memo |
| 98 |
-- eof |
| 99 |
red B ==> 'c 2 'd . |
| 100 |
** |
| 101 |
-- eof |
| 102 |
|
| 103 |
-- ------------------------------- |
| 104 |
--> the problem is NOT confluent: both of the following are normal forms |
| 105 |
red ('c 3 'a) ('d 1 'a) ('a 2 'b) ==> ('d 1 'a) ('c 2 'b) ('c 1 'a) . |
| 106 |
red ('c 3 'a) ('d 1 'a) ('a 2 'b) ==> ('c 2 'a) ('d 1 'b) ('c 1 'b) . |
| 107 |
close |
| 108 |
|
| 109 |
-- ------------------------------------------------------------ |
| 110 |
-- proof by induction in RWL |
| 111 |
-- global-debt(sys2) <= global-debt(sys1) if sys1 ==> sys2 |
| 112 |
-- ------------------------------------------------------------ |
| 113 |
|
| 114 |
-- BASE CASE |
| 115 |
-- no transition: |
| 116 |
open DEBT-REDUCE . |
| 117 |
ops sys1 sys2 : -> Debt* . |
| 118 |
eq sys2 = sys1 . |
| 119 |
red global-debt(sys2) <= global-debt(sys1) . |
| 120 |
close |
| 121 |
|
| 122 |
mod* BASE { |
| 123 |
pr(DEBT-REDUCE) |
| 124 |
|
| 125 |
ops a b c : -> Agent |
| 126 |
ops m n k : -> DNat |
| 127 |
|
| 128 |
eq 0 < m = true . |
| 129 |
eq 0 < n = true . |
| 130 |
eq 0 < k = true . |
| 131 |
} |
| 132 |
|
| 133 |
-- m < n |
| 134 |
open BASE . |
| 135 |
eq k + m = n . |
| 136 |
red global-debt((a m c) (b k c)) <= global-debt((a m b) (b n c)) . |
| 137 |
close |
| 138 |
-- m = n |
| 139 |
open BASE . |
| 140 |
eq n = m . |
| 141 |
red global-debt((a m c) (b 0 c)) <= global-debt((a m b) (b n c)) . |
| 142 |
close |
| 143 |
-- n < m |
| 144 |
open BASE . |
| 145 |
eq n + k = m . |
| 146 |
red global-debt((a k c) (b n c)) <= global-debt((a m b) (b n c)) . |
| 147 |
close |
| 148 |
|
| 149 |
-- INDUCTION STEP |
| 150 |
open DEBT-REDUCE . |
| 151 |
ops sys1 sys2 a b : -> Debt* . |
| 152 |
eq global-debt(sys1) <= global-debt(sys2) = true . |
| 153 |
eq global-debt(a) <= global-debt(b) = true . |
| 154 |
red global-debt(sys1 a) <= global-debt(sys2 b) . |
| 155 |
close |
| 156 |
-- |
| 157 |
eof |
| 158 |
-- |
| 159 |
$Id: debt-red.mod,v 1.2 2010-08-04 04:37:34 sawada Exp $ |