So I have something like this:
main :-
% Check whether B,C,D is equal to A
... ,
% Relevant code:
(
(B==A -> write('B is the same as A.'));
(C==A -> write('C is the same as A.'));
(D==A -> write('D is the same as A.'));
).
Is there any way that this could be shortened but still print the relevant letter? There could be 100's of letters to test so this current method is not very nice.
Just a quick note in case you weren't aware of this difference: When you call A == B, you're resting whether the value bound to the variable A is equivalent to the value bound to variable B. But when you use write/1 to output
'B is the same as A.', you are just outputting the atomic literal represented by that string of letters. There is no relationship between the character 'A' as part of an atom and the value bound to a variable which is represented by A (no ') in your source code.
So I'm not 100% clear on your intended result, but here are two different solutions that demonstrate the use of the format family of predicates for outputting values and literals:
If you just want to compare the values of two variables, you can use a predicate to perform the comparison and printout the desired result, which can then be used on all members of a list (forall/2 is appropriate here because we are only concerned with output):
report_on_equality(A, B) :-
A == B,
format('~w is the same as ~w.~n', [A, B]).
report_on_equality(A, B) :-
A \== B,
format('~w is not the same as ~w.~n', [A, B]).
example_1 :-
Vals = [1,4,6,1,7],
forall( member(V, Vals),
report_on_equality(V, 1)
).
But there is no reason to output the value of the variables twice in this case, since if they are equivalent, they will of course be the same value. So maybe you actually want to print out uppercase characters that have been previously associated with values. This, of course, requires that you have first made some paring between uppercase characters and some other values. I have chosen to use a simple list of pairs for this purpose:
report_on_labeled_equality(LabelA-ValA, LabelB-ValB) :-
ValA == ValB,
format('~w is the same as ~w.~n', [LabelA, LabelB]).
report_on_labeled_equality(LabelA-ValA, LabelB-ValB) :-
ValA \== ValB,
format('~w is not the same as ~w.~n', [LabelA, LabelB]).
example_2 :-
Vals = ['B'-1, 'C'-3, 'D'-1, 'E'-4],
forall( member(V, Vals),
report_on_labeled_equality(V, 'A'-1)
).
Related
I am trying to write a tiny recursive rewrite system inspired by Aristid Lindenmayers L-System basically to learn Prolog as well as to think about generative concepts in Prolog. I would like to achieve this without DCG. Due to the initial generate. and output predicate with side effects it is not a 100% pure prolog idea. DonĀ“t hesitate to take the concept apart.
My main problem is at the end of the listing. Matching the rule for every element in the original list and creating a new list with the result of each substitution.
[a] the Axiom becomes [a,b] becomes [a,b,a] and so on. Or better as a list of lists
[[a,b],[a]] to keep it more flexible and comprehensible and then flatten it later?
Basic example without constants, which could be added in a similar way. The Axiom is just used one time at the start. The idea is to encode the rule name or symbol to exchange and the symbols it should be exchanged with, as a fact/relation. Start with generate. would repeat it 20 times with a counter.
% The rules
axiom(a, [a]).
rule(a, [a, b]).
rule(b, [a]).
% Starts the program
generate :-
axiom(a, G),
next_generation(20, G).
% repeats it until counter 0.
next_generation(0, _) :- !.
next_generation(N, G) :-
output(G),
linden(G, Next),
succ(N1, N),
next_generation(N1, Next).
% Concatenates the list to one string for the output
output(G) :-
atomic_list_concat(G,'',C),
writeln(C).
% Here I am stuck, the recursive lookup and exchange.
% How do I exchange each element with the according substitution to a new list
linden([],[]). % Empty list returns empty list.
linden([R],Next) :- % List with just one Element, e.g. the axiom
rule(R, Next). % Lookup the rule and List to exchange the current
linden([H|T], Next) :- % If more than one Element is in the original list
rule(H,E), % match the rule for the current Head/ List element
% ????? % concatenate the result with the result of former elements
linden(T, Next). % recursive until the original list is processed.
% Once this is done it returns the nw list to next_generation/2
Yes, you want lists of lists. Each character can then map cleanly to one corresponding expansion list:
linden([], []).
linden([H|T], [E | Next]) :-
rule(H, E),
linden(T, Next).
(This is simpler and shorter than with a DCG for the same thing, BTW.)
For example:
?- linden([a], Expansion).
Expansion = [[a, b]].
?- linden([a, b, a], Expansion).
Expansion = [[a, b], [a], [a, b]].
Then flatten this to a flat list before expanding the next generation.
The predicate needs to compare two lists (one of variables, one of constants) like this :
?- test([A,B,B],[1,2,3]).
false.
?- test([A,B,B],[1,2,2]).
true.
?- test([A,B,C],[1,2,2]).
false.
First i associate each variable to its constant with this predicate :
set([],[]).
set([X],[Y]):-X is Y.
set([H1|T1],[H2|T2]):-H1 is H2, set(T1,T2).
It works for the first two exemples above however it doesn't write "true". Also it doesn't work for the third one :
?- set([A,B,C],[1,2,2]).
A = 1,
B = C, C = 2
How do can I modify this predicate so it checks if T1 was already used and in that case if it was associated to a different variable (and therefore return false)?
You can add a dif/2 constraint between every two different variables variables.
We can obtain the list of variables with term_variables/2, and then we can for example design a predicate all_diff/1 that applies dif between every two different variables by making use of maplist/2, like:
all_diff([]).
all_diff([H|T]) :-
maplist(dif(H), T),
all_diff(T).
So we can define our set/2 as:
set(V, W) :-
term_variables(V, VV),
all_diff(VV),
maplist(is, V, W).
The original set/2 can thus be written as maplist/3 with is/2 as goal.
For example:
?- set([A,B,B], [1,2,2]).
A = 1,
B = 2.
?- set([A,B,C], [1,2,2]).
false.
If the second list contains only terms, and you do not want to evaluate expressions, we can - like #DanielLyons says - just use V = W:
set(V, W) :-
term_variables(V, VV),
all_diff(VV),
V = W.
Since the unification algorithm will "peal" the functors, and thus eventually unfiy all elements in the left list with the values in the right list.
I have a predicate that takes two arguments in which the first argument can be a compound one and the second argument is always B. I have also defined some new operators like + and &.
pvc(A, B) :- somestuff(A, B).
Here the user may type something like pvc((x+y)&(A+b), B).
As a beginner in Prolog, what I want to do is to convert the compound A to all lowercase and call somestuff with new AN. So it shall be somestuff((x+y)&(a+b), B).
I tried something like pvc(A, B) :- downcase_atom(A,AN),somestuff(AN, B). But it doesn't seem like to be the correct way. I will appreciate any help.
So, you will need to induct on the structure of your thing, and the easiest way to do this is with the univ operator =../2. First handle your base case, which you did:
downcase_compound(A, B) :- atom(A), downcase_atom(A, B).
Now you will take the structure apart and induct on it using univ:
downcase_compound(A, B) :-
compound(A),
A =.. [Functor|Args],
downcase_compound(Functor, DowncaseFunctor),
maplist(downcase_compound, Args, DowncaseArgs),
B =.. [DowncaseFunctor|DowncaseArgs].
The trick here is simply breaking your compound down into bits you can use, and then recursively calling downcase_compound/2 on those bits. See it in action:
?- downcase_compound((x+y,('A'+b)), X).
X = (x+y, a+b)
I have defined a predicate find_word/2 that when given a list of letters (with some letters possibly ungrounded), produces possible words that match the pattern given in the list. This is something like a hangman solver.
word('entity', n, 11).
word('physical entity', n, 1).
word('abstraction', n, 0).
% ... and 200,000 more entries ...
% Example: find_word([_,o,u,n,t,r,y], X) -> X = country
find_word(LetterList, Word) :-
word(Word, _, _),
atom_chars(Word, LetterList).
The code above works as intended. The challenge is that I receive hangman problems from outside the Prolog system as a string (e.g. app_e), where the underscores in the string represent the missing letters to be found by the prolog program above. i.e. I need to convert the app_e string into a list that can be fed into find_word/2.
On my first attempt, I used atom_chars\2:
?- atom_chars(app_e, L), find_word(L, Word).
Unfortunately, this does not work as hoped because atom_chars(app_e, L) -> L = [a, p, p, '_', e]. i.e. the '_' isn't a wildcard.
In summary, given a string app_e, how do I transform it into a list that can be fed into find_word\2 to achieve the same effect as find_word([a,p,p,_,e], Word).?
I think atom_chars/2 is working as intended here, you just need a little cleanup step to finish turning your input into the desired form, which I think you can do quite straightforwardly like so:
charvar('_', _).
charvar(C, C) :- C \= '_'.
Usage looks like this:
?- maplist(charvar, [a,p,p,'_',e], X).
X = [a, p, p, _3398, e] .
Don't worry about the fact that this variable is not rendered as an underscore; your own probably wouldn't be either:
?- X=[_].
X = [_3450].
I am a newbie to prolog and am trying to write a program which returns the atoms in a well formed propositional formula. For instance the query ats(and(q, imp(or(p, q), neg(p))), As). should return [p,q] for As. Below is my code which returns the formula as As. I dont know what to do to split the single F in ats in the F1 and F2 in wff so wff/2 never gets called. Please I need help to proceed from here. Thanks.
CODE
logical_atom( A ) :-
atom( A ),
atom_codes( A, [AH|_] ),
AH >= 97,
AH =< 122.
wff(A):- ground(A),
logical_atom(A).
wff(neg(A)) :- ground(A),wff(A).
wff(or(F1,F2)) :-
wff(F1),
wff(F2).
wff(and(F1,F2)) :-
wff(F1),
wff(F2).
wff(imp(F1,F2)) :-
wff(F1),
wff(F2).
ats(F, As):- wff(F), setof(F, logical_atom(F), As).
First, consider using a cleaner representation: Currently, you cannot distinguish atoms by a common functor. So, wrap them for example in a(Atom).
Second, use a DCG to describe the relation between a well-formed formula and the list of its atoms, like in:
wff_atoms(a(A)) --> [A].
wff_atoms(neg(F)) --> wff_atoms(F).
wff_atoms(or(F1,F2)) --> wff_atoms(F1), wff_atoms(F2).
wff_atoms(and(F1,F2)) --> wff_atoms(F1), wff_atoms(F2).
wff_atoms(imp(F1,F2)) --> wff_atoms(F1), wff_atoms(F2).
Example query and its result:
?- phrase(wff_atoms(and(a(q), imp(or(a(p), a(q)), neg(a(p))))), As).
As = [q, p, q, p].
This should do what you want. It extracts the unique set of atoms found in any arbitrary prolog term.
I'll leave it up to you, though, to determine what constitutes a "well formed propositional formula", as you put it in your problem statement (You might want to take a look at DCG's for parsing and validation).
The bulk of the work is done by this "worker predicate". It simply extracts, one at a time via backtracking, any atoms found in the parse tree and discards anything else:
expression_atom( [T|_] , T ) :- % Case #1: head of list is an ordinary atom
atom(T) , % - verify that the head of the list is an atom.
T \= [] % - and not an empty list
. %
expression_atom( [T|_] , A ) :- % Case #2: head of listl is a compound term
compound(T) , % - verify that the head of the list is a compound term
T =.. [_|Ts] , % - decompose it, discarding the functor and keeping the arguments
expression_atom(Ts,A) % - recurse down on the term's arguments
. %
expression_atom( [_|Ts] , A ) :- % Finally, on backtracking,
expression_atom(Ts,A) % - we simply discard the head and recurse down on the tail
. %
Then, at the top level, we have this simple predicate that accepts any [compound] prolog term and extracts the unique set of atoms found within by the worker predicate via setof/3:
expression_atoms( T , As ) :- % To get the set of unique atoms in an arbitrary term,
compound(T) , % - ensure that's its a compound term,
T =.. [_|Ts] , % - decompose it, discarding the functor and keeping the arguments
setof(A,expression_atom(Ts,A),As) % - invoke the worker predicate via setof/3
. % Easy!
I'd approach this problem using the "univ" operator =../2 and explicit recursion. Note that this solution will not generate and is not "logically correct" in that it will not process a structure with holes generously, so it will produce different results if conditions are reordered. Please see #mat's comments below.
I'm using cuts instead of if statements for personal aesthetics; you would certainly find better performance with a large explicit conditional tree. I'm not sure you'd want a predicate such as this to generate in the first place.
Univ is handy because it lets you treat Prolog terms similarly to how you would treat a complex s-expression in Lisp: it converts terms to lists of atoms. This lets you traverse Prolog terms as lists, which is handy if you aren't sure exactly what you'll be processing. It saves me from having to look for your boolean operators explicitly.
atoms_of_prop(Prop, Atoms) :-
% discard the head of the term ('and', 'imp', etc.)
Prop =.. [_|PropItems],
collect_atoms(PropItems, AtomsUnsorted),
% sorting makes the list unique in Prolog
sort(AtomsUnsorted, Atoms).
The helper predicate collect_atoms/2 processes lists of terms (univ only dismantles the outermost layer) and is mutually recursive with atoms_of_prop/2 when it finds terms. If it finds atoms, it just adds them to the result.
% base case
collect_atoms([], []).
% handle atoms
collect_atoms([A|Ps], [A|Rest]) :-
% you could replace the next test with logical_atom/1
atom(A), !,
collect_atoms(Ps, Rest).
% handle terms
collect_atoms([P|Ps], Rest) :-
compound(P), !, % compound/1 tests for terms
atoms_of_prop(P, PAtoms),
collect_atoms(Ps, PsAtoms),
append(PAtoms, PsAtoms, Rest).
% ignore everything else
collect_atoms([_|Ps], Rest) :- atoms_of_prop(Ps, Rest).
This works for your example as-is:
?- atoms_of_prop(ats(and(q, imp(or(p, q), neg(p))), As), Atoms).
Atoms = [p, q].