11use crate :: lambda_parse:: { parse_lambda, LambdaExpression } ;
22use std:: rc:: Rc ;
33use uuid:: Uuid ;
4- use log:: { debug, trace, info } ;
4+ use log:: { debug, trace} ;
55use colored:: * ;
66
77pub struct VM {
@@ -83,7 +83,7 @@ impl VM {
8383 debug ! ( "{}IsZero: {}" , " " . repeat( depth) , expr. to_string( ) ) ;
8484 let eval_inner = self . eval_recursive ( inner, depth + 1 ) ;
8585 match & * eval_inner {
86- LambdaExpression :: Abstraction { parameter : f , body } => {
86+ LambdaExpression :: Abstraction { parameter : _f , body } => {
8787 match & * * body {
8888 LambdaExpression :: Abstraction { parameter : x, body : inner_body } => {
8989 if * * inner_body == LambdaExpression :: Variable ( x. clone ( ) ) {
@@ -157,7 +157,7 @@ impl VM {
157157 let eval_left = self . eval_recursive ( left, depth + 1 ) ;
158158 let eval_right = self . eval_recursive ( right, depth + 1 ) ;
159159 match ( & * eval_left, & * eval_right) {
160- ( LambdaExpression :: Number ( n1 ) , LambdaExpression :: Number ( n2 ) ) => {
160+ ( LambdaExpression :: Number ( _n1 ) , LambdaExpression :: Number ( _n2 ) ) => {
161161 // Implement multiplication for your number representation
162162 // This is a placeholder and needs to be implemented correctly
163163 Rc :: new ( LambdaExpression :: Number ( Rc :: new ( LambdaExpression :: Number (
@@ -240,12 +240,6 @@ impl VM {
240240 debug ! ( "{}YCombinator: {}" , " " . repeat( depth) , expr. to_string( ) ) ;
241241 self . eval_y_combinator ( f, depth)
242242 }
243-
244- // show error and not support yet
245- _ => {
246- debug ! ( "{}Unsupported expression: {}" , " " . repeat( depth) , expr. to_string( ) ) ;
247- Rc :: new ( expr. clone ( ) )
248- }
249243 } ;
250244
251245 if * result == * expr {
@@ -281,7 +275,7 @@ impl VM {
281275 var : & str ,
282276 replacement : & LambdaExpression ,
283277 ) -> LambdaExpression {
284- let result = match expr {
278+ match expr {
285279 LambdaExpression :: Variable ( name) if name == var => replacement. clone ( ) ,
286280 LambdaExpression :: Variable ( _) => expr. clone ( ) ,
287281 LambdaExpression :: Number ( _) => expr. clone ( ) ,
@@ -350,8 +344,7 @@ impl VM {
350344 LambdaExpression :: YCombinator ( inner) => {
351345 LambdaExpression :: YCombinator ( Rc :: new ( self . substitute ( inner, var, replacement) ) )
352346 }
353- } ;
354- result
347+ }
355348 }
356349
357350 fn alpha_convert ( & self , param : & str , body : & LambdaExpression ) -> ( String , LambdaExpression ) {
@@ -360,6 +353,7 @@ impl VM {
360353 ( new_param, new_body)
361354 }
362355
356+ #[ allow( clippy:: only_used_in_recursion) ]
363357 fn occurs_free ( & self , var : & str , expr : & LambdaExpression ) -> bool {
364358 match expr {
365359 LambdaExpression :: Variable ( name) => name == var,
@@ -402,6 +396,12 @@ impl VM {
402396 }
403397}
404398
399+ impl Default for VM {
400+ fn default ( ) -> Self {
401+ Self :: new ( )
402+ }
403+ }
404+
405405pub fn church_encode ( n : u64 ) -> LambdaExpression {
406406 let body = ( 0 ..n) . fold ( LambdaExpression :: Variable ( "x" . to_string ( ) ) , |acc, _| {
407407 LambdaExpression :: Application {
@@ -421,18 +421,17 @@ pub fn church_encode(n: u64) -> LambdaExpression {
421421pub fn church_decode ( expr : & LambdaExpression ) -> Result < u64 , String > {
422422 fn count_applications ( expr : & LambdaExpression ) -> u64 {
423423 match expr {
424- LambdaExpression :: Application { function, argument } => {
424+ LambdaExpression :: Application { function : _ , argument } => {
425425 1 + count_applications ( argument)
426426 }
427- LambdaExpression :: Variable ( _) => 0 ,
428427 _ => 0 ,
429428 }
430429 }
431430
432431 match expr {
433- LambdaExpression :: Abstraction { parameter : f , body } => match & * * body {
432+ LambdaExpression :: Abstraction { parameter : _f , body } => match & * * body {
434433 LambdaExpression :: Abstraction {
435- parameter : x ,
434+ parameter : _x ,
436435 body : inner_body,
437436 } => Ok ( count_applications ( inner_body) ) ,
438437 _ => Err ( format ! (
@@ -478,28 +477,16 @@ mod tests {
478477 use super :: * ;
479478 use crate :: lambda_parse:: parse_lambda;
480479
480+ // check expr === value return boolean
481481 fn is_church_numeral ( expr : & LambdaExpression , value : u64 ) -> bool {
482482 match expr {
483- LambdaExpression :: Abstraction { parameter : f, body } => match & * * body {
484- LambdaExpression :: Abstraction {
485- parameter : x,
486- body : inner_body,
487- } => {
488- let mut current = inner_body;
489- let mut count = 0 ;
490- while let LambdaExpression :: Application { function, argument } = & * * current {
491- if !matches ! ( & * * function, LambdaExpression :: Variable ( name) if name == f) {
492- return false ;
493- }
494- current = argument;
495- count += 1 ;
496- }
497- matches ! ( & * * current, LambdaExpression :: Variable ( name) if name == x)
498- && count == value
499- }
500- _ => false ,
483+ LambdaExpression :: Abstraction { parameter : _f, body } => match & * * body {
484+ LambdaExpression :: Abstraction { parameter : _x, body : _inner_body } => {
485+ church_decode ( expr) . map_or ( false , |n| n == value)
486+ } ,
487+ _ => false
501488 } ,
502- _ => false ,
489+ _ => false
503490 }
504491 }
505492
0 commit comments