/ This script is an example of Min, a minuscule functional language.            
/ Call min from APL by typing:                                                  
/                                                                               
/      min''                                                                    
/                                                                               
    3           / Only single digit numbers may be entered: 0-9.                
3
    123         / When we make a mistake, a rather terse error message results. 
?
    +4          / '+', next successive number, is min's sole primitive function.
5
    +(+4)       / We can use ()s to make complex expressions. In this example,  
6
    ++3         / ···  we need them as functions associate (bind) to the left.  
?
    i = 0       / =  makes a definition using only a single character name: a-z.
                                                                                
    f x = +(+x) / Functions are defined thus.                                   
                                                                                
    f(fi)       / We don't need blank spaces and in fact they are ignored.      
4
                                                                                
/ We  can  see  that min is very simple and on the face of it, immensely feeble:
/ Only  single digit numbers; only single character names; only a single primit-
/ ive function. It also takes rather a long time to evaluate any non-trivial ex-
/ pression.  However, with a sufficiently powerful computer and a fair amount of
/ patience, one can do some interesting things:                                 
                                                                                
    t f x = f(f x)     / Functions can take functions as arguments.             
                                                                                
    t + 0              / t applies its first arg twice to its second arg.       
2
    t t + 0                                                                     
4
    t t t + 0          / Remember, functions associate to the left.             
16
/   t t t t + 0        / Would eventually return 65536, but life's too short.   
                                                                                
    n 0 = 1            / Functions use _patterns_ to match specific arguments.  
    n 1 = 0            / This function implements 'not'.                        
                                                                                
    n 0                                                                         
1
    n 1                                                                         
0
    n 2                / (domain error)                                         
?
    x   0  = 0         / Sign of: (signum)                                      
    x (+n) = 1         / Patterns may be expressions.                           
                                                                                
    x 0                                                                         
0
    x 2                                                                         
1
                                                                                
/ Pattern:  i   matches     0   1   2   3   ···                                 
/         (+i)    ..            1   2   3   ···                                 
/       (+(+i))   ..                2   3   ···                                 
/               and so on.                                                      
/                                                                               
/ Equations must not "overlap". For example, the definition:                    
/           x 0 = ···                                                           
/           x i = ···                                                           
/ ··· is ambiguous because _both_ patterns match an argument of 0.              
/                                                                               
/ A  function  may  take more than one argument and an equation is selected when
/ _all_ of its patterns match the given arguments. The function body (right hand
/ side  of  the equation) is then evaluated with any occurrences of each pattern
/ variable having a value consistent with the match. For example, when equation:
/ z i (+(+j)) (+k) = ···  matches arguments of 4, 5 and 6; variables i, j, and k
/ assume values of 4, 3, 5 respectively.                                        
/                                                                               
/   Pattern:        i   (+(+j))     (+k)                                        
/   Matches:        4       5         6                                         
/   Therefore:    i=4     j=3       k=5                                         
                                                                                
    s i  0   = i               / Sum: is defined recursively        s i j ≡≡ i+j
    s i (+j) = +(s i j)        / in terms of successor and sum.                 
                                                                                
    s 2 3                      / sum of 2 and 3.                                
5
    p i  0   = 0               / Product: is defined recursively    p i j ≡≡ i×j
    p i (+j) = s (p i j) i     / in terms of sum and product.                   
                                                                                
    p 2 3                      / product of 2 and 3.                            
6
    e i  0   = 1               / Exponent is defined recursively    e i j ≡≡ i*j
    e i (+j) = p (e i j) i     / in terms of product and exponent.              
                                                                                
    e 2 3                      / 2-to-the-power-3                               
8
    d i j = s (p (+9) i) j     / Decimal decode.                 d i j ≡≡ 10⊥i j
                                                                                
    d(d12)3                    / One hundred and twenty three.                  
123
    l   0    0  = 0            / Less than:                         l i j ≡≡ i<j
    l   0  (+j) = 1                                                             
    l (+i)   0  = 0                                                             
    l (+i) (+j) = l i j                                                         
                                                                                
    l 2 3                                                                       
1
    c 1 t f = t                / Condition: t or f depending on the value of the
    c 0 t f = f                / first argument. Note: t and f may be functions.
                                                                                
    m i j = c (l i j) j i      / Maximum:                           m i j ≡≡ i⌈j
                                                                                
    m 2 3                                                                       
3
                                                                                
/ A  function applied to fewer than its defined number of arguments yields a new
/ function  of the remaining ones. This phenomenon known as "currying" after the
/ American logician Haskell Curry, was discovered by Schönfinkel in 1924 follow-
/ ing an idea by Frege in 1893:                                                 
                                                                                
    q = p 4                    / quadruple.                                     
                                                                                
    q 3                                                                         
12
    y = l 0                    / Alternative signum using less than.            
                                                                                
    y 0                                                                         
0
    y 3                                                                         
1
                                                                                
/ Note  that  as  function application associates left, currying occurs with any
/ function of more than one argument. Using sum defined above:                  
                                                                                
    s 2 3                       / sum 2 3 is interpreted as:                    
5
    (s 2) 3                     / (sum 2) 3                                     
5
                                                                                
/ We can trace the evaluation of an expression by appending a semicolon.        
                                                                                
    t+0;                                                                        
SBI+0
¯¯¯¯ 
B+(I+)0
¯¯¯¯¯¯¯
+(I+0)
  ¯¯  
+(+0)
  ¯¯ 
+1
¯¯
2
                                                                                
/ Min has two system commands:                                                  
/                                                                               
/   ~ removes definition(s) and displays remaining ones.                        
/   ) exits from the min session returning the current set of definitions.      
                                                                                
    ~                           / Display definitions.                          
i f t n x s p e d l c m q y 
    ~ifq                        / Remove: i, f, and q.                          
t n x s p e d l c m y 
    )                           / Quit.                                         


Back to: contents