prologdcg

Writing elements in prolog console


In prolog I have do to something like this:

    ************************
    *                      *
    *      ##########      *
    *      # button #      *
    *      ##########      *
    *                      *
    ************************

User gives the width and height of window (created with *), and coordinates, width, height and text of button (created by #). This "window" is written in prolog console. I don't have trouble with writing the window itself but I don't know how to do it with something in it. Can anyone help me, I don't mean writing it for me, but even small guidenes will be helpfull.


Solution

  • Write it with the help of a DCG as a clean grammar.

    Here is a start. More details, here.

    :- use_module(library(double_quotes)).
    :- set_prolog_flag(double_quotes,chars).
    
    newline --> "\n".
    
    pre --> "# ".
    
    post --> " #".
    
    button -->
       pre,
       "button",
       post.
    

    Now you can query it on the toplevel:

    ?- phrase(button, Xs).
       Xs = "# button #".
    

    And you can print it out directly:

    ?- phrase(button, Xs), atom_chars(A, Xs), write(A).