From fa66f1ee135fe5bfac38b4155e9cca2bd8ce746b Mon Sep 17 00:00:00 2001 From: schaeff Date: Tue, 28 Mar 2023 23:27:07 +0200 Subject: [PATCH] simplify --- src/analyzer/pil_analyzer.rs | 20 +++++++++------ src/parser/ast.rs | 47 ++++++------------------------------ src/parser/display.rs | 15 +++--------- src/parser/powdr.lalrpop | 4 +-- 4 files changed, 24 insertions(+), 62 deletions(-) diff --git a/src/analyzer/pil_analyzer.rs b/src/analyzer/pil_analyzer.rs index 4c89cf628..a312f7d7b 100644 --- a/src/analyzer/pil_analyzer.rs +++ b/src/analyzer/pil_analyzer.rs @@ -3,7 +3,7 @@ use std::fs; use std::path::{Path, PathBuf}; use crate::number::{abstract_to_degree, DegreeType}; -use crate::parser::ast::{self, ArrayExpression, Repetition}; +use crate::parser::ast::{self, ArrayExpression}; pub use crate::parser::ast::{BinaryOperator, UnaryOperator}; use crate::{parser, utils}; @@ -342,8 +342,8 @@ impl PILContext { } } ast::FunctionDefinition::Array(value) => { - let expressions = self - .process_array_expression(&value.clone().concretize(self.polynomial_degree)); + let star_value = value.solve(self.polynomial_degree); + let expressions = self.process_array_expression(value, star_value); assert_eq!(expressions.len() as u64, self.polynomial_degree); FunctionValueDefinition::Array(expressions) } @@ -438,17 +438,21 @@ impl PILContext { } } - fn process_array_expression(&mut self, array_expression: &ArrayExpression) -> Vec { + fn process_array_expression( + &mut self, + array_expression: &ArrayExpression, + star_value: Option, + ) -> Vec { match array_expression { - ArrayExpression::RepeatedValue(expressions, Repetition::Concrete(times)) => (0..*times) + ArrayExpression::Value(expressions) => self.process_expressions(expressions), + ArrayExpression::RepeatedValue(expressions) => (0..star_value.unwrap()) .flat_map(|_| self.process_expressions(expressions)) .collect(), ArrayExpression::Concat(left, right) => self - .process_array_expression(left) + .process_array_expression(left, star_value) .into_iter() - .chain(self.process_array_expression(right)) + .chain(self.process_array_expression(right, star_value)) .collect(), - _ => unreachable!("The repetitions should have a concrete value, found *"), } } diff --git a/src/parser/ast.rs b/src/parser/ast.rs index 295c298db..23e178b2a 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -98,17 +98,18 @@ pub enum FunctionDefinition { #[derive(Debug, PartialEq, Eq, Clone)] pub enum ArrayExpression { - RepeatedValue(Vec, Repetition), + Value(Vec), + RepeatedValue(Vec), Concat(Box, Box), } impl ArrayExpression { pub fn value(v: Vec) -> Self { - Self::RepeatedValue(v, Repetition::Concrete(1)) + Self::Value(v) } pub fn repeated_value(v: Vec) -> Self { - Self::RepeatedValue(v, Repetition::Star) + Self::RepeatedValue(v) } pub fn concat(self, other: Self) -> Self { @@ -125,7 +126,7 @@ impl ArrayExpression { impl ArrayExpression { /// solve for `*` - fn solve(&self, degree: DegreeType) -> Option { + pub fn solve(&self, degree: DegreeType) -> Option { // the length of this expression is `a + b*x` let (a, b) = self.len(); // it must match `degree`, and we solve for `x` @@ -140,8 +141,8 @@ impl ArrayExpression { /// find the total length of an array expression as an affine expression: `a + b*x` fn len(&self) -> (DegreeType, DegreeType) { match self { - ArrayExpression::RepeatedValue(e, Repetition::Star) => (0, e.len() as u64), - ArrayExpression::RepeatedValue(e, Repetition::Concrete(r)) => (e.len() as u64 * r, 0), + ArrayExpression::RepeatedValue(e) => (0, e.len() as u64), + ArrayExpression::Value(e) => (e.len() as u64, 0), ArrayExpression::Concat(left, right) => { let (a0, b0) = left.len(); let (a1, b1) = right.len(); @@ -155,38 +156,4 @@ impl ArrayExpression { } } } - - // replace `*` by a concrete value `star_value` - fn concretize_aux(self, star_value: DegreeType) -> Self { - match self { - ArrayExpression::RepeatedValue(value, repetition) => { - Self::RepeatedValue(value, repetition.concretize(star_value)) - } - ArrayExpression::Concat(left, right) => left - .concretize(star_value) - .concat(right.concretize(star_value)), - } - } - - pub fn concretize(self, degree: DegreeType) -> Self { - match self.solve(degree) { - Some(star_value) => self.concretize_aux(star_value), - None => self, - } - } -} - -#[derive(Debug, PartialEq, Eq, Clone)] -pub enum Repetition { - Concrete(u64), - Star, -} - -impl Repetition { - fn concretize(self, star_value: DegreeType) -> Self { - match self { - Repetition::Star => Repetition::Concrete(star_value), - r => r, - } - } } diff --git a/src/parser/display.rs b/src/parser/display.rs index 7dca7c0ac..343d744d8 100644 --- a/src/parser/display.rs +++ b/src/parser/display.rs @@ -91,26 +91,17 @@ fn format_names(names: &[PolynomialName]) -> String { impl Display for ArrayExpression { fn fmt(&self, f: &mut Formatter<'_>) -> Result { match self { - ArrayExpression::RepeatedValue(expressions, Repetition::Concrete(1)) => { + ArrayExpression::Value(expressions) => { write!(f, "[{}]", format_expressions(expressions)) } - ArrayExpression::RepeatedValue(expressions, r) => { - write!(f, "[{}]{}", format_expressions(expressions), r) + ArrayExpression::RepeatedValue(expressions) => { + write!(f, "[{}]*", format_expressions(expressions)) } ArrayExpression::Concat(left, right) => write!(f, "({} + {})", left, right), } } } -impl Display for Repetition { - fn fmt(&self, f: &mut Formatter<'_>) -> Result { - match self { - Repetition::Concrete(v) => write!(f, "*{{{}}}", v), - Repetition::Star => write!(f, "*"), - } - } -} - impl Display for FunctionDefinition { fn fmt(&self, f: &mut Formatter<'_>) -> Result { match self { diff --git a/src/parser/powdr.lalrpop b/src/parser/powdr.lalrpop index 9db8b6530..01b692540 100644 --- a/src/parser/powdr.lalrpop +++ b/src/parser/powdr.lalrpop @@ -330,8 +330,8 @@ PublicReference: String = { } ArrayExpression: ArrayExpression = { - "[" "]" => ArrayExpression::RepeatedValue(<>, Repetition::Concrete(1)), - "[" "]" "*" => ArrayExpression::RepeatedValue(<>, Repetition::Star), + "[" "]" => ArrayExpression::value(<>), + "[" "]" "*" => ArrayExpression::repeated_value(<>), "(" "+" ")" => ArrayExpression::concat(<>), }