Simple typed lambda calculus with recursion in python
Use pip to install it from the repository (stlc package is not available in pypy yet).
pip install git+https://github.com/Luis-omega/STLC.git
After installation you have available the command stlc
To try a single string:
stlc -i "a=b;"
To try a full file:
stlc -f filename
expression : variable
| literal
| "(" expression expression ")"
|"\" variable "->" expression
| "(" expression operator expression ")"
| "if" expression "then" expression "else" expression
variable_character : "a" | ... | "z" | "A" | ... | "Z"
variable : "_" variable_character+ | variable_character+
literal : bool_literal | int_literal | "unit"
bool_literal : True | False
int_literal : 0 | 1 | -1 | 2 | -2 | ...
operator: "+" | "-" | "*" | "/" | "<" | ">" | "<=" | ">=" | "==" | "&" | "|" |"~"
type : "Bool" | "Int" | "Unit" | "(" type "->" type ")"
variable_definition : variable "=" expression ";"
variable_declaration : variable ":" type ";"
expression : variable
| literal
| "(" expression expression_paren
|"\" variable "->" expression
| "if" expression "then" expression "else" expression
expression_paren : expression ")"
| operator expression ")"
| ":" type ")"
We use variable:type
declarations .
-
$x[x := r] = r$ -
$y[x := r] = y$ if$x \neq y$ -
$(e_1 \quad e_2)[x:=r] = ((e_1[ x:= r])(e_2[x:=r]))$ -
$(\textbackslash x -> e)[x:=r] = (\textbackslash x->e)$ -
$(\textbackslash y -> e)[x:=r] = (\textbackslash y -> (e[y:=z])[x:=r])$ provided$x \neq y$ and$z \notin Free(e)$ -
$(e_1 \quad op \quad e_2)[x:=r] = ((e_1[x:=r]) \quad op \quad (e_2[x:=r]))$ -
$(if \quad e_1 \quad then \quad e_2 \quad else \quad e_3)[x:=r] = if e_1[x:=r] then e_2[x:=r] else e_3[x:=r]$
Let variable = expression
.
True,False,unit,0,1,-1,-2,...
, lambda expressions of the form \ x -> e
and operator expressions v_1 op v_2
where