-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogic_client.erl
178 lines (124 loc) · 4.56 KB
/
logic_client.erl
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
%% -*- erlang -*-
%%
%% CRE: common runtime environment for distributed programming languages
%%
%% Copyright 2015 Jörgen Brandt <[email protected]>
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% -------------------------------------------------------------------
%% @author Jörgen Brandt <[email protected]>
%% @copyright 2015
%%
%%
%%
%%
%%
%% @end
%% --------------------------------------------------------------------
-module(logic_client).
-behaviour(cre_client).
%%====================================================================
%% Exports
%%====================================================================
-export([init/1, is_value/2, step/2, recv/3, load/2, unload/2]).
-export([start_link/0, start_link/1, start_link/2, eval/2, stop/1]).
%%====================================================================
%% Language definition
%%====================================================================
-type e() :: boolean() |
{'not', e()} |
{'and', e(), e()} |
{'or', e(), e()} |
{fut, e()}.
-type ctx() :: hole |
{'not', ctx()} |
{'and', ctx(), e()} |
{'and', e(), ctx()} |
{'or', ctx(), e()} |
{'or', e(), ctx()}.
%%====================================================================
%% API functions
%%====================================================================
start_link() ->
{ok, CreName} = cre:pid(),
start_link(CreName).
start_link(CreName) ->
cre_client:start_link(CreName, ?MODULE, []).
start_link(ClientName, CreName) ->
cre_client:start_link(ClientName, CreName, ?MODULE, []).
eval(ClientName, T) ->
cre_client:eval(ClientName, T).
stop(ClientName) ->
cre_client:stop(ClientName).
%%====================================================================
%% CRE worker callback functions
%%====================================================================
-spec init(Arg :: _) -> UsrInfo :: _.
init(_InitArg) -> [].
-spec is_value(E :: e(), UsrInfo :: _) -> boolean().
is_value(E, _UsrInfo) -> is_boolean(E).
-spec step(E, UsrInfo) -> {ok, _, [_]}
when E :: _,
UsrInfo :: _.
step(E, _UsrInfo) ->
case find_context(E) of
{ok, {Ctx, TNext}} -> {ok, in_hole(Ctx, {fut, TNext}), [TNext]};
{error, nocontext} -> {ok, E, []}
end.
-spec recv(E, ReplyLst, UsrInfo) -> _
when E :: _,
ReplyLst :: [{_, _}],
UsrInfo :: _.
recv(E, ReplyLst, _UsrInfo) ->
subst_fut(E, ReplyLst).
load(E, _) -> E.
unload(E, _) -> E.
%%====================================================================
%% Internal functions
%%====================================================================
-spec in_hole(Ctx :: ctx(), E :: e()) -> e().
in_hole(hole, T) -> T;
in_hole({'not', E}, T) -> {'not', in_hole(E, T)};
in_hole({'and', E1, E2}, T) -> {'and', in_hole(E1, T), in_hole(E2, T)};
in_hole({'or', E1, E2}, T) -> {'or', in_hole(E1, T), in_hole(E2, T)};
in_hole(T, _) -> T.
find_context(T) ->
case find_context(T, hole) of
[] -> {error, nocontext};
[H | _] -> {ok, H}
end.
find_context(T, _E) when is_boolean(T) -> [];
find_context({fut, _T}, _E) -> [];
find_context({'not', T}, E) when is_boolean(T) ->
[{E, {'not', T}}];
find_context({'not', T}, E) ->
find_context(T, in_hole(E, {'not', hole}));
find_context({Op, T1, T2}, E) when is_boolean(T1), is_boolean(T2) ->
[{E, {Op, T1, T2}}];
find_context({Op, T1, T2}, E) ->
find_context(T1, in_hole(E, {Op, hole, T2})) ++
find_context(T2, in_hole(E, {Op, T1, hole})).
subst_fut({'not', T}, ReplyLst) ->
{'not', subst_fut(T, ReplyLst)};
subst_fut({'and', T1, T2}, ReplyLst) ->
{'and', subst_fut(T1, ReplyLst), subst_fut(T2, ReplyLst)};
subst_fut({'or', T1, T2}, ReplyLst) ->
{'or', subst_fut(T1, ReplyLst), subst_fut(T2, ReplyLst)};
subst_fut({fut, A}, ReplyLst) ->
case lists:keyfind(A, 1, ReplyLst) of
false -> {fut, A};
{A, Delta} -> Delta
end;
subst_fut(V, _) ->
V.