1

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.

selubamih
  • 83
  • 3
  • 15
  • 1
    Possible duplicate of [Convert string to Upper and Lower case Turbo Prolog](https://stackoverflow.com/questions/33464226/convert-string-to-upper-and-lower-case-turbo-prolog) & https://stackoverflow.com/a/1243922/6212957 – Feelsbadman Jan 08 '19 at 16:04
  • 1
    Note that if you actually write `A+b` Prolog will interpret that A as a variable, and there is nothing you can do to get from that back to `a`, so write it as an uppercase atom instead using single quotes: `'A'`. – Daniel Lyons Jan 08 '19 at 17:34

1 Answers1

2

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) 
Daniel Lyons
  • 22,421
  • 2
  • 50
  • 77