-
Notifications
You must be signed in to change notification settings - Fork 0
/
eye2-trealla.pl
145 lines (135 loc) · 3.48 KB
/
eye2-trealla.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
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
% --------------------------
% eye2-trealla -- Jos De Roo
% --------------------------
%
% See https://github.com/eyereasoner/eye2-trealla
%
:- use_module(library(format)).
:- use_module(library(iso_ext)).
:- use_module(library(lists)).
:- use_module(library(terms)).
:- op(1200, xfx, ?-).
:- dynamic((?-)/2).
:- dynamic(answer/1).
:- dynamic(brake/0).
:- dynamic(ether/3).
version_info('eye2-trealla v1.3.7 (2024-12-03)').
% main goal
main :-
bb_put(closure, 0),
bb_put(limit, -1),
bb_put(fm, 0),
bb_put(mf, 0),
( (_ ?- _)
-> format(":- op(1200, xfx, ?-).~n~n", [])
; version_info(Version),
format("~w~n", [Version])
),
run,
bb_get(fm, Fm),
( Fm = 0
-> true
; format(user_error, "*** fm=~w~n", [Fm])
),
bb_get(mf, Mf),
( Mf = 0
-> true
; format(user_error, "*** mf=~w~n", [Mf])
),
halt(0).
% run eye2 abstract machine
%
% 1/ select rule Conc ?- Prem
% 2/ prove Prem and if it fails backtrack to 1/
% 3/ if Conc = true assert answer(Prem)
% else if Conc = false stop with return code 2
% else if ~Conc assert Conc and retract brake
% 4/ backtrack to 2/ and if it fails go to 5/
% 5/ if brake
% if not stable start again at 1/
% else output answers, output ethers and stop
% else assert brake and start again at 1/
%
run :-
( (Conc ?- Prem), % 1/
copy_term((Conc ?- Prem), Rule),
Prem, % 2/
( Conc = true % 3/
-> ( \+answer(Prem)
-> assertz(answer(Prem))
; true
)
; ( Conc = false
-> format("% inference fuse, return code 2~n", []),
portray_clause(fuse(Prem)),
halt(2)
; ( term_variables(Conc, [])
-> Concl = Conc
; Concl = (Conc ?- true)
),
\+Concl,
astep(Concl),
assertz(ether(Rule, Prem, Concl)),
retract(brake)
)
),
fail % 4/
; ( brake % 5/
-> ( bb_get(closure, Closure),
bb_get(limit, Limit),
Closure < Limit,
NewClosure is Closure+1,
bb_put(closure, NewClosure),
run
; answer(Prem),
portray_clause(answer(Prem)),
fail
; ( ether(_, _, _)
-> format("~n%~n% Explain the reasoning~n%~n~n", []),
ether(Rule, Prem, Conc),
portray_clause(ether(Rule, Prem, Conc)),
fail
; true
)
; true
)
; assertz(brake),
run
)
).
% assert new step
astep((B, C)) :-
astep(B),
astep(C).
astep(A) :-
( \+A
-> assertz(A)
; true
).
% stable(+Level)
% fail if the deductive closure at Level is not yet stable
stable(Level) :-
bb_get(limit, Limit),
( Limit < Level
-> bb_put(limit, Level)
; true
),
bb_get(closure, Closure),
Level =< Closure.
% debugging tools
fm(A) :-
write(user_error, '*** '),
portray_clause(user_error, A),
bb_get(fm, B),
C is B+1,
bb_put(fm, C).
mf(A) :-
forall(
catch(A, _, fail),
( write(user_error, '*** '),
portray_clause(user_error, A),
bb_get(mf, B),
C is B+1,
bb_put(mf, C)
)
).