#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#include <stdbool.h>
enum lambda_which {lambda_ident, lambda_abs, lambda_app};
struct lambda {
struct lambda *make_ident(char *);
struct lambda *make_abstraction(char *, struct lambda *);
struct lambda *make_application(struct lambda *, struct lambda *);
+struct lambda *make_numeral(unsigned int i);
+struct lambda *make_bool(bool b);
void decls_free();
void decls_print();
#define YYSTYPE struct lambda *
\. return DOT;
\( return OBRACE;
\) return CBRACE;
+true yylval = make_bool(true); return LITERAL;
+false yylval = make_bool(false); return LITERAL;
+[0-9]+ yylval = make_numeral(atoi(yytext)); return LITERAL;
[a-z]+ yylval = make_ident(yytext); return IDENT;
[A-Z]+ yylval = make_ident(yytext); return FUNC;
return r;
}
+struct lambda *make_numeral(unsigned int i)
+{
+ struct lambda *body = make_ident("x");
+ while(i-- > 0)
+ body = make_application(make_ident("f"), body);
+ return make_abstraction("f", make_abstraction("x", body));
+}
+
+struct lambda *make_bool(bool b)
+{
+ return b
+ ? make_abstraction("a", make_abstraction("b", make_ident("a")))
+ : make_abstraction("a", make_abstraction("b", make_ident("b")));
+}
+
void decls_prepend(char *ident, struct lambda *value)
{
struct decllist *head = malloc(sizeof (struct decllist));
%}
-%token LAMBDA DOT OBRACE CBRACE IDENT FUNC SEMICOLON ASSIGN
+%token LAMBDA DOT OBRACE CBRACE IDENT FUNC SEMICOLON ASSIGN LITERAL
%%
| appterm
{ $$ = $1; }
appterm
- : FUNC
+ : LITERAL
+ { $$ = $1; }
+ | FUNC
{
$$ = decls_lookup($1->data.identifier.ident);
lambda_free($1);
SOMEGA=\x.x x;
OMEGA=SOMEGA SOMEGA;
Y=\g.(\x.g (x x))(\x.g (x x));
+
ZERO=\f x.x;
SUCC=\n f x.f(n f x);
PLUS=\m n.m SUCC n;
-MULT=\m n f x.m(n f)x;
+MULT=\m n.m (PLUS n) 0;
+EXP=\b e.e b;
+
+PRED=\n f x.n(\g h.h(g f))(\u.x)(\u.u);
+SUB=\m n.n PRED m;
+
+ISZERO=\n.n(\x.false)true;
+LEQ=\m n.ISZERO (MINUS m n);
+
+AND=\p q.p q p;
+OR=\p.p p;
+
t1 = t->data.application.expr1;
t2 = t->data.application.expr2;
lambda_reduce(t1, total, maxdepth);
+ //For applicative order
+ lambda_reduce(t2, total, maxdepth);
if(t1->which == lambda_abs){
lambda_print(total, t);
printf("β -> ");
(*maxdepth)--;
lambda_reduce(t, total, maxdepth);
}
+ //For applicative order
+ } else if(t->which == lambda_abs) {
+ lambda_reduce(t->data.abstraction.expr, total, maxdepth);
}
}