prologcase-conversion

How can I convert uppercase input to the lowercase?


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.


Solution

  • 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)