Theory and Design of PL (CS 538)
January 31, 2020
-Wall
before submitting
hlint
before submitting
Will grade solutions for given functions
0
, true
), operations (+
, -
, *
)x
)if
, then
, else
, let
, where
)2*x+1
)if b then 3 else 4
)"..."
or '...'
|
means ordigit-0-to-4 = "0" | "1" | "2" | "3" | "4" ;
digit-5-to-9 = "5" | "6" | "7" | "8" | "9" ;
digit = digit-0-to-4 | digit-5-to-9 ;
num = digit { digit }
signed-num = [ "-" ] num
Begin with Boolean constants:
bool-cons = "true" | "false" ; (* constants *)
Then add logical combinations:
bool-expr = bool-cons (* constants *)
| "!" bool-expr (* negation *)
| "(" bool-expr ")" (* paren term *)
| bool-expr "==" bool-expr (* equals *)
| bool-expr "&&" bool-expr (* and *)
| bool-expr "||" bool-expr ; (* or *)
Integers and arithmetic operations
num-expr = signed-num (* constants *)
| "-" num-expr (* negate *)
| "(" num-expr ")" (* paren term *)
| num-expr "+" num-expr (* add *)
| num-expr "-" num-expr (* minus *)
| num-expr "*" num-expr ; (* multiply *)
Begin with variable names and constants:
var = "x" | "y" | "z" | ... ;
expr = var (* variables *)
| bool-cons | num-cons (* base const *)
| "(" expr ")" ; (* paren expr *)
expr = var (* variables *)
| bool-cons | num-cons (* base const *)
| "(" expr ")" (* paren expr *)
| "λ" var "." expr ; (* functions *)
Functions have input variable, body expression
expr = var (* variables *)
| bool-cons | num-cons (* base const *)
| "(" expr ")" (* paren expr *)
| "λ" var "." expr (* functions *)
| expr " " expr ; (* application *)
Call function with argument by separating with space
Adding in some Boolean operations…
expr = ...
| expr "==" expr
| expr "&&" expr
| expr "||" expr
| "!" expr ;
…and some other operations
expr = ...
| expr "+" expr
| expr "*" expr
| "-" expr
| "if" expr "then" expr "else" expr ;
1+2*3
is (1+2)*3
? or 1+(2*3)
?Code is more than a just list of characters
We will mostly work with abstract syntax