Friday code drop: Transforming properly between atom, string, codelist and charlist

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”.


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.


A new predicate:


which takes type information to cleanly transform between string and atom.


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

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

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:


which behaves likewise for transformations between atom/string and list of code/char.

1 Like