So I’m playing with a very simplified CHR/rewrite thing.
I hope to declare rewrites with code like
this, that ~~> something, else
.
Then expect step([this, that | Rest], [something, else | Rest])
So far the code does what I hope, but I can’t seem to appease pceemacs that a ~~> b.
is valid code.
Working with Claude/ChatGPT and trying to learn from yall/chr has simply resulted in many failed attempts. Any help is appreciated. Actual code starts at % ACTUAL CODE
and the rest is the current attempt at the proper incantations.
:- module(steps,
[ steps/2,
op(1150, xfx, '~~>')
]).
% ATTEMPTS at syntax handling/coloring in PceEmacs
:- meta_predicate
transformation(?,0,?),
steps(:, ?).
:- dynamic transformation/3.
:- multifile transformation/3.
:- op(1150, xfx, '~~>').
% Additional CHR-style declarations
:- multifile prolog:called_by/4.
:- multifile prolog:called_by_1/4.
:- multifile prolog_colour:term_colours/2.
% Tell Prolog how to analyze calls to the operator
prolog:called_by('~~>'(_,_), [], _, []).
prolog:called_by_1('~~>'(_,_), [], _, []).
% Tell PceEmacs how to color the terms
prolog_colour:term_colours((Head ~~> Body), expanded - [classify, classify]).
% Define how the operator should be treated in analysis
:- multifile prolog:hook/1.
prolog:hook('~~>'(_,_)).
% END ATTEMPTS at syntax handling/coloring in PceEmacs
% ACTUAL CODE
user:term_expansion(T, G) :-
\+ var(T),
~~>(A,B) = T,
prolog_load_context(module, Module),
( B = '|'(GoalB, BodyB)
-> G = Module:transformation(A, GoalB, BodyB)
; G = Module:transformation(A, true, B)
).
steps(M:In, Final) :-
( M:transformation(A, G, B),
remove_terms(A, In, Intermediate),
( call(G)
*-> add_terms(B, Intermediate, Next),
steps(M:Next, Final)
; fail
)
-> true
; Final = In
).
remove_terms(NonVar, In, Out) :-
\+ var(NonVar),
( functor(NonVar, ',', 2)
-> (A,R) = NonVar,
( ground(A)
-> selectchk(A, In, Next)
; select(A, In, Next)
),
remove_terms(R, Next, Out)
; select(NonVar, In, Out)
).
add_terms(NonVar, In, Final) :-
\+ var(NonVar),
( functor(NonVar, ',', 2)
-> (A,R) = NonVar,
add_terms(R, [A|In], Final)
; Final = [NonVar|In]
).