Debian Codegraph @

doc/examples/sorting.mod

raw
1 -- FILE: /home/diacon/LANG/Cafe/prog/sorting.mod
2 -- CONTENTS: generic sorting algorithm for strings featuring
3 -- algorithm execution modulo axioms
4 -- reasoning about the algorithm in RWL
5 -- module parameterization mechanism
6 -- AUTHOR: Razvan Diaconescu
7 -- DIFFICULTY: ****
8
9 -- generic strings
10 mod! STRG (X :: TRIV) {
11 [ Elt < Strg ]
12
13 op nil : -> Elt
14 op __ : Strg Strg -> Strg {assoc idr: nil}
15 }
16
17 -- the theory of partially ordered sets
18 mod* POSET {
19 [ Elt ]
20
21 op _<=_ : Elt Elt -> Bool
22
23 vars E1 E2 E3 : Elt
24
25 eq E1 <= E1 = true .
26 cq [:non-exec]: E1 = E2 if (E1 <= E2) and (E2 <= E1) .
27 cq [:non-exec]: (E1 <= E3) = true if (E1 <= E2) and (E2 <= E3) .
28 }
29
30 -- generic sorting algorithm
31 mod! SORTING-STRG(Y :: POSET) {
32 protecting(STRG(Y))
33
34 ctrans N:Elt N':Elt => N' N if (N' <= N) and (N =/= N') .
35 }
36
37 ** set exec normalize on <- now this is an obsolete switch.
38
39 open SORTING-STRG(NAT) .
40 exec (4 3 5 3 1) .
41 -- fast execution by brute engine
42 -- set tram path brute .
43 -- tram exec (40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1) .
44 close
45
46 -- functions for proving the generic termination of the sorting algorithm
47 mod! SORTING-STRG-PROOF {
48 protecting(SORTING-STRG + NAT)
49
50 op disorder : Strg -> Nat
51 op _>>_ : Elt Strg -> Nat
52
53 vars E E' : Elt
54 var S : Strg
55
56 eq disorder(nil) = 0 .
57 eq disorder(E) = 0 .
58 eq disorder(E S) = disorder(S) + (E >> S) .
59
60 eq E >> nil = 0 .
61 cq E >> E' = 0 if E <= E' .
62 cq E >> E' = 1 if (E' <= E) and (E =/= E') .
63 cq E >> (E' S) = s(E >> S) if (E' <= E) and (E =/= E') .
64 cq E >> (E' S) = (E >> S) if E <= E' .
65 }
66
67 **> THEOREM: the sorting always terminates
68 **> s ==> s' implies disorder(s') < disorder(s)
69
70 **> LEMMA1: disorder(e' e s) < disorder(e e' s) if e' < e .
71 open SORTING-STRG-PROOF .
72 ops e e' : -> Elt .
73 op s : -> Strg .
74 vars N N' M M' : Nat
75 ** lemmas about ordering of natural numbers
76 eq N < s(N) = true .
77 -- cq (M + N) < (M + N') = true if N < N' .
78 cq (M + N) + M' < (M + M') + N' = true if (N < N') .
79 ** hypothesis
80 eq e' <= e = true .
81 ** conclusion
82 -- red disorder(e' e s) < disorder(e e' s) .
83 close
84
85 **> LEMMA2: disorder(e s) < disorder(e s')
86 **> if (e >> s) == (e >> s') and disorder(s) < disorder(s')
87 open SORTING-STRG-PROOF .
88 op e : -> Elt .
89 ops s s' : -> Strg .
90 vars M M' N : Nat
91 ** lemma about ordering of natural numbers
92 cq (M + N) < (M' + N) = true if (M < M') .
93 ** hypothesis
94 eq (e >> s) = (e >> s') .
95 eq disorder(s) < disorder(s') = true .
96 ** conclusion
97 red disorder(e s) < disorder(e s') .
98 close
99
100 -- proof of (generic) local confluence for the sorting algorithm
101 ** case 1 for local confluence
102 open SORTING-STRG .
103 ops e e' e1 e1' : -> Elt .
104 ops s s' s'' : -> Strg .
105 ** hypothesis
106 eq e' <= e = true .
107 eq e1' <= e1 = true .
108 ** proof of local confluence
109 red (s e' e s' e1 e1' s'') ==> (s e' e s' e1' e1 s'') .
110 red (s e e' s' e1' e1 s'') ==> (s e' e s' e1' e1 s'') .
111 close
112 ** case 2 for local confluence
113 open SORTING-STRG .
114 ops e e' e'' : -> Elt .
115 ops s s' : -> Strg .
116 ** hypothesis
117 eq e' <= e = true .
118 eq e'' <= e' = true .
119 ** lemma
120 eq e'' <= e = true .
121 ** proof of local confluence
122 red (s e' e e'' s') ==> (s e'' e' e s') .
123 red (s e e'' e' s') ==> (s e'' e' e s') .
124 close
125
126 -- given a fixed string, the following two modules embed
127 -- the partial order of Z into a total order I-POSET(Z)
128 -- this embedding is conservative wrt to the execution steps of the
129 -- sorting algorithm; this is used for proving the generic confluence
130 -- of the sorting algorithm (see CafeOBJ Jewels for more details)
131 mod! I-POSET (Y' :: POSET)
132 principal-sort 2Tuple {
133 protecting(2TUPLE(Y',NAT))
134
135 op _<=_ : 2Tuple 2Tuple -> Bool
136
137 vars E1 E2 : Elt
138 vars N1 N2 : Nat
139
140 ceq << E1 ; N1 >> <= << E2 ; N2 >> = true if E1 <= E2 .
141 ceq << E1 ; N1 >> <= << E2 ; N2 >> = true
142 if (E1 <= E2 =/= true) and (E2 <= E1 =/= true) and (N1 <= N2) .
143 }
144
145 mod! STRG-MAPPING (Z :: POSET) {
146 protecting(STRG(X <= I-POSET(Y' <= Z))* { sort Strg -> 2Strg } +
147 STRG(X <= Z))
148
149 var E : Elt
150 var N : Nat
151 var S : Strg
152 vars S1 S2 : 2Strg
153
154 op s : 2Tuple -> 2Tuple
155 op s : 2Strg -> 2Strg
156
157 op [_] : Elt -> 2Tuple
158 op [_] : Strg -> 2Strg
159
160 eq s(<< E ; N >>) = << E ; s(N) >> .
161 eq s(S1 S2) = s(S1) s(S2) .
162
163 eq [ E ] = << E ; 1 >> .
164 eq [ E S ] = << E ; 1 >> s([ S ]) .
165 }
166
167 open STRG-MAPPING .
168 ops e1 e2 e3 e4 e5 : -> Elt .
169 red [ e1 e2 e3 e4 e5 ] .
170 close
171 --
172 eof
173 --
174 $Id: sorting.mod,v 1.1.1.1 2003-06-19 08:30:11 sawada Exp $