-
I am working on transpiling lisp code to Prolog code. I have a question that I don't have the correct words for, and may not make sense. For background, I may define a predicate in Clojure syntax as: (:- (foo ?x ?y) (#= ?y (+ ?x 1)) with the hopes that this would transpile to foo(X, Y) :-
Y #= X + 1. To make the example really simple, this clearly does not work: named_vars(VarNameIn, VarNameOut, Pred) :-
Pred=(foo(VarNameIn, VarNameOut) :- VarNameIn #= VarNameOut + 1). Would I need to do this with string formatting DCGs...? I realize this may be asking the wrong question. If someone can help me figure out what the right question is, I'd appreciate it. I realize what I'm trying to do is in some ways nonsensical, it probably doesn't matter what the variables are named so long as they are logically distinct. But I haven't fully wrapped my head around the problem yet. Update Still confused, my question still doesn't make sense. I'd need to take a string and convert it to named variable, I think. So, named_vars(VarNameChar, Pred) :-
something.
?- varnamestring_pred("Zebra", Pred).
%@ Pred = foo(Zebra). This is what I meant to do. Update 2 I think I've narrowed my question down to "how do I implement variablenamestr_pred(VarNameStr, Pred) :-
write_term_to_chars(foo('~s'), [], PredString),
phrase(format_(PredString, [VarNameStr]), PredStringOut, []),
string_term(PredStringOut, Pred).
If we implement variablenamestr_pred(VarNameStr, Pred) :-
write_term_to_chars(foo('~s'), [], PredString),
phrase(format_(PredString, [VarNameStr]), PredStringOut, []),
string_term(PredStringOut, Pred).
string_term(A,B) :- A=B.
?- variablenamestr_pred("Zebra", Pred).
%@ Pred = "foo(Zebra)".
So close! Update 3 After confusing the heck out of everyone, I realize I don't actually need this functionality right now. What I need to do is to convert a clojure string to a prolog string and then feed the prolog string into the shared library code. At some point we should probably look into implementing Update 4 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
Wait, this does work. named_vars(VarNameIn, VarNameOut, Pred) :-
Pred=(foo(VarNameIn, VarNameOut) :- VarNameIn #= VarNameOut + 1).
?- named_vars(X, Y, Pred).
%@ Pred = (foo(X,Y):-X#=Y+1). Wait, lol. What? |
Beta Was this translation helpful? Give feedback.
-
I did something similar in one of my projects (https://github.com/hurufu/ffmpeg-planner/blob/master/fol.prolog) Don't bother reading full project, because currently ChatGPT is 10000 times better at what I wanted that project to be :) But some parts are salvageable. I don't remember much detail, but if I recall correctly (and most probably I don't) the main idea was as follows:
|
Beta Was this translation helpful? Give feedback.
-
The mightiest of facepalms. Thanks @triska |
Beta Was this translation helpful? Give feedback.
read_term_from_chars/3
The mightiest of facepalms. Thanks @triska