From c96333038485afd78bcd15539dcba89a2d3fb328 Mon Sep 17 00:00:00 2001 From: mehbark Date: Sun, 8 Dec 2024 21:31:53 -0500 Subject: [PATCH] prepublish --- src/lib.rs | 50 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a8ad3a2..939c2fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,7 +15,7 @@ use thiserror::Error; /// A representation of a term of the /// [lambda calculus](https://en.wikipedia.org/wiki/Lambda_calculus), using /// [de Bruijn indices](https://en.wikipedia.org/wiki/De_Bruijn_index). -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum LC { /// A lambda abstraction; binds a new variable. Abs(Rc), @@ -111,7 +111,7 @@ impl fmt::Display for LC { /// A representation of a term of the /// [SKI combinator calculus](https://en.wikipedia.org/wiki/SKI_combinator_calculus). -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum SKI { /// The S (substitution) combinator. /// @@ -162,12 +162,6 @@ impl fmt::Display for SKI { } } -#[derive(Debug, PartialEq, Eq)] -pub enum Iota { - Iota, - App(Rc, Rc), -} - #[derive(Debug, Error, PartialEq, Eq)] pub enum SKICompileError { #[error("LC term has a free variable")] @@ -179,9 +173,41 @@ impl TryFrom for SKI { type Error = SKICompileError; fn try_from(term: LC) -> Result { - if term.has_free() { - return Err(SKICompileError::FreeVar); - } - todo!() + use SKI::{App, I, K, S}; + Ok(match term { + LC::Abs(x) => match &*x { + LC::Abs(y) => match &**y { + // x + LC::Var(1) => K, + LC::Abs(z) => match z { + LC::App(_, _) => todo!(), + _ => todo!(), + }, + _ => todo!(), + }, + LC::Var(0) => I, + _ => todo!(), + }, + LC::App(f, x) => { + let f: SKI = f.try_into()?; + let x: SKI = x.try_into()?; + f.app(x) + } + LC::Var(_) => Err(SKICompileError::FreeVar)?, + }) } } + +impl TryFrom> for SKI { + type Error = SKICompileError; + + fn try_from(value: Rc) -> Result { + (*value).clone().try_into() + } +} + +#[derive(Debug, PartialEq, Eq)] +pub enum Iota { + Iota, + App(Rc, Rc), +}