adamc@915
|
1 datatype dlist'' t =
|
adamc@915
|
2 Nil
|
adamc@915
|
3 | Cons of t * source (dlist'' t)
|
adamc@915
|
4
|
adamc@915
|
5 datatype dlist' t =
|
adamc@915
|
6 Empty
|
adamc@915
|
7 | Nonempty of { Head : dlist'' t, Tail : source (source (dlist'' t)) }
|
adamc@915
|
8
|
adamc@951
|
9 con dlist t = source (dlist' t)
|
adamc@915
|
10
|
adamc@915
|
11 type position = transaction unit
|
adamc@915
|
12
|
adamc@915
|
13 fun headPos [t] (dl : dlist t) =
|
adamc@951
|
14 dl' <- get dl;
|
adamc@915
|
15 case dl' of
|
adamc@915
|
16 Nonempty { Head = Cons (_, tl), Tail = tl' } =>
|
adamc@915
|
17 cur <- get tl;
|
adamc@951
|
18 set dl (case cur of
|
adamc@951
|
19 Nil => Empty
|
adamc@951
|
20 | _ => Nonempty {Head = cur, Tail = tl'})
|
adamc@915
|
21 | _ => return ()
|
adamc@915
|
22
|
adamc@915
|
23 fun tailPos [t] (cur : source (dlist'' t)) new tail =
|
adamc@915
|
24 new' <- get new;
|
adamc@915
|
25 set cur new';
|
adamc@915
|
26
|
adamc@915
|
27 case new' of
|
adamc@915
|
28 Nil => set tail cur
|
adamc@915
|
29 | _ => return ()
|
adamc@915
|
30
|
adamc@951
|
31 val create [t] = source Empty
|
adamc@915
|
32
|
adamc@951
|
33 fun clear [t] (s : dlist t) = set s Empty
|
adamc@915
|
34
|
adamc@915
|
35 fun append [t] dl v =
|
adamc@951
|
36 dl' <- get dl;
|
adamc@915
|
37 case dl' of
|
adamc@915
|
38 Empty =>
|
adamc@915
|
39 tl <- source Nil;
|
adamc@915
|
40 tl' <- source tl;
|
adamc@951
|
41 set dl (Nonempty {Head = Cons (v, tl), Tail = tl'});
|
adamc@915
|
42 return (headPos dl)
|
adamc@915
|
43
|
adamc@915
|
44 | Nonempty {Tail = tl, ...} =>
|
adamc@915
|
45 cur <- get tl;
|
adamc@915
|
46 new <- source Nil;
|
adamc@915
|
47 set cur (Cons (v, new));
|
adamc@915
|
48 set tl new;
|
adamc@915
|
49 return (tailPos cur new tl)
|
adamc@915
|
50
|
adamc@952
|
51 fun renderDyn [ctx] [ctx ~ body] [t] (f : t -> position -> xml (ctx ++ body) [] []) filter dl = <xml>
|
adamc@951
|
52 <dyn signal={dl' <- signal dl;
|
adamc@915
|
53 return (case dl' of
|
adamc@915
|
54 Empty => <xml/>
|
adamc@915
|
55 | Nonempty {Head = hd, Tail = tlTop} =>
|
adamc@915
|
56 let
|
adamc@915
|
57 fun render' prev dl'' =
|
adamc@915
|
58 case dl'' of
|
adamc@915
|
59 Nil => <xml/>
|
adamc@915
|
60 | Cons (v, tl) =>
|
adamc@915
|
61 let
|
adamc@915
|
62 val pos = case prev of
|
adamc@915
|
63 None => headPos dl
|
adamc@915
|
64 | Some prev => tailPos prev tl tlTop
|
adamc@915
|
65 in
|
adamc@951
|
66 <xml><dyn signal={b <- filter v;
|
adamc@944
|
67 return (if b then
|
adamc@944
|
68 f v pos
|
adamc@944
|
69 else
|
adamc@944
|
70 <xml/>)}/>
|
adamc@944
|
71 <dyn signal={tl' <- signal tl;
|
adamc@944
|
72 return (render' (Some tl) tl')}/></xml>
|
adamc@915
|
73 end
|
adamc@915
|
74 in
|
adamc@915
|
75 render' None hd
|
adamc@915
|
76 end)}/>
|
adamc@915
|
77 </xml>
|
adamc@915
|
78
|
adamc@952
|
79 fun renderFlat [ctx] [ctx ~ body] [t] (f : t -> position -> xml (ctx ++ body) [] []) filter ls =
|
adamc@952
|
80 List.mapX (fn p => f p.1 p.2) ls
|
adamc@952
|
81
|
adamc@952
|
82 fun render [ctx] [ctx ~ body] [t] f r dl = <xml>
|
adamc@952
|
83 <dyn signal={sort <- r.Sort;
|
adamc@952
|
84 case sort of
|
adamc@952
|
85 None => return (renderDyn f r.Filter dl)
|
adamc@952
|
86 | Some sort =>
|
adamc@952
|
87 dl' <- signal dl;
|
adamc@952
|
88 elems <- (case dl' of
|
adamc@952
|
89 Empty => return []
|
adamc@952
|
90 | Nonempty {Head = hd, Tail = tlTop} =>
|
adamc@952
|
91 let
|
adamc@952
|
92 fun listOut prev dl'' acc =
|
adamc@952
|
93 case dl'' of
|
adamc@952
|
94 Nil => return acc
|
adamc@952
|
95 | Cons (v, tl) =>
|
adamc@952
|
96 let
|
adamc@952
|
97 val pos = case prev of
|
adamc@952
|
98 None => headPos dl
|
adamc@952
|
99 | Some prev => tailPos prev tl tlTop
|
adamc@952
|
100 in
|
adamc@952
|
101 tl' <- signal tl;
|
adamc@952
|
102 listOut (Some tl) tl' ((v, pos) :: acc)
|
adamc@952
|
103 end
|
adamc@952
|
104 in
|
adamc@952
|
105 listOut None hd []
|
adamc@952
|
106 end);
|
adamc@952
|
107 return (renderFlat f r.Filter elems)}/>
|
adamc@952
|
108 </xml>
|
adamc@952
|
109
|
adamc@952
|
110
|
adamc@952
|
111
|
adamc@915
|
112 fun delete pos = pos
|
adamc@915
|
113
|
adamc@915
|
114 fun elements' [t] (dl'' : dlist'' t) =
|
adamc@915
|
115 case dl'' of
|
adamc@915
|
116 Nil => return []
|
adamc@915
|
117 | Cons (x, dl'') =>
|
adamc@915
|
118 dl'' <- signal dl'';
|
adamc@915
|
119 tl <- elements' dl'';
|
adamc@915
|
120 return (x :: tl)
|
adamc@915
|
121
|
adamc@915
|
122 fun elements [t] (dl : dlist t) =
|
adamc@951
|
123 dl' <- signal dl;
|
adamc@915
|
124 case dl' of
|
adamc@915
|
125 Empty => return []
|
adamc@915
|
126 | Nonempty {Head = hd, ...} => elements' hd
|
adamc@937
|
127
|
adamc@937
|
128 fun foldl [t] [acc] (f : t -> acc -> signal acc) =
|
adamc@937
|
129 let
|
adamc@937
|
130 fun foldl'' (i : acc) (dl : dlist'' t) : signal acc =
|
adamc@937
|
131 case dl of
|
adamc@937
|
132 Nil => return i
|
adamc@937
|
133 | Cons (v, dl') =>
|
adamc@937
|
134 dl' <- signal dl';
|
adamc@937
|
135 i' <- f v i;
|
adamc@937
|
136 foldl'' i' dl'
|
adamc@937
|
137
|
adamc@937
|
138 fun foldl' (i : acc) (dl : dlist t) : signal acc =
|
adamc@951
|
139 dl <- signal dl;
|
adamc@937
|
140 case dl of
|
adamc@937
|
141 Empty => return i
|
adamc@937
|
142 | Nonempty {Head = dl, ...} => foldl'' i dl
|
adamc@937
|
143 in
|
adamc@937
|
144 foldl'
|
adamc@937
|
145 end
|