-
Notifications
You must be signed in to change notification settings - Fork 0
/
betaConversion.pl
153 lines (112 loc) · 5.31 KB
/
betaConversion.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
146
147
148
149
150
151
152
153
/*************************************************************************
File: betaConversion.pl
Copyright (C) 2004,2005,2006 Patrick Blackburn & Johan Bos
This file is part of BB1, version 1.3 (November 2006).
BB1 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
BB1 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BB1; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*************************************************************************/
:- module(betaConversion,[info/0,
infix/0,
prefix/0,
betaConvertTestSuite/0,
betaConvert/2]).
:- use_module(comsemPredicates,[infix/0,
prefix/0,
compose/3]).
:- use_module(alphaConversion,[alphaConvert/2,
alphabeticVariants/2]).
:- use_module(betaConversionTestSuite,[expression/2]).
/*========================================================================
Beta-Conversion (introducing stack)
========================================================================*/
betaConvert(X,Y):-
betaConvert(X,Y,[]).
/*========================================================================
Beta-Conversion (comment-in for tracing)
========================================================================*/
%betaConvert(X,_,S):-
% nl, write('Expre: '), print(X),
% nl, write('Stack: '), print(S), nl,
% fail.
/*========================================================================
Beta-Conversion (core stuff)
========================================================================*/
betaConvert(X,Y,[]):-
var(X), !,
Y=X.
betaConvert(Expression,Result,Stack):-
nonvar(Expression),
Expression = app(Functor,Argument),
%% To suppress alpha-conversion:
alphaConvert(Functor,Converted), %% comment-out this line
% Functor=Converted, %% comment-in this line
betaConvert(Converted,Result,[Argument|Stack]), !.
betaConvert(Expression,Result,[X|Stack]):-
nonvar(Expression),
Expression = lam(X,Formula),
betaConvert(Formula,Result,Stack), !.
betaConvert(Formula,Result,[]):-
nonvar(Formula), !,
compose(Formula,Functor,Formulas),
betaConvertList(Formulas,ResultFormulas),
compose(Result,Functor,ResultFormulas).
betaConvert(Exp,app(Exp,Y),[X]):- %% Impossible to perform application.
betaConvert(X,Y).
/*========================================================================
Beta-Convert a list
========================================================================*/
betaConvertList([],[]).
betaConvertList([Formula|Others],[Result|ResultOthers]):-
betaConvert(Formula,Result),
betaConvertList(Others,ResultOthers).
/*========================================================================
Prove all formulas from the test suite
========================================================================*/
betaConvertTestSuite:-
format('~n>>>>> BETA CONVERSION ON TEST SUITE <<<<<~n',[]),
expression(Expression,Expected),
format('~n~nExpression: ~p~nExpected: ~p',[Expression,Expected]),
betaConvert(Expression,Converted,[]),
format('~nConverted: ~p',[Converted]),
compareResults(Expected,Converted,Result),
format('~nResult: ~p',[Result]),
fail.
betaConvertTestSuite.
/*========================================================================
Compare Results of the Test Suite
========================================================================*/
compareResults(A,B,Result):-
(
alphabeticVariants(A,B),
!,
Result=ok
;
Result=error
).
/*========================================================================
Info
========================================================================*/
info:-
format('~n> ------------------------------------------------------------------- <',[]),
format('~n> betaConversion.pl, by Patrick Blackburn and Johan Bos <',[]),
format('~n> <',[]),
format('~n> ?- betaConvert(F,C). - beta-convert a formula <',[]),
format('~n> ?- betaConvertTestSuite. - run the test suite <',[]),
format('~n> ?- infix. - switches to infix display mode <',[]),
format('~n> ?- prefix. - switches to prefix display mode <',[]),
format('~n> ?- info. - shows this information <',[]),
format('~n> ------------------------------------------------------------------- <',[]),
format('~n~n',[]).
/*========================================================================
Display info at start
========================================================================*/
% :- info.