1. PROLOG AND KM2
1.1. MACROS and KM2
KM2 is a powerful macro-processor which allows to generate automaticaly files written hierarchically.Today, KM2 uses the XMind format, but will use other sources in the future.
Programms and text in KM2 are described by Xmind trees. The processor uses a depth first walking routine to generate documents in several languages : python, prolog, html, C, java, javascript, etc.
Macros allow to gain in
- readability The writing tool allows to write hierarchically all problems and solutions, making it most suitable for many applications.
- shortness of expression macros process easily repetitive tasks and elements.
- clarity of experession Graphics can be embedded in source code
- back processing : YDeTO You Describe Things Once .
MACROS are widely used in KM2 to generate :
- documentation textes in html can be easily generated
- programming
More information about the graphic and non linear writing style
1.2. Standard macros
Standard macros are available in all KM2 languages.Examples | source code | generated code |
---|---|---|
Example of use |
in Prolog
This is the standard way to write programms in KM2, in any language.
in Python source code |
|
Definition of standard macros |
|
1.3. Presentation of MACROS in PROLOG
As a programming language, prolog is very interesting
However writing and reading prolog programms is not so easy The linear structure of the editor and programming style do not allow easily to see the deep structure of programs. |
A set of KM2 MACROS in prolog allow to write much more easily all kind of programs
|
NOTE : Programming with trees , icons and drawings require some practice, but at the end, it is much more easy to produce and maintain programms. KM2 some possibilities with macros in prolog
2. KM2 some possibilities with macros in prolog
Warning In the chapter below, some Prolog clauses are written without comas, for easiness of writing.We have equivalent formulations :
- solve_and_show_solutions :- problem (P) solution (P, S) write (solution (P, S)) nl fail
- solve_and_show_solutions :- problem (P), solution (P, S), write (solution (P, S)) , nl, fail.
KM2 generates commas and points as needed during the generation, as well as indentation, comments, etc
2.1. General use of KM2
2.2. shorten some writing
Some examples below from a general mechanism that applies to all kinds of programms and documents.2.2.1. The first way is to have more tree_like programming
which allows to present better programms. Just compare
Expected result
(This is a screen copy) |
Source in KM2
|
source in Prolog
|
This example might seems more verbose in KM2, but :
-
it contains much more information than just the source :
structure of the programm example of test - it is more readable and more easy to understand and read
- it shows that all functions have been tested and validated.
2.2.2. one example is the ALL and FIRST macros
The ALL macro solves a recuring annoying issue when programming in prolog : generate all solutions one by one to a predicate, then do something with the results. Finally this should succeeed.The classic formula in Prolog looks like this solve_and_show_solutions :- problem (P) solution (P, S) write (solution (P, S)) nl
The above predicate will solve one by one all the instances of P and solutions S, and then print them.
Suppose now we want to show all solutions
One way is to rewrite the predicate :
- solve_and_show_solutions :- problem (P) solution (P, S) write (solution (P, S)) nl fail
- solve_and_show_solutions.
- solve_and_show_solutions :- ALL problem (P) solution (P, S) write (solution (P, S))
Which is obviously more simple
The other alternative is to use the repeat predicate.
But the fail at the end of the repeat terms is not really beautiful |
The macro ALL can be written in 2 different ways :
|
The FIRST MACRO will succeed at most once.
Definition of the macro FIRST |
Examples |
An exemple
Standard Prolog
|
with KM2
The scope of the ALL is easy to understand |
2.3. Easy trace of anything
The idea is that you can change, just with a macro, a comment into a print2.3.1. Change a comment to a print statement
This is very easy, just by changing the icon on the statement :Usually comments are ignored in programms, but here the comment wil be printed at run time. And this can be easily suppressed later.
It is easy to choose to have comments also printed on the backtracking phase.
2.3.2. The echo macro
The "echo" macro is useful in trace.In many programs we want to insert something like in a predicate: write ("k = ") write (K) nl
Here we just write : echo K
The trick is just to define a macro : echo ::= write ("{a1} =") write ( {a1} ) nl
now we can compare the process with a simple function :
Prolog 'standard' source
- fact (K, 1) :- k < 2 ! fail
-
fact (K, N) :-
- k >= 2,
- K1 is K -1,
- fact (K1, N1),
- N is N1 * K.
enhanced source with inside trace
3rd phase : the program is working without trace
We see the power of using icons while programming : we can change the behaviour of the programm very easily. KM2 - PROLOG MACROS REFERENCE GUIDE
3. KM2 - PROLOG MACROS REFERENCE GUIDE
NOTE MACROS are defined in the same file under the "notation prolog" topic3.1. Macros for lists and automatic macros
3.1.1. Prolog 2 notation inside swi-prolog
This notation was added to ease the writing and reading of source code.Base | equivalent to |
---|---|
X . Y .. L | [X, Y | L] |
X . Y | [X, Y] |
3.1.2. White spaces
in Prolog, the designation of a term is in the form "f (X, ...)".
The term "f (X, ...)" will generate an error, just because of the 'space' between the 'f' and the '('.
The KM2 preprocessor supppresses all leading whites.
NAME | Purpose | Appearance | Result |
---|---|---|---|
remove excedent spaces for functions |
in prolog, "f (x)" is correct, but "f (x)" is not
The purpose of this macro is to remove all 'spaces' between function and first parenthesis of argumenst |
|
|
3.2. Macros for defining functions
NAME | Purpose | Syntax | Source | Result |
---|---|---|---|---|
define a database |
use a fully tabular description for tabled data
NOTE : an alternative in swi-prolog is
|
© DEF_DB {pred_name}
|
|
|
define a just regular predicate | simplify and standardize the expression of predicates |
© DEF {pred_name}, {arguments}
|
|
|
define a predicate with inputs and outputs identified | simplify and standardize the expression of predicates |
© DEF_IO {pred_name}, {arguments}
|
The order of columns is not important :
|
|
3.3. Control macros
NAME | Purpose | Syntax | Source | Result |
---|---|---|---|---|
DOC |
Define a simple and more readable way to define herarchical text.
Also, for the source in Xmind, graphics can be added, but which will not appear in the prolog file. |
© DOC
|
with images in the source !
|
|
IF |
readable and stackable format of IF
there are 2 forms of 'IF' depending if the condition is placed just after the 'IF' or is a subtree in itself |
© IF {Condition}
© IF
|
|
|
OR | readable and stackable format of OR |
© OR
|
|
|
NOT | NOT of a sequence | © NOT {tree} |
|
|
ALL : all answers and succeed |
In prolog, you need frequently to write functions to process all elements of a list or solutions to an equation.
And at the end you want the predicate to succeed
For exemple
Now you can just use
|
|
|
3.4. Trace and debug macros
3.4.1. Definitions
NAME | Purpose | Appearance | Result |
---|---|---|---|
trace comment | display the comment while the prolog processor runs on it |
mnemotechnics : the comment will just fly to the user |
write ('this is a comment") |
trace comment and trace backtrack | display the comment while the prolog processor runs on it |
mnemotechnics : the comment will return the call to the user |
(
|
echo value |
echoes the term on the terminal
equivalent to write (" |
echo foo (X) |
|
3.4.2. Full examples of trace macros
In KM2 , just by changing (one click) the icon on a node allows to change the generated code :Case | source code | generated code | execution |
---|---|---|---|
trace all |
|
|
|
trace nothing |
|
|
|