8 struct lambda
*copy(struct lambda
*t
)
10 struct lambda
*c
= malloc(sizeof (struct lambda
));
14 c
->data
.identifier
= strdup(t
->data
.identifier
);
17 c
->data
.abstraction
.ident
= strdup(t
->data
.abstraction
.ident
);
18 c
->data
.abstraction
.expr
= copy(t
->data
.abstraction
.expr
);
21 c
->data
.application
.expr1
= copy(t
->data
.application
.expr1
);
22 c
->data
.application
.expr2
= copy(t
->data
.application
.expr2
);
28 void subst(char *ident
, struct lambda
*t1
, struct lambda
*t2
, struct lambda
*total
)
33 if(strcmp(t1
->data
.identifier
, ident
) == 0){
35 free(t1
->data
.identifier
);
42 if(strcmp(t1
->data
.abstraction
.ident
, ident
) != 0)
43 subst(ident
, t1
->data
.abstraction
.expr
, t2
, total
);
46 subst(ident
, t1
->data
.application
.expr1
, t2
, total
);
47 subst(ident
, t1
->data
.application
.expr2
, t2
, total
);
53 void lambda_reduce(struct lambda
*t
, struct lambda
*total
, int *maxdepth
)
57 struct lambda
*t1
, *t2
;
62 lambda_reduce(t
->data
.abstraction
.expr
, total
, maxdepth
);
65 //If the first argument is an abstraction, we apply
66 t1
= t
->data
.application
.expr1
;
67 t2
= t
->data
.application
.expr2
;
68 lambda_reduce(t1
, total
, maxdepth
);
69 lambda_reduce(t2
, total
, maxdepth
);
70 if(t1
->which
== lambda_abs
){
71 subst(t1
->data
.abstraction
.ident
, t1
->data
.abstraction
.expr
, t2
, total
);
74 *total
= *t1
->data
.abstraction
.expr
;
77 lambda_reduce(t
, total
, maxdepth
);