1. Python with KM2 : advantages
PYTHON is a well known language.The KM2 processor allows to write more structured and efficient programs in python
One of the important features of KM2 is the generation of text/documentation (html or latex files), and this can be done in very coherent manner with programming.
For documentation generation :
- In many technical papers, a lot of tables are written, while many of them contain the same information/labels. For exemple, for a project, we need many tables describing either the planning, or the inputs and outputs, etc each starting with the name of the WorkPackage (WP).
- Then most of the information and knowledge is split over multiple pages in a document, difficult to read, but also to maintain.
- For example, adding a WP in many tables is difficult, at least really tedious !
- KM2 allows to define a table, that contains all the project data, and then to define what to extract for each sub-table, one by one, thus preventing mistakes.
- This allows to separate the content of the project, from it's formatted desired output. Once the project has sufficient information, each table is just one-line of SGH scheme.
Some advantages :
- Better use of the screen width programs are usually more than 50% shorter in height, so long programs can fit into a single screen.
- A very important feature is the definition of macros. Macros allow to improve on readability
2. BASIC PYTHON EXAMPLES
2.1. The factorial function
The factorial function is well known as
However in python we cannot use the '!' as a function name, so we use fact (n) as n !
- n is an integer ≥ 0
- 0 ! = 1
- n! = n * (n -1)!
At first glance, python is not changed very much when writing code with trees : ?
Example of factorial
Here we use the 'up left arrow ' icon to indique a 'return'
|
|
Python code : |
Example of factorial 2, with documentation
HERE we use the blue information icon to indicate a comment line
|
|
Python code : |
Example of factorial 3
Here we use the green pen icon to indique that we have a succession of comment lines.
|
|
Python code : |
Example of factorial 4
Here we add the "DESCRIPTION" MACRO, which generates the doc string in the traditional python format.
|
|
Python code : |
Example of factorial 5
Here we now embed graphics in the source, which allows easier understanding.
Also, we use icons to show that the tests are done or ongoing. And we prove it.
Also, we use icons to show that the tests are done or ongoing. And we prove it.
|
|
Python code : |
KM2 considers that readability and aesthetics of programms is primordial. KM2 brings easiness of writing, correction, easier maintenance, etc |
3. SPECIFIC BUILT-IN MACROS AVAILABLE IN ALL LANGUAGES
3.1. CASE
The CASE macro allows to write much easier a series of 'if then else ...'
|
|
if cond1 : # list of instructions 1 pass elif cond_2 : # list of instructions 1 pass #some instructions pass if cond_3 : # instructions 3 pass elif cond_4 : # instructions 4 pass else : # list of instructions |
Example : The Ackermann function
Programming in KM2
|
|
def Ackermann (m, n) : if m == 0 : return n + 1 elif n == 0 : return Ackermann (m -1, 1) else : return Ackermann (m-1, Ackermann (m, n-1)) |
Programming in KM2 with WHERE and forward '='
Here, we could like to print the value of Ackerman before the return
We use 2 features :
We use 2 features :
- WHERE
- delayed =
|
|
#another version of the Ackermann function def Ackermann_2 (m, n) : if m == 0 : z = n + 1 elif n == 0 : z = Ackermann_2 (m -1, 1) else : z = Ackermann_2 (m-1, Ackermann_2 (m, n-1)) return z |
The CASE MACRO provides readability and precision. It allows also to check easily the various cases, and their order. The CASE macro contains a THEN keyword that interrupts the list of 'if's. This is most useful when some checks must be done before the array of tests will be run. |
3.2. CASE ALL
The CASE ALL macro wil run all tests.The final 'else' is run only if none of the conditions was satisfied
Example
|
CASE_ALL_15 = False if test1 : instructions1 () CASE_ALL_15 = True if test2 : instructions2 () CASE_ALL_15 = True instructions_THEN () if test3 : instructions3 () CASE_ALL_15 = True if test4 : instructions4 () CASE_ALL_15 = True if CASE_ALL_15 : instructions_else () |
CASE_ALL_16 = False if (test1) { instructions1 (); CASE_ALL_16 = True } if (test2) { instructions2 (); CASE_ALL_16 = True } instructions_THEN (); if (test3) { instructions3 (); CASE_ALL_16 = True } if (test4) { instructions4 (); CASE_ALL_16 = True } if (CASE_ALL_16) { instructions_else (); } |
3.3. CASEXY
One of the most unusual visual extensions is the CASEXY, which allows the easy and maintenable expression of embedded 'if's (see example below) , which generates 70 lines of source code :The CASEXY uses names of rows and columns as tests
|
|
#give the clock in plain text , given h and mn in numbers def clock_speak (h, mn) : if h == 12 : if mn < 10 : return 'midi' elif 10 <= mn <= 18 : return 'midi and quart' elif 25 < mn < 35 : return 'midi and demie' elif 40 < mn < 50 : return ['une heure moins le quart'] elif mn >= 51 : return ['une heure moins', 60 - mn] else : return ['midi', mn] elif h == 00 : if mn < 10 : return 'minuit' elif 10 <= mn <= 18 : return 'minuit and quart' elif 25 < mn < 35 : return 'minuit and demie' elif 40 < mn < 50 : return ['une heure moins le quart'] elif mn >= 51 : return ['une heure moins', 60 - mn] else : return ['minuit', mn] elif h == 11 : if mn < 10 : return [h, 'heure', mn ] elif 10 <= mn <= 18 : return [h, 'heure and quart'] elif 25 < mn < 35 : return [h, 'heure', mn ] elif 40 < mn < 50 : return ['midi moins le quart'] elif mn >= 51 : return ['midi moins', 60 - mn] else : return [h, 'heure', mn ] elif h == 23 : if mn < 10 : return [h, 'heure', mn ] elif 10 <= mn <= 18 : return [h, 'heure and quart'] elif 25 < mn < 35 : return [h, 'heure', mn ] elif 40 < mn < 50 : return ['minuit moins le quart'] elif mn >= 51 : return ['minuit moins', 60 - mn] else : return [h, 'heure', mn ] else : if mn < 10 : return [h, 'heure', mn ] elif 10 <= mn <= 18 : return [h, 'heure and quart'] elif 25 < mn < 35 : return [h, 'heure', mn ] elif 40 < mn < 50 : return [h+1, 'moins le quart'] elif mn >= 51 : return [h+1, 'moins', 60 - mn] else : return [h, 'heure', mn ] |
The CASEXY macro provides readability and precision for complex cases. The table presentation allows rigor in the definition of the various cases. |
3.4. WHERE
The WHERE macro is defined by the following scheme :
|
|
and replaces : X =1 Y = 2 T = 2.5 Z = 3 |
Example We could have the following sequences of steps for programming
STEP | Source | Note | Code |
---|---|---|---|
1 |
|
we just define foo, and know its result |
def foo (x, y) : # some integer z return z |
2 |
|
we provide more precisions |
|
The WHERE macro allows programming with a reversed order, which suits better the way of human thinking. The WHERE macro also allows to write programms progressively. |
3.5. DELAYED =
The delayed = is a feature that is derived from functional languages (Lisp/Scheme, etc).EXAMPLE OF GENERATED CODE
Source |
|
if y > 100 : x = 30 else : S = 0 for i in range (y) : S += i x = S + y |
The DELAYED= is sometimes a very nice feature |
3.6. The "=" in tests
Issue most of the time, a test of equality between 2 variables is written as x == y while we think in termes of x = y In KM2, the equality in if instructions is simplified to "="
|
|
if x == y : x = x + 2 else : x = x -2 |
KM2 allows more natural writing of programms. |
This may cause some issues when using the = character in other strings.
EXAMPLE
The best is to use a variable
|
4. PYTHON SPECIFICS
Some macros have been hard-coded in the KM2 processor (the KM2 processor is written in python and bootstrapped)....