91299fc4f52bcb169f9ef711ae29663cdbfc2f91
9 #define binder(t) (*t)->data.identifier.binding
10 #define lhs(t) (*t)->data.application.expr1
11 #define rhs(t) (*t)->data.application.expr2
12 #define bdy(t) (*t)->data.abstraction.expr
14 void lambda_beta(struct lambda
*binding
, struct lambda
**body
, struct lambda
*target
, struct lambda
*total
)
16 // lambda_print(*body, NULL);
17 // printf(" [%s=", binding->data.abstraction.ident);
18 // lambda_print(target, NULL);
20 switch((*body
)->which
){
22 if(binder(body
) == binding
){
29 lambda_beta(binding
, &bdy(body
), target
, total
);
32 lambda_beta(binding
, &lhs(body
), target
, total
);
33 lambda_beta(binding
, &rhs(body
), target
, total
);
38 bool lambda_reduce(struct lambda
**t
, struct lambda
**total
, bool applicative
)
40 if((*t
)->which
== lambda_app
){
42 if(lambda_reduce(&rhs(t
), total
, applicative
))
44 if(lhs(t
)->which
!= lambda_abs
){
45 return lambda_reduce(&lhs(t
), total
, applicative
);
47 if(lhs(t
)->data
.abstraction
.strict
)
48 lambda_reduce(&rhs(t
), total
, true);
51 rhs(t
) = copy(rhs(t
));
53 //In this abstraction we substitute the result with the rhs
54 lambda_print(*total
, *t
);
55 lambda_beta(lhs(t
), &lhs(t
)->data
.abstraction
.expr
, rhs(t
), *total
);
57 struct lambda
*newt
= lhs(t
)->data
.abstraction
.expr
;
58 lhs(t
)->data
.abstraction
.expr
->refcount
++;
65 } else if ((*t
)->which
== lambda_abs
) {
66 return lambda_reduce(&bdy(t
), total
, applicative
);