forked from mthom/cl-psoatransrun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxsb_server.pl
111 lines (96 loc) · 3.26 KB
/
xsb_server.pl
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
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The Prolog server for PSOATransRun implementations, for XSB Prolog.
Written in October 2020 by Mark Thom ([email protected])
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
:- module(xsb_server, []).
:- use_module(file_io, [fd2ioport/2, file_close/1, file_flush/2, fmt_write_string/3]).
:- use_module(xsb_writ, [file_write_canonical/2]).
:- use_module(lists, [member/2]).
:- use_module(string, [term_to_codes/3]).
start_server :-
eval_loop.
eval_loop :-
read_term(user_input, Term, [variable_names(VNNames)]),
( Term == end_of_file ->
true
;
read_term(user_input, _, [variable_names(UVNNames)]),
split_vars(VNNames, UVNNames, RVNNames),
catch(call(Term), _, false),
compile_solution_string(RVNNames, VarString),
( ( var(VarString) ; VarString == "" ) ->
write_string("Yes")
;
write_string(VarString)
),
false
;
write_string("No"),
eval_loop
).
write_string(Codes) :-
fmt_write_string(OutString, "%s\n", args(Codes)),
write(OutString).
split_vars([], _, []).
split_vars([VN | VNs], UVNNames, RVNs) :-
( member(VN, UVNNames) ->
split_vars(VNs, UVNNames, RVNs)
;
RVNs = [VN | RVNs1],
split_vars(VNs, UVNNames, RVNs1)
).
compile_solution_string(VNs, VarString) :-
compile_solution_string_(VNs, "", VarString, 0).
compile_solution_string_([], VarString, VarString, _).
compile_solution_string_([VN=Value|VNs], PrefixString, VarString, VarCount0) :-
( var(Value) ->
fmt_write_string(NamedVarString, "'Var%d'", args(VarCount0)),
term_to_codes(VN=NamedVarString, [quoted(false), ignore_ops(true)], Codes),
( PrefixString == "" ->
fmt_write_string(VarString0, "%s", args(Codes))
;
fmt_write_string(VarString0, "%s, %s", args(PrefixString, Codes))
),
Value = NamedVarString,
VarCount is VarCount0 + 1
;
replace_char_lists_with_strings(Value, Y),
term_to_codes(VN=Y, [quoted(false), ignore_ops(true)], Codes),
( PrefixString == "" ->
fmt_write_string(VarString0, "%s", args(Codes))
;
fmt_write_string(VarString0, "%s, %s", args(PrefixString, Codes))
),
VarCount = VarCount0
),
compile_solution_string_(VNs, VarString0, VarString, VarCount).
maplist(_, [], []).
maplist(Pred, [X|Xs], [Y|Ys]) :-
call(Pred, X, Y),
maplist(Pred, Xs, Ys).
atom_quoted(Atom, NewAtom) :-
atom_concat('\'', Atom, NewAtom0),
atom_concat(NewAtom0, '\'', NewAtom).
replace_char_lists_with_strings(X, Y) :-
( number(X) ->
X = Y
; atom(X) ->
( ( atom_concat('_', _, X) ; atom_concat('<', _, X) ) ->
fmt_write_string(Y, "'%s'", args(X))
;
fmt_write_string(Y, "%s", args(X))
)
; is_charlist(X) ->
fmt_write_string(Y, "\"%s\"", args(X))
;
X =.. [F | Args],
( ( atom_concat('_', _, F) ; atom_concat('<', _, F) ) ->
atom_quoted(F, NewF)
;
NewF = F
),
maplist(replace_char_lists_with_strings, Args, NewArgs),
!,
Y =.. [NewF | NewArgs]
).
:- initialization(start_server).