The K programming language.



SourceForge.net Logo Copyright © 2006 Fabrice Marchant, GPL. 

Original project page

Notes :
Maybe must change "K" name to "didah" ( or something else ) because of Whitney K programming language.

This is only a draft : the definition of this language can evolve when interpreter experimentations make ideas to become more accurate .

There are lot of forward references in this doc.


         Table of contents


0                                                       Meta

1                                                     Forms
1.1                                          Characters
1.2                                            List forms
1.3                                                 Strings

2         Keywords and predefined things
2.1                                         Parenthesis
2.2                                                   Quote
2.3                                                  Define
2.4                                              Dit forms
2.5                                            Dah forms
2.6                                                     Read
2.7                                                     Print
2.8                                            Comments
2.9                                              Packages

3                                               Evaluation
3.1                                               Overload

4                                                 Examples

5                                         An interpreter


0 Meta
"->" here means : "evaluates to".
"->" is not part of the language.


1 Forms
A form is a character of a list of forms.

Input forms can be split in several lines. When the top level closing parenthesis is met, evaluation proceeds.


1.1 Characters
What follows the symbol ` is taken as a character.

`a     ; The "a" character.
`\t    ; The tabulation.

Characters can be written in using C language escaped notation - using "\" - \t for tab, \xA9 for hexadecimal definition etc...

Characters cannot be redefined. Means :
( : '* star ) -> ( : '* star )    ; Can't do anything. Then return as is.

( We prevent redefinition of characters to speed evaluation up a bit and to avoid things become unnecessarily scrambled. )


1.2 List forms
A list : like in Lisp, between parenthesis.
It is a linear thing, not the special case of a binary tree.

(a () .o °+° (# 0) `*)

When parts of a list form must be evaluated, this is done from left to right.
All elements are evaluated, even the first one.


1.3 Strings
A string is a list form whose only elements are characters.
Strings are nothing special for the language.
There are provided for input / output convenience.
However they continue to exist as a special kind of List form in the
interpreter : as all elements of it are unredefinable chars, there is no need to
continue the evaluation inside it.

It can be useful to surround a string with "\"" if it contains white spaces.
It's possible to use \ <- ASCII 32, like in UNIX bash.

(`s `t `r `i `n `g) -> string
Hi! -> Hi!    ; When "Hi!" is entered, the language works with (`H `i `!).
"Hello World !" -> "Hello World !"
Hello\ World\ ! -> "Hello World !"
qwer\"ty        -> qwer"ty    ; A " inside a string
1#^+° -> 1#^+° ; This is a valid string


2 Keywords and predefined things
  The language owns very few predefined things.
( No numbers. Not even an "if" nor a "cons" or a "car". )
  However it has few to do with funny obfuscated languages like brainfuck, redcode or (worse) unlambda :
  it is intended to define clear and readable programs.


2.1 The parenthesis

The opening parenthesis "(" and
the closing parenthesis ")" delimit a list.
The parenthesis do not need to be surrounded with blank :
cannot glue with other characters unless writing \( or `( or inside a double quoted string.


2.2 Quote
(quote form) or 'form prevents "form" to be evaluated.
quote and its shortcut acts like in Lisp.

(: a b)    -> b    ; Defines a to b
(:)    -> ((a b))
(: a c) -> c    ; Defines b to c
(:) -> ((a b) (b c))
(: 'a d) -> d    ; Or (: (quote a) d)
(:)
(:) -> ((a d) (b c))

When a definition is instanciated quotes apply to first replaced did forms.

(: (= .var .val) (: '.var .val))
(= weight 90kg)
weight -> 90kg
(= weight 0.11t)
weight -> 0.11t

(: v (a b))
(: (1st (.x .y)) .x)
v -> (a b)
(= (1st v) Zorro)
v -> (Zorro b)


2.3 Define
":" in first place of a list form is a kind of "define" : a link that binds the first form to another.
Any form else than a car can be defined, not only strings.

Three ways to use ":"

; Define a form to another one
(: phi (/ (+ 1
                  (sqrt 5)
             )
             2
          )
)

(: (car (. _)) . )

(: (& 0 .) 0)        ; Defining boolean "and"
(: (& 1 .) .)
(: 'phi fi) ; Redefines phi to fi.
; Ask for the list of definition couples.
(:) -> ((phi fi)
          ((car (. _)) .)
          ((& 0 .) 0)
          ((& 1 .) .)
         )

; Defines to nothing : undefines.
(: 'phi)
(:) -> (((car (. _)) .)
          ((& 0 .) 0)
          ((& 1 .) .)
         )


2.4 Dit forms
Forms that begins with "." are variables, function parameters.
These variables are bound between the two definition parts.
They can be instanciated later.

(: (swap .x .y) (.y .x))
(swap [ ]) -> (] [)    ; .x is instanciated by [, .y by ].

; ., .., ... are valid dits.

(: (cons (.) (..)) (. ..))
(cons a b) -> ab
(cons a (*)) -> (`a *)


2.5 Dah forms
Forms that begins with "_" mean : any - maybe null - number of parameters.

(: (cdr (. _) (_))
(cdr (() * (°)) -> (* (°))
(cdr abc) -> bc

(: (append (_1) (_2)) (_1 _2))
(append abc (())) -> (`a `b `c ())
(append abc de) -> abcde



2.6 Read
Read a character from the input.

(: 'ch read)


2.7 Print

Prints a form.

(print  +---+---+---+\n)
(print "+   +   +   +\n")

2.8 Comments
A semicolon ";" indicates a comment.
It extends to the end of the line.

(: (Id .) .)    ; The identity function.


2.9    Packages

(package (d1 (d3 .))    ; Only these two forms 'll be seen outside the package.

    (: d1 (id rec maybe))
    (: (id .) .)    
    (: maybe ?)
    (: d2 Pi)
    (+ k( x ) 18)
    (append Jo hnny)
    (: (d3 word))
)

Outside visible forms could be compiled, i.e. bound to their ... inside the
package evaluation.


3 Evaluation
When a form is input, it can maybe be evaluated, according of previous
defined things.
If it doesn't match anything known, it evaluates to itself.


3.1 Overload

A quoted form is returned as is.


4 Examples :

(: (rol (.x .y .z)) (.y .z .x))
(: t1 (q () wer))
(rol t1) -> (() wer q)


5    An interpreter :
"k" accepts a list of file names as input parameters.
It loads and evaluates them before to give the hand to a command line input.
(load filename)
All parsable input of the line is evaluated. That remains, if any, is
returned to the input line to be completed or corrected.
This console interface relies on readline ( acts like the bash, no
completion however. )


Copyright © 2006 Fabrice Marchant <ecirbaf at users.sourceforge.net>