Some ideas on how to present our PEG examples

These are some ideas on several ways in which we can present the Prolog examples developed within PEG, as background material for our discussions:

  1. A simple and standard way is to simply save them in a code repository (gitlab,
    github, etc.).

  2. If we want to present them in a more user-friendly way, these are
    some possible options:

    2.1 A plain web page or discourse page.

    • Advantage: simplest solution
    • Disadvantage:
      • to run examples you have to cut and paste to some playground or your Prolog installation;
      • difficult to support minor differences among Prologs.

    2.2 A web or discourse page with links that load the examples in
    different Prolog systems (SWI, Ciao, XSB, etc.).

    • Advantage:
      • can run examples,
      • only one copy of the page needed,
      • can support slightly different code for each Prolog if needed.
    • Disadvantage: examples do not run within the page.

    2.3 Notebooks with runnable examplesSWI :gear: Ciao :gear: XSB :gear:

    • Advantage: examples run within the page.
    • Disadvantage:
      • will need to develop a different page for each Prolog supported;
      • also, not all Prologs have this functionality.
  • Other issues:

    • Ease of editing.
    • Privacy.
    • Need to set up a server or not.

This is a links version of the example page: contains the examples with links to execution in several Prologs.

An example:

Consider the factorial example, using Peano arithmetic, that we saw in the first part of the course:

factorial(0,s(0)).
factorial(s(N),F) :-
    factorial(N,F1),
    times(s(N),F1,F).

This version is very attractive:

  • It is fully reversible!
  • But also inefficient…

However, we can also code it using ISO-Prolog arithmetic, i.e., is/2:

 ... Z is X * Y ... 

Note that this type of arithmetic has limitations: it only works in
one direction, i.e., X and Y must be bound to
arithmetic terms.

But it provides a (large!) performance gain. Also, meta-logical tests
(see later) allow using it in more modes.

The factorial program can be encoded using is/2 as follows:

SWI :gear: Ciao :gear: XSB :gear: … (i.e., here we would put links to run in different Prologs,
maybe with slightly different code)

:- module(_,_). 

factorial(0,1). 
factorial(N,F) :-
    N > 0,
    N1 is N-1,
    factorial(N1,F1),
    F is F1*N.

% Try it:
% ?- factorial(20,F).
%
% Now it works even for very large numbers: 
% ?- factorial(100,F).
%
% But it does not run "backwards":
%
% ?- factorial(N,40320).

Note that wrong goal order can raise an error (e.g., moving the last
call to is/2 before the call to factorial). We can solve this using constraints:

SWI :gear: Ciao :gear: XSB :gear:

:- module(_,_).
:- use_package(clpq).

factorial(0,1). 
factorial(N,F) :-
    N .>. 0,
    N1 .=. N-1,
    factorial(N1,F1),
    F .=. F1*N.

% And now it runs in both directions, and more efficiently than Peano: 
% 
% ?- factorial(8,F).
% 
% ?- factorial(N,40320).

This is a plain version of the example page: contains just the examples, with no links or executable cells.

A factorial example:

Consider the factorial example, using Peano arithmetic, that we saw in the first part of the course:

factorial(0,s(0)).
factorial(s(N),F) :-
    factorial(N,F1),
    times(s(N),F1,F).

This version is very attractive:

  • It is fully reversible!
  • But also inefficient…

However, we can also code it using ISO-Prolog arithmetic, i.e., is/2:

 ... Z is X * Y ... 

Note that this type of arithmetic has limitations: it only works in
one direction, i.e., X and Y must be bound to
arithmetic terms.

But it provides a (large!) performance gain. Also, meta-logical tests
(see later) allow using it in more modes.

The factorial program can be encoded using is/2 as follows:

:- module(_,_).

factorial(0,1). 
factorial(N,F) :-
    N > 0,
    N1 is N-1,
    factorial(N1,F1),
    F is F1*N.

Try it:

?- factorial(20,F).

Now it works even for very large numbers:

?- factorial(100,F).

But it does not run “backwards”:

?- factorial(N,40320).

Note that wrong goal order can raise an error (e.g., moving the last
call to is/2 before the call to factorial). We can solve this using constraints:

:- module(_,_).
:- use_package(clpq).

factorial(0,1). 
factorial(N,F) :-
    N .>. 0,
    N1 .=. N-1,
    factorial(N1,F1),
    F .=. F1*N.

And now it runs in both directions, and more efficiently than Peano:

?- factorial(8,F).
?- factorial(N,40320).

Possibly one additional data point to consider is the work by Peter Flach and Kacper Sokol for their publishing of “Simply Logical”. Citing Simply Logical — Simply Logical

The online edition of the book is hosted on GitHub Pages. It is built with Jupyter Book and SWISH, the online version of SWI-Prolog. It follows the structure of the original print version, with minor corrections and additions as explained in the Preface to the online edition. A brief description of how the online edition came to be is posted on arXiv.

This looks like a really professional way to create educational content. While it currently uses SWISH as backend, I suspect it should be fairly easy to offer multiple backends. I have only been (very) remotely involved in this work though and I do not know the technical details of the backend integration. Merely ensured it was up :slight_smile:

Hi Jan, yes, indeed Sphinx is a good solution and there are more of course (e.g., the related jupyter notebooks integration with SICStus Prolog, etc.). I think a first objective in our context, as Prolog Education Group, is to simply agree on a means through which anyone in the group can easily provide examples and materials, in a way that can be easily stored and modified, and commented on, and that is as system-agnostic as possible. We are using already this discourse and gitlab/github repos for the web site. The idea would be to use those and, from there, as you mention, it is easy to generate materials in different formats. Regarding books, in the case of the Flach/Sokol book the hard work is already done: the examples are already in Prolog, so it is relatively easy to generate versions using other Prologs. For other books where the material is not already in Prolog or is not in suitable form this has to be done.