1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
-module(ataxic_optimize).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-include("ataxia/ataxic.hrl").
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-export([aggressive/1, light/1]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% list(..., const(a), ...) -> list(const(a), ...)
-spec remove_overridden_operations
(
list(ataxic:basic())
)
-> list(ataxic:basic()).
remove_overridden_operations (List) ->
lists:foldr
(
fun (Elem, CurrentResult) ->
case CurrentResult of
{done, _} -> CurrentResult;
{ok, List} ->
case Elem of
#const{} -> {done, [Elem|List]};
_ -> {ok, [Elem|List]}
end
end
end,
{ok, []},
List
).
% list(update_field(a, o0), update(a, o1), ...) -> update_field(a, list(o0, o1))
-spec optimize_update_field_sequence
(
list(ataxic:basic()),
list(ataxic:basic())
)
-> ataxic:basic().
optimize_update_field_sequence ([], Result) ->
case Result of
[] -> ataxic:current_value();
[A] -> A;
_ -> ataxic:sequence(Result)
end;
optimize_update_field_sequence (UnsortedOPs, CurrentResults) ->
% Get all field updates until you encounter something else
{FieldUpdates, PotentiallyImportantOPs} =
lists:splitwith(fun (E) -> is_record(E, upfield) end, UnsortedOPs),
% Sort field updates by updated field
SortedFieldUpdates =
lists:sort
(
fun (A, B) ->
((A#upfield.ix) =< (B#upfield.ix))
end,
FieldUpdates
),
% Merge all field updates that are for the same field
% LastIX, LastUpdateOPs correspond to the last field updates that should be
% merged but that were surprised by the sequence ending.
{LastIX, LastUpdateOPs, OtherMergedFieldUpdates} =
lists:foldl
(
fun (Update, {CurrentIX, CurrentOPs, CurrentResult}) ->
case (Update#upfield.ix == CurrentIX) of
true ->
{CurrentIX, [Update#upfield.op|CurrentOPs], CurrentResult};
_ ->
{
Update#upfield.ix,
[Update#upfield.op],
(
case CurrentOPs of
[] -> CurrentResult;
[OP] ->
[
ataxic:update_field(CurrentIX, OP)
|CurrentResult
];
_ ->
[
ataxic:update_field
(
CurrentIX,
ataxic:sequence(lists:reverse(CurrentOPs))
)
|CurrentResult
]
end
)
}
end
end,
{-1, [], []},
SortedFieldUpdates
),
% Add the merged LastUpdateOPs for field LastIX
MergedFieldUpdates =
(
case LastUpdateOPs of
[] -> OtherMergedFieldUpdates;
[OP] ->
[
ataxic:update_field(LastIX, OP)
|OtherMergedFieldUpdates
];
_ ->
[
ataxic:update_field
(
LastIX,
ataxic:sequence(lists:reverse(LastUpdateOPs))
)
|OtherMergedFieldUpdates
]
end
),
% Skip the OPs until we find another field update
{ImportantOPs, PotentialFieldUpdates} =
lists:splitwith
(
fun (E) -> not is_record(E, upfield) end,
PotentiallyImportantOPs
),
optimize_update_field_sequence
(
PotentialFieldUpdates,
(CurrentResults ++ MergedFieldUpdates ++ ImportantOPs)
).
-spec flatten_sequence (list(ataxic:basic())) -> list(ataxic:basic()).
flatten_sequence (OPs) ->
lists:foldl
(
fun (E, CurrentOPs) ->
case is_record(E, seq) of
true -> (E#seq.ops ++ CurrentOPs);
_ -> [E|CurrentOPs]
end
end,
[],
OPs
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec aggressive
(ataxic:basic()) -> ataxic:basic();
(ataxic:meta()) -> ataxic:meta().
aggressive (In = #field{ op = OP }) ->
In#field{ op = aggressive(OP) };
aggressive (In = #upfield{ op = S0OP}) ->
S1OP = aggressive(S0OP),
case S1OP of
#current{} -> S1OP;
_ -> In#upfield{ op = S1OP }
end;
aggressive (#seq{ ops = S0OPs }) ->
S1OPs = lists:map(fun aggressive/1, S0OPs),
S2OPs = flatten_sequence(S1OPs),
S3OPs = lists:filter(fun (E) -> (not is_record(E, current)) end, S2OPs),
Result = optimize_update_field_sequence(S3OPs, []),
case Result of
#seq{ ops = S4OPs } -> #seq{ ops = remove_overridden_operations(S4OPs) };
_ -> Result
end;
aggressive (In = #apply_fun{ params = OPs }) ->
In#apply_fun
{
params = lists:map(fun aggressive/1, OPs)
};
aggressive (In = #list_cons{ param = OP }) ->
In#list_cons{ param = lists:map(fun aggressive/1, OP) };
aggressive (In = #read_perm{ op = OP }) ->
In#read_perm{ op = aggressive(OP) };
aggressive (In = #write_perm{ op = OP }) ->
In#write_perm{ op = aggressive(OP) };
aggressive (In = #lock{ op = OP }) ->
In#lock{ op = aggressive(OP) };
aggressive (In = #value{ op = OP }) ->
In#value{ op = aggressive(OP) };
aggressive (In = #mseq{ ops = OPs }) ->
In#mseq{ ops = lists:map(fun aggressive/1, OPs) };
aggressive (Other) ->
Other.
-spec light
(ataxic:basic()) -> ataxic:basic();
(ataxic:meta()) -> ataxic:meta().
light (#seq{ ops = S0OPs }) ->
S1OPs = flatten_sequence(S0OPs),
S2OPs = lists:filter(fun (E) -> (not is_record(E, current)) end, S1OPs),
Result = optimize_update_field_sequence(S2OPs, []),
Result;
light (OP) -> OP.
|