Improving Wang's algorithm in Prolog

Use format/2,3 instead of write/1.

Your way with write/1

help_01 :-
    write('-----------------------------------------------------------'),nl,
    write('\nA Propositional Theorem Prover using Wang\'s Algorithm\n'),
    write(' \tby Ben Hudson\n\n'),
    write('-----------------------------------------------------------'),nl,
    write('To test the validity of your formula with Wang\'s algorithm:\n'),
    write('- First, write "wang." and press Enter.\n'),
    write('- Second, with this usual syntax: ~ a | a &  b | a v b | a -> b \n'),
    write('put your formula into square brackets (e.g. [p -> p]), press Enter.\n'),
    write('That\'s it. Have fun!').

Using format/2,3

help('\c
    -----------------------------------------------------------\n\n\c
    A Propositional Theorem Prover using Wang\'s Algorithm\n\c
    \tby Ben Hudson\n\n\c
    -----------------------------------------------------------\n\c
    To test the validity of your formula with Wang\'s algorithm:\n\c
    - First, write "wang." and press Enter.\n\c
    - Second, with this usual syntax: ~ a | a &  b | a v b | a -> b \n\c
    put your formula into square brackets (e.g. [p -> p]), press Enter.\n\c
    That\'s it. Have fun!\c
').

help_02 :-
    help(Help),
    format('~a',[Help]).

\c is not common.
See: Character Escape Syntax

I am also thinking you should replace read/1 which reads Prolog terms with DCGs because many people don’t understand what a Prolog term is, they get frustrated quickly and walk away when seeing error messages that don’t help them understand how to change the input to make it valid.

If you are taking the time to make the code work then take the time to make the user interface user friendly. I can’t count how many times I have walked away from code that I know is of use just because it was not easy to use or understand.

Learning DCGs could take days to weeks to get correct depending on your skill level, but having been their and having done the work it is worth it. If you search the SWI-Prolog code at GitHub for --> (search) you will see how often they are used because DCGs are just that useful.

As an example of learning something different to get the user interfaces to be nice, for many years I have been using GraphViz to display graphs. Since Prolog is about relationships and Graphs are great for drawing relationships, the synergy is great. But one of the things that leaves a bad taste in my mouth with GraphViz is that the graphs are static, typically displayed as PNG, PDF, or SVG.

Graphs should allow a user to move the nodes around and so the last several days I have been learning how to convert Prolog facts into Cytoscape.js graphs. Quite the learning experience. But once done I will update the Wiki page and we will all have more knowledge to draw upon when using Prolog with Cytoscape.js. I must note that @CapelliC did something similar for SWISH (ref).

Also you might want to consider having your code work on SWISH. Don’t ask me about SWISH as I don’t use it currently. Not that I don’t want to, but one can only do so much at a time.

If you want to get more advanced with printing messages, see: Printing Messages in SWI-Prolog

3 Likes