From f3e226d8e89278a25303ce0c9e3693f30b0e53d6 Mon Sep 17 00:00:00 2001 From: Cassie Jones Date: Mon, 17 Feb 2020 12:05:01 +0100 Subject: [PATCH] Move the terms out into a separate file The term is essentially purely user syntax, and has a decent chunk of trait implementation boilerplate. It's nice to split it out from the main chunk of the algorithm. --- src/lib.rs | 63 +++-------------------------------------------------- src/term.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 60 deletions(-) create mode 100644 src/term.rs diff --git a/src/lib.rs b/src/lib.rs index ad587d1..4c84efb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,66 +1,9 @@ use dyn_clone::{self, DynClone}; use im::{HashMap, Vector}; -use std::{fmt, sync::Arc}; +use std::fmt; -#[derive(Clone, Copy, PartialEq, Eq, Hash)] -pub struct Var(u32); - -#[derive(Clone, PartialEq)] -pub enum Term { - Var(Var), - Pair(Arc, Arc), - Lit(i32), -} - -impl From<&Term> for Term { - fn from(term: &Term) -> Term { - term.clone() - } -} - -impl From for Term { - fn from(i: i32) -> Term { - Term::Lit(i) - } -} - -impl From for Term { - fn from(var: Var) -> Term { - Term::Var(var) - } -} - -impl fmt::Display for Term { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self { - Term::Var(v) => write!(fmt, "{:?}", v), - Term::Pair(l, r) => write!(fmt, "({}, {})", *l, *r), - Term::Lit(i) => write!(fmt, "{}", i), - } - } -} - -impl fmt::Debug for Term { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self) - } -} - -impl fmt::Debug for Var { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "x{}", self.0) - } -} - -impl From<(T, U)> for Term -where - T: Into, - U: Into, -{ - fn from((t, u): (T, U)) -> Term { - Term::Pair(Arc::new(t.into()), Arc::new(u.into())) - } -} +pub mod term; +pub use term::{Term, Var}; #[derive(Debug, Clone, Default)] pub struct State { diff --git a/src/term.rs b/src/term.rs new file mode 100644 index 0000000..5a7f80f --- /dev/null +++ b/src/term.rs @@ -0,0 +1,61 @@ +use std::{fmt, sync::Arc}; + +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +pub struct Var(pub(crate) u32); + +#[derive(Clone, PartialEq)] +pub enum Term { + Var(Var), + Pair(Arc, Arc), + Lit(i32), +} + +impl From<&Term> for Term { + fn from(term: &Term) -> Term { + term.clone() + } +} + +impl From for Term { + fn from(i: i32) -> Term { + Term::Lit(i) + } +} + +impl From for Term { + fn from(var: Var) -> Term { + Term::Var(var) + } +} + +impl From<(T, U)> for Term +where + T: Into, + U: Into, +{ + fn from((t, u): (T, U)) -> Term { + Term::Pair(Arc::new(t.into()), Arc::new(u.into())) + } +} + +impl fmt::Display for Term { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + match self { + Term::Var(v) => write!(fmt, "{:?}", v), + Term::Pair(l, r) => write!(fmt, "({}, {})", *l, *r), + Term::Lit(i) => write!(fmt, "{}", i), + } + } +} + +impl fmt::Debug for Term { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "{}", self) + } +} + +impl fmt::Debug for Var { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "x{}", self.0) + } +} -- 2.47.0