While cleaning up my “dict prettyprinter”, a little solution to the problem of
on the one hand, and
on the other hand,
which are non-logical because they “accept a lot” but provide “at most one solution”.
Direct
The README and code, including plunit test code
Currently a bit hard to install as you have to install dependencies, but the plan is to package all that ASAP.
Explainer
A new predicate:
stringy_morph(StringyA,StringyB,TypeOfStringyA,TypeOfStringB)
which takes type information to cleanly transform between string and atom.
Example
I want a string at argument place 2, StringyB
. I don’t care to be told about the type of argument 1, so I provide a _
at argument place 3:
?- stringy_morph(hello,StringyB,_,string).
StringyB = "hello".
If I state that the type of argument 1 is a string , I get told otherwise:
?- stringy_morph(hello,StringyB,string,string).
false.
Because it’s an atom :
?- stringy_morph(hello,StringyB,atom,string).
StringyB = "hello".
If I am lax in specifying the wanted output type, I get two solutions:
?- stringy_morph(hello,StringyB,_,Whatever).
StringyB = hello, Whatever = atom ;
StringyB = "hello", Whatever = string.
Of course, one can accept a pair of arguments:
?- stringy_morph(hello,"string",atom,string).
true.
Or query their type, as long as they can be morphed from one to the other:
?- stringy_morph(hello,"hello",TypeA,TypeB).
TypeA = atom,
TypeB = string.
Similarly a new predicate:
stringy_charylist_morph(Stringy,Charylist,StringyType,CharylistType)
which behaves likewise for transformations between atom/string and list of code/char.