Conditional equations with a directive for flipping arguments
Kuniaki Mukai <[email protected]> Fri, 8 Aug 2014 23:44:27 +0900
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
Hi,
I have updated my term_expansion codes for conditional equations.
Some features are:
1. A simple and transparent rules for evaluation.
2. No runtime helpers needed.
3. flip of arguments positions of Prolog predicates to be used as functions.
5. Anonymous predicates to be used for function definitions.
6. information hiding syntax like DCG.
So far since old version I have written more than 100 examples including
experimental ones.
Below are two examples "string" and "list" class operations
with listings of expanded codes, which show, I hope,
that it has reached to a usable level for others. However,
the demos include only features 1, 2, 3 above.
Is it really useful for Prolog user ? I hope so, but currently it is just
a kind of macros facility for functional notations. So I have to say,
it is not essential at all for Prolog user. Also currently the equation macro
is private, no documentation for others are available. But syntax and
semantics are so clear, I hope the macro codes is readble enough with
a minimal amount of documentations prepared in the future. Also from
my experience I even hope one will find
better syntax, semantics and implementations for the functional syntax
for Prolog.
My justification of the proposed conditional equations in Prolog is
simple as follows. It is fundamental fact that function symbols can be
eliminated from a FOL(first-order language) in such a way that
provability is preserved. In other words, function symbols are only
macros for predicate logic. The term_expansion for the current
implementation has been written following the fundamental fact. In
fact, what the tranlation does is a recursive rewriting
the following equation:
[[P @ A1 @ ... @ An]] = [[P]]([[A1]], ..., [[An]]),
where the symbol [[ E ]] means the semantics of the expression E
as in the literature on the FOL semantics.
The language OBJ proposed by J. Goguen et al exploits conditional
equations as syntax of their language. Unfortunately, my memory has
no further information about OBJ to add.
The "list" below is a "class" with concatenation, prefix, and suffix operators
:-bekind(list).
A+B = :append@A@B.
(A\B) = :flip([X,Y,Z] :- append(X,Z,Y))@A@B.
(A/B) = :flip([X,Y,Z] :- append(Z,Y,X))@A@B.
^(E,L) = :times(N) @ E :- N is L.
^^(E,L) = :flip([A, C] :- math:nlist(A, N, C)) @ E :- N is L.
X = `(X) :- is_list(X).
(+A) = :append@A.
flat(A) = :flatten@A.
:- ekind.
"flip([X1,..., Xn]:- P(X1,...,Xn), A1,..., An)" above is
a directive, which works as if it were a meta-predicate defined as follows:
flip([X1,..., Xn]:- P(X1,...,Xn), A1,..., An)
<==>
[X1,..., Xn] = [A1,..., An], P(X1,..., Xn)
that is, P(A1, ..., An).
Sample queries for list/2:
% ?- list([a,b]^0+[c,d]^1+[e,f]^2, R), length(R, L).
%@ R = [[], [c], [d], [e, e], [e, f], [f, e], [f, f]],
%@ L = 7
% ?- list([a,b]^^3, R), length(R, L).
%@ L = 15 .
%@ R = [[], [a], [a, a], [a, a, a], [a, a, b], [a, b], [a, b|...], [a|...], [...]|...],
The "string" below is a "class" with concatenation,
prefix, and suffix operators.
:-bekind(string).
A+B = :string_concat@A@B.
(A\B) = :flip([X,Y,Z] :- string_concat(X,Z,Y))@A@B.
(A/B) = :flip([X,Y,Z] :- string_concat(Z,Y,X))@A@B.
^(E,L) = :string_times(N) @ E :- N is L.
reverse(X) = :flip([A, B]:- string_codes(B, A))
@ (:reverse @ (:string_codes@X)).
(+A) = :string_list_concat@ (:list@ A).
X = `(X) :- string(X).
:- ekind.
Some Prolog predicates used:
% ?- string_list_concat(["ab", "cd", "ef"], Y).
%@ Y = "efcdab".
string_list_concat(X, Y):-
foldl(pred([A, U, V]:- string_concat(A, U, V)),
X, "", Y).
% ?- string_times(3, "ab", X).
%@ X = "ababab" .
string_times(0, _, "").
string_times(N, A, B):- N>0, N0 is N-1,
string_times(N0, A, B0),
string_concat(A, B0, B).
Sample queries for string/2:
% ?- string("ab"+"cd"+"ef", X).
%@ X = "abcdef" .
% ?- string("abcd"/"cd", X).
%@ X = "ab" .
% ?- string("ab"\"abcd", X).
%@ X = "cd" .
% ?- string(("ab"\"abcd")^3, X).
%@ X = "cdcdcd" .
% ?- string(reverse("abcd"), X).
%@ X = "dcba" .
% ?- string(reverse(reverse("uvw") + reverse("ab" + "12")), X).
%@ X = "ab12uvw" .
?- listing(list/2).
%@ list(A+B, E) :-
%@ list(A, C),
%@ list(B, D),
%@ append(C, D, E).
%@ list((A\B), D) :-
%@ list(A, C),
%@ list(B, E),
%@ append(C, D, E).
%@ list(A/B, C) :-
%@ list(A, E),
%@ list(B, D),
%@ append(C, D, E).
%@ list(B^A, E) :-
%@ C is A,
%@ list(B, D),
%@ times(C, D, E).
%@ list((B^^A), E) :-
%@ D is A,
%@ list(B, C),
%@ math:nlist(C, D, E).
%@ list(A, A) :-
%@ is_list(A).
%@ list(+A, C) :-
%@ list(A, B),
%@ append(B, C).
%@ list(flat(A), C) :-
%@ list(A, B),
%@ flatten(B, C).
%@
%@ true.
?- listing(string/2).
%@ string(A+B, E) :-
%@ string(A, C),
%@ string(B, D),
%@ string_concat(C, D, E).
%@ string((A\B), D) :-
%@ string(A, C),
%@ string(B, E),
%@ string_concat(C, D, E).
%@ string(A/B, C) :-
%@ string(A, E),
%@ string(B, D),
%@ string_concat(C, D, E).
%@ string(B^A, E) :-
%@ C is A,
%@ string(B, D),
%@ string_times(C, D, E).
%@ string(reverse(A), D) :-
%@ string(A, B),
%@ string_codes(B, C),
%@ reverse(C, E),
%@ string_codes(D, E).
%@ string(+A, D) :-
%@ string(A, B),
%@ list(B, C),
%@ string_list_concat(C, D).
%@ string(A, A) :-
%@ string(A).
%@
%@ true.
Regards,
Kuniaki Mukai
p.s. I am learning GIT to make the source codes to be available for
others, which seems ten times difficult for me than writing the
term_expansion codes for the conditional equations. git-clone and
git-pull has been all that I knows about GIT, which for long has been
enough for my purpose.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 496 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <https://lists.iai.uni-bonn.de/pipermail/swi-prolog/attachments/20140808/a9018846/signature.asc>