From 67477b8439f8f055a27c1b10f4b3c982a4c43d88 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Thu, 3 Oct 2024 23:40:04 +0200 Subject: [PATCH] make TypeDict a trait & BimapTypeDict an impl --- src/dict.rs | 76 ++++++++++++++++++++++++++++++++++++------------- src/parser.rs | 17 +++++++++-- src/sugar.rs | 7 +++-- src/unparser.rs | 8 ++++-- 4 files changed, 82 insertions(+), 26 deletions(-) diff --git a/src/dict.rs b/src/dict.rs index 419d599..4d5b35b 100644 --- a/src/dict.rs +++ b/src/dict.rs @@ -8,9 +8,27 @@ pub enum TypeID { Var(u64) } +pub trait TypeDict { + fn insert(&mut self, name: String, id: TypeID); + fn add_varname(&mut self, vn: String) -> TypeID; + fn add_typename(&mut self, tn: String) -> TypeID; + fn get_typeid(&self, tn: &String) -> Option; + fn get_typename(&self, tid: &TypeID) -> Option; + + fn get_varname(&self, var_id: u64) -> Option { + self.get_typename(&TypeID::Var(var_id)) + } + + fn add_synonym(&mut self, new: String, old: String) { + if let Some(tyid) = self.get_typeid(&old) { + self.insert(new, tyid); + } + } +} + //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\ -pub struct TypeDict { +pub struct BimapTypeDict { typenames: Bimap, type_lit_counter: u64, type_var_counter: u64, @@ -18,46 +36,66 @@ pub struct TypeDict { //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\ -impl TypeDict { +impl BimapTypeDict { pub fn new() -> Self { - TypeDict { + BimapTypeDict { typenames: Bimap::new(), type_lit_counter: 0, type_var_counter: 0, } } +} - pub fn add_varname(&mut self, tn: String) -> TypeID { +impl TypeDict for BimapTypeDict { + fn insert(&mut self, name: String, id: TypeID) { + self.typenames.insert(name, id); + } + + fn add_varname(&mut self, tn: String) -> TypeID { let tyid = TypeID::Var(self.type_var_counter); self.type_var_counter += 1; - self.typenames.insert(tn, tyid.clone()); + self.insert(tn, tyid.clone()); tyid } - pub fn add_typename(&mut self, tn: String) -> TypeID { + fn add_typename(&mut self, tn: String) -> TypeID { let tyid = TypeID::Fun(self.type_lit_counter); self.type_lit_counter += 1; - self.typenames.insert(tn, tyid.clone()); + self.insert(tn, tyid.clone()); tyid } - pub fn add_synonym(&mut self, new: String, old: String) { - if let Some(tyid) = self.get_typeid(&old) { - self.typenames.insert(new, tyid); - } - } - - pub fn get_typename(&self, tid: &TypeID) -> Option { + fn get_typename(&self, tid: &TypeID) -> Option { self.typenames.my.get(tid).cloned() } - pub fn get_typeid(&self, tn: &String) -> Option { + fn get_typeid(&self, tn: &String) -> Option { self.typenames.mλ.get(tn).cloned() } - - pub fn get_varname(&self, var_id: u64) -> Option { - self.typenames.my.get(&TypeID::Var(var_id)).cloned() - } +} + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +use std::sync::Arc; +use std::ops::{Deref, DerefMut}; +use std::sync::RwLock; + +impl TypeDict for Arc> { + fn insert(&mut self, name: String, id: TypeID) { + self.write().unwrap().insert(name, id); + } + fn add_varname(&mut self, vn: String) -> TypeID { + self.write().unwrap().add_varname(vn) + } + fn add_typename(&mut self, tn: String) -> TypeID { + self.write().unwrap().add_typename(tn) + } + fn get_typename(&self, tid: &TypeID)-> Option { + self.read().unwrap().get_typename(tid) + } + fn get_typeid(&self, tn: &String) -> Option { + self.read().unwrap().get_typeid(tn) + } } //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> diff --git a/src/parser.rs b/src/parser.rs index 85ff9b4..6160ca6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -18,10 +18,23 @@ pub enum ParseError { UnexpectedToken } +pub trait ParseLadderType { + fn parse(&mut self, s: &str) -> Result; + + fn parse_app(&mut self, tokens: &mut Peekable>) -> Result + where It: Iterator; + + fn parse_rung(&mut self, tokens: &mut Peekable>) -> Result + where It: Iterator; + + fn parse_ladder(&mut self, tokens: &mut Peekable>) -> Result + where It: Iterator; +} + //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\ -impl TypeDict { - pub fn parse(&mut self, s: &str) -> Result { +impl ParseLadderType for T { + fn parse(&mut self, s: &str) -> Result { let mut tokens = LadderTypeLexer::from(s.chars()).peekable(); match self.parse_ladder(&mut tokens) { diff --git a/src/sugar.rs b/src/sugar.rs index 4d13f78..dea73ba 100644 --- a/src/sugar.rs +++ b/src/sugar.rs @@ -1,7 +1,8 @@ use { - crate::{TypeTerm, TypeID} + crate::{TypeTerm, TypeID, parser::ParseLadderType} }; +#[derive(Clone)] pub enum SugaredTypeTerm { TypeID(TypeID), Num(i64), @@ -17,7 +18,7 @@ pub enum SugaredTypeTerm { } impl TypeTerm { - pub fn sugar(self: TypeTerm, dict: &mut crate::TypeDict) -> SugaredTypeTerm { + pub fn sugar(self: TypeTerm, dict: &mut impl crate::TypeDict) -> SugaredTypeTerm { match self { TypeTerm::TypeID(id) => SugaredTypeTerm::TypeID(id), TypeTerm::Num(n) => SugaredTypeTerm::Num(n), @@ -61,7 +62,7 @@ impl TypeTerm { } impl SugaredTypeTerm { - pub fn desugar(self, dict: &mut crate::TypeDict) -> TypeTerm { + pub fn desugar(self, dict: &mut impl crate::TypeDict) -> TypeTerm { match self { SugaredTypeTerm::TypeID(id) => TypeTerm::TypeID(id), SugaredTypeTerm::Num(n) => TypeTerm::Num(n), diff --git a/src/unparser.rs b/src/unparser.rs index ccf754d..b78494e 100644 --- a/src/unparser.rs +++ b/src/unparser.rs @@ -2,8 +2,12 @@ use crate::{dict::*, term::*}; //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\ -impl TypeDict { - pub fn unparse(&self, t: &TypeTerm) -> String { +pub trait UnparseLadderType { + fn unparse(&self, t: &TypeTerm) -> String; +} + +impl UnparseLadderType for T { + fn unparse(&self, t: &TypeTerm) -> String { match t { TypeTerm::TypeID(id) => self.get_typename(id).unwrap(), TypeTerm::Num(n) => format!("{}", n),