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 :
Some advantages :

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 !

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'
SOURCE IN KM2



Generated by KM2 :

Python code :


Example of factorial 2, with documentation
HERE we use the blue information icon to indicate a comment line
SOURCE IN KM2



Generated by KM2 :

Python code :


Example of factorial 3
Here we use the green pen icon to indique that we have a succession of comment lines.
SOURCE IN KM2



Generated by KM2 :

Python code :


Example of factorial 4
Here we add the "DESCRIPTION" MACRO, which generates the doc string in the traditional python format.
SOURCE IN KM2



Generated by KM2 :

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.
SOURCE IN KM2



Generated by KM2 :

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 ...'
SOURCE IN KM2



Generated by KM2 :

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
The Ackermann function is defined as :

Programming in KM2
SOURCE IN KM2



Generated by 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 :
  • WHERE
  • delayed =
SOURCE IN KM2



Generated by KM2 :

#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
SOURCE IN KM2




Generated by KM2 :

#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 :
SOURCE IN KM2



Generated by KM2 :

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 IN KM2

Source

Generated by KM2 :

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 "="
SOURCE IN KM2



Generated by KM2 :


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
SOURCE IN KM2



Generated by KM2 :

wrong code
if x == '==' :
    y = 3

The best is to use a variable
SOURCE IN KM2



Generated by KM2 :

equ = '='
if x == equ :
    y = 3
This could be corrected in a near future.

4. PYTHON SPECIFICS

Some macros have been hard-coded in the KM2 processor (the KM2 processor is written in python and bootstrapped).





...