From 22e7d941bae6e6c1b25ea691fe9c4500377f534c Mon Sep 17 00:00:00 2001 From: Liberxue Date: Thu, 12 Sep 2024 22:04:53 +0800 Subject: [PATCH] #714Refactor Transform String Resugar Term Scott && keep function mimics the shape of the AST for easier visualization --- src/fun/transform/resugar_list.rs | 6 +- src/fun/transform/resugar_string.rs | 155 ---------------------------- 2 files changed, 3 insertions(+), 158 deletions(-) diff --git a/src/fun/transform/resugar_list.rs b/src/fun/transform/resugar_list.rs index e71aaa41..e343b4ea 100644 --- a/src/fun/transform/resugar_list.rs +++ b/src/fun/transform/resugar_list.rs @@ -79,7 +79,7 @@ impl Term { /// Converts scott-encoded lists ending with List/Nil to list literals. fn resugar_lists_scott(&mut self) { maybe_grow(|| { - // Searc h for a List/Cons pattern in the term and try to build a list from that point on. + // Searc h for a List/Cons pattern in the term and try to build a list from that point on. // If successful, replace the term with the list. // If not, keep as-is. @@ -124,7 +124,7 @@ impl Term { match l { Ok(l) => *self = Term::List { els: l.into_iter().map(|x| *x).collect() }, // Was not a list term, keep as-is. - Err(mut l) => { + Err(mut l) => { *head = l.pop().unwrap(); assert!(l.is_empty()) } @@ -241,7 +241,7 @@ fn build_list_scott(term: &mut Term, mut l: Vec>) -> Result ) if let Term::App { tag: Tag::Static, fun, arg: tail } = term { if let Term::App { tag: Tag::Static, fun, arg: head } = fun.as_mut() { diff --git a/src/fun/transform/resugar_string.rs b/src/fun/transform/resugar_string.rs index 77dfad0f..ec47533c 100644 --- a/src/fun/transform/resugar_string.rs +++ b/src/fun/transform/resugar_string.rs @@ -5,7 +5,6 @@ use crate::{ impl Term { /// Converts lambda-encoded strings ending with String/nil to string literals. - /// This method chooses the appropriate resugaring function based on the ADT encoding. pub fn resugar_strings(&mut self, adt_encoding: AdtEncoding) { match adt_encoding { AdtEncoding::Scott => self.try_resugar_strings_with(Self::resugar_strings_scott), @@ -14,7 +13,6 @@ impl Term { } /// Converts encoded strings to string literals using the provided extraction function. - /// This method recursively processes the term structure. fn try_resugar_strings_with(&mut self, extract_fn: fn(&Term) -> Option<(char, &Term)>) { maybe_grow(|| { // Try to resugar nil or cons patterns. If unsuccessful, recurse into child terms. @@ -31,38 +29,13 @@ impl Term { matches!(self, Term::Ref { nam } if nam == builtins::SNIL).then(|| *self = Term::str("")).is_some() } -<<<<<<< HEAD - /// Attempts to resugar a Scott-encoded cons term. - fn try_resugar_cons_scott(term: &mut Term) -> bool { - // Pattern matching to extract the necessary components Cons: @x (x CONS_TAG ) - let Term::Lam { tag: Tag::Static, pat: outer_pat, bod } = term else { return false }; - let Pattern::Var(None) = outer_pat.as_ref() else { return false }; - let Term::Lam { tag: Tag::Static, pat: inner_pat, bod: inner_bod } = bod.as_mut() else { return false }; - let Pattern::Var(Some(var_lam)) = inner_pat.as_ref() else { return false }; - let Term::App { tag: Tag::Static, fun, arg: tail } = inner_bod.as_mut() else { return false }; - let Term::App { tag: Tag::Static, fun: inner_fun, arg: head } = fun.as_mut() else { return false }; - let (Term::Var { nam: var_app }, Term::Num { val: Num::U24(head_val) }) = - (inner_fun.as_mut(), head.as_mut()) - else { - return false; - }; - - if var_lam != var_app { - return false; - }; - - let head_char = char::from_u32(*head_val).unwrap_or(char::REPLACEMENT_CHARACTER); - Self::build_string_scott(tail, head_char.to_string()).map(|str| *term = Term::str(&str)).is_some() -======= /// Attempts to resugar a cons term to a string literal. - /// This method tries both the provided extraction function and the common extraction method. fn try_resugar_strings_cons(&mut self, extract_fn: fn(&Term) -> Option<(char, &Term)>) -> bool { self .try_resugar_strings_cons_with(extract_fn) .or_else(|| self.try_resugar_strings_cons_common()) .map(|str| *self = Term::str(&str)) .is_some() ->>>>>>> 6b0dd50b (#714Refactor Transform String Resugar Term Scott && keep function mimics the shape of the AST for easier visualization) } /// Attempts to resugar a cons term using the provided extraction function. @@ -71,69 +44,6 @@ impl Term { .and_then(|(head_char, tail)| Self::build_strings_common(tail, head_char.to_string(), extract_fn)) } -<<<<<<< HEAD - /// Attempts to resugar a common cons term. - fn try_resugar_cons_common(&mut self, build_string_fn: fn(&Term, String) -> Option) -> bool { - let Term::App { tag: Tag::Static, fun, arg: tail } = self else { return false }; - let Term::App { tag: Tag::Static, fun: inner_fun, arg: head } = fun.as_mut() else { return false }; - let (Term::Ref { nam }, Term::Num { val: Num::U24(head_val) }) = (inner_fun.as_ref(), head.as_ref()) - else { - return false; - }; - - if nam != builtins::SCONS { - return false; - }; - - let head_char = char::from_u32(*head_val).unwrap_or(char::REPLACEMENT_CHARACTER); - build_string_fn(tail, head_char.to_string()).map(|str| *self = Term::str(&str)).is_some() - } - - /// common string building function that uses custom and default cons handlers. - /// pattern keep if tree mimics the shape of the AST visualize the structure #713 - fn build_string(term: &Term, mut s: String, cons_handler: F) -> Option - where - F: Fn(&Term) -> Option<(char, &Term)>, - { - maybe_grow(|| { - let mut current = term; - loop { - // Nil: String/nil - if let Term::Ref { nam } = current { - if nam == builtins::SNIL { - return Some(s); - } - } - - // Try custom cons handler - if let Some((head, tail)) = cons_handler(current) { - s.push(head); - current = tail; - continue; - } - - // Cons: @x (x CONS_TAG ) - if let Term::Lam { tag: Tag::Static, pat, bod } = current { - if let Pattern::Var(Some(var_lam)) = pat.as_ref() { - if let Term::App { tag: Tag::Static, fun, arg: tail } = bod.as_ref() { - if let Term::App { tag: Tag::Static, fun, arg: head } = fun.as_ref() { - if let Term::App { tag: Tag::Static, fun, arg } = fun.as_ref() { - if let Term::Var { nam: var_app } = fun.as_ref() { - if let Term::Ref { nam } = arg.as_ref() { - if let Term::Num { val: Num::U24(head_val) } = head.as_ref() { - if var_lam == var_app && nam == builtins::SCONS_TAG_REF { - let head_char = char::from_u32(*head_val).unwrap_or(char::REPLACEMENT_CHARACTER); - s.push(head_char); - current = tail; - continue; - } - } - } - } - } - } - } -======= /// Attempts to resugar a cons term using the common extraction method. fn try_resugar_strings_cons_common(&self) -> Option { if let Term::App { tag: Tag::Static, fun, arg: tail } = self { @@ -168,53 +78,12 @@ impl Term { let (head, next) = extract_fn(current).or_else(|| Self::extract_strings_common(current))?; s.push(head); current = next; ->>>>>>> 6b0dd50b (#714Refactor Transform String Resugar Term Scott && keep function mimics the shape of the AST for easier visualization) } } - - // Cons: (String/cons ) - if let Term::App { tag: Tag::Static, fun, arg: tail } = current { - if let Term::App { tag: Tag::Static, fun, arg: head } = fun.as_ref() { - if let Term::Ref { nam } = fun.as_ref() { - if let Term::Num { val: Num::U24(head_val) } = head.as_ref() { - if nam == builtins::SCONS { - let head_char = char::from_u32(*head_val).unwrap_or(char::REPLACEMENT_CHARACTER); - s.push(head_char); - current = tail; - continue; - } - } - } - } - } - - // Not a string term, stop - return None; } }) } -<<<<<<< HEAD - /// Builds a string from a NumScott-encoded term. - fn build_string_num_scott(term: &Term, s: String) -> Option { - Self::build_string(term, s, |term| { - let Term::Lam { tag: Tag::Static, pat, bod } = term else { return None }; - let Pattern::Var(Some(var_lam)) = pat.as_ref() else { return None }; - let Term::App { tag: Tag::Static, fun, arg: tail } = bod.as_ref() else { return None }; - let Term::App { tag: Tag::Static, fun, arg: head } = fun.as_ref() else { return None }; - let Term::App { tag: Tag::Static, fun, arg } = fun.as_ref() else { return None }; - let (Term::Var { nam: var_app }, Term::Ref { nam }, Term::Num { val: Num::U24(head_val) }) = - (fun.as_ref(), arg.as_ref(), head.as_ref()) - else { - return None; - }; - - if var_lam == var_app && nam == builtins::SCONS_TAG_REF { - let head_char = char::from_u32(*head_val).unwrap_or(char::REPLACEMENT_CHARACTER); - Some((head_char, tail)) - } else { - None -======= /// Extracts a character and the remaining term from a Scott-encoded string term. /// The structure of this function mimics the shape of the AST for easier visualization. fn resugar_strings_scott(term: &Term) -> Option<(char, &Term)> { @@ -236,33 +105,11 @@ impl Term { } } } ->>>>>>> 6b0dd50b (#714Refactor Transform String Resugar Term Scott && keep function mimics the shape of the AST for easier visualization) } } None } -<<<<<<< HEAD - /// Builds a string from a Scott-encoded term. - fn build_string_scott(term: &Term, s: String) -> Option { - Self::build_string(term, s, |term| { - let Term::Lam { tag: Tag::Static, pat, bod } = term else { return None }; - let Pattern::Var(None) = pat.as_ref() else { return None }; - let Term::Lam { tag: Tag::Static, pat, bod } = bod.as_ref() else { return None }; - let Pattern::Var(Some(var_lam)) = pat.as_ref() else { return None }; - let Term::App { tag: Tag::Static, fun, arg: tail } = bod.as_ref() else { return None }; - let Term::App { tag: Tag::Static, fun, arg: head } = fun.as_ref() else { return None }; - let (Term::Var { nam: var_app }, Term::Num { val: Num::U24(head_val) }) = (fun.as_ref(), head.as_ref()) - else { - return None; - }; - - if var_lam == var_app { - let head_char = char::from_u32(*head_val).unwrap_or(char::REPLACEMENT_CHARACTER); - Some((head_char, tail)) - } else { - None -======= /// Extracts a character and the remaining term from a NumScott-encoded string term. /// The structure of this function mimics the shape of the AST for easier visualization. fn resugar_strings_num_scott(term: &Term) -> Option<(char, &Term)> { @@ -285,7 +132,6 @@ impl Term { } } } ->>>>>>> 6b0dd50b (#714Refactor Transform String Resugar Term Scott && keep function mimics the shape of the AST for easier visualization) } } None @@ -307,4 +153,3 @@ impl Term { None } } -