Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename core types for clarity #252

Merged
merged 2 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion air-script-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This crate contains the core components used by other crates in AirScript. These components include:

- Access structures defined in [VectorAccess and MatrixAccess](./access.rs) structs
- Constant structure defined in [Constant](./constant.rs) struct
- Constant structure defined in [ConstantBinding](./constant.rs) struct
- Expression set defined in [Expression](./expression.rs) enum
- Identifier structure defined in [Identifier](./identifier.rs) struct
- Trace Access structures defined in [TraceBindingAccess and TraceAccess](./trace.rs) structs
Expand Down
6 changes: 3 additions & 3 deletions air-script-core/src/comprehension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ impl ListComprehension {

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum ListFolding {
Sum(ListFoldingValueType),
Prod(ListFoldingValueType),
Sum(ListFoldingValueExpr),
Prod(ListFoldingValueExpr),
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum ListFoldingValueType {
pub enum ListFoldingValueExpr {
Identifier(Identifier),
Vector(Vec<Expression>),
ListComprehension(ListComprehension),
Expand Down
22 changes: 11 additions & 11 deletions air-script-core/src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,38 @@ use super::Identifier;
/// - Vector: \[1, 2, 3\]
/// - Matrix: \[\[1, 2, 3\], \[4, 5, 6\]\]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Constant {
pub struct ConstantBinding {
name: Identifier,
value: ConstantType,
value: ConstantValueExpr,
}

impl Constant {
/// Returns a new instance of a [Constant]
pub fn new(name: Identifier, value: ConstantType) -> Self {
impl ConstantBinding {
/// Returns a new instance of a [ConstantBinding]
pub fn new(name: Identifier, value: ConstantValueExpr) -> Self {
Self { name, value }
}

/// Returns the name of the [Constant]
/// Returns the name of the [ConstantBinding]
pub fn name(&self) -> &Identifier {
&self.name
}

/// Returns the value of the [Constant]
pub fn value(&self) -> &ConstantType {
/// Returns the value of the [ConstantBinding]
pub fn value(&self) -> &ConstantValueExpr {
&self.value
}

pub fn into_parts(self) -> (String, ConstantType) {
pub fn into_parts(self) -> (String, ConstantValueExpr) {
(self.name.into_name(), self.value)
}
}

/// Type of constant. Constants can be of 3 types:
/// Value of a constant. Constants can be of 3 value types:
/// - Scalar: 123
/// - Vector: \[1, 2, 3\]
/// - Matrix: \[\[1, 2, 3\], \[4, 5, 6\]\]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum ConstantType {
pub enum ConstantValueExpr {
Scalar(u64),
Vector(Vec<u64>),
Matrix(Vec<Vec<u64>>),
Expand Down
6 changes: 3 additions & 3 deletions air-script-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ mod access;
pub use access::{Iterable, MatrixAccess, Range, VectorAccess};

mod constant;
pub use constant::{Constant, ConstantType};
pub use constant::{ConstantBinding, ConstantValueExpr};

mod comprehension;
pub use comprehension::{
ComprehensionContext, ListComprehension, ListFolding, ListFoldingValueType,
ComprehensionContext, ListComprehension, ListFolding, ListFoldingValueExpr,
};

mod expression;
Expand All @@ -21,4 +21,4 @@ pub use trace::{
};

mod variable;
pub use variable::{Variable, VariableType};
pub use variable::{VariableBinding, VariableValueExpr};
17 changes: 9 additions & 8 deletions air-script-core/src/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,39 @@ use super::{Expression, Identifier, ListComprehension};
use std::fmt::Display;

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Variable {
pub struct VariableBinding {
name: Identifier,
value: VariableType,
value: VariableValueExpr,
}

impl Variable {
pub fn new(name: Identifier, value: VariableType) -> Self {
impl VariableBinding {
pub fn new(name: Identifier, value: VariableValueExpr) -> Self {
Self { name, value }
}

pub fn name(&self) -> &str {
self.name.name()
}

pub fn value(&self) -> &VariableType {
pub fn value(&self) -> &VariableValueExpr {
&self.value
}

pub fn into_parts(self) -> (String, VariableType) {
pub fn into_parts(self) -> (String, VariableValueExpr) {
(self.name.into_name(), self.value)
}
}

/// The expression or expressions that define the value of a variable binding.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum VariableType {
pub enum VariableValueExpr {
Scalar(Expression),
Vector(Vec<Expression>),
Matrix(Vec<Vec<Expression>>),
ListComprehension(ListComprehension),
}

impl Display for VariableType {
impl Display for VariableValueExpr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Scalar(_) => write!(f, "scalar"),
Expand Down
16 changes: 8 additions & 8 deletions codegen/winterfell/src/air/constants.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
use super::{AirIR, Constant, ConstantType, Scope};
use super::{AirIR, ConstantBinding, ConstantValueExpr, Scope};

/// Updates the provided scope with constant declarations.
pub(super) fn add_constants(scope: &mut Scope, ir: &AirIR) {
let constants = ir.constants();
let mut consts = vec![];
for constant in constants {
let const_str = match constant.value() {
ConstantType::Scalar(_) => {
ConstantValueExpr::Scalar(_) => {
format!(
"const {}: Felt = {};",
constant.name(),
constant.to_string()
)
}
ConstantType::Vector(vector) => format!(
ConstantValueExpr::Vector(vector) => format!(
"const {}: [Felt; {}] = {};",
constant.name(),
vector.len(),
constant.to_string()
),
ConstantType::Matrix(matrix) => format!(
ConstantValueExpr::Matrix(matrix) => format!(
"const {}: [[Felt; {}]; {}] = {};",
constant.name(),
matrix[0].len(),
Expand All @@ -37,17 +37,17 @@ trait Codegen {
fn to_string(&self) -> String;
}

impl Codegen for Constant {
impl Codegen for ConstantBinding {
fn to_string(&self) -> String {
match self.value() {
ConstantType::Scalar(scalar_const) => match scalar_const {
ConstantValueExpr::Scalar(scalar_const) => match scalar_const {
0 => "Felt::ZERO".to_string(),
1 => "Felt::ONE".to_string(),
_ => {
format!("Felt::new({scalar_const})")
}
},
ConstantType::Vector(vector_const) => format!(
ConstantValueExpr::Vector(vector_const) => format!(
"[{}]",
vector_const
.iter()
Expand All @@ -61,7 +61,7 @@ impl Codegen for Constant {
.collect::<Vec<String>>()
.join(", ")
),
ConstantType::Matrix(matrix_const) => {
ConstantValueExpr::Matrix(matrix_const) => {
let mut rows = vec![];
for row in matrix_const {
rows.push(format!(
Expand Down
2 changes: 1 addition & 1 deletion codegen/winterfell/src/air/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{AirIR, Impl, Scope};
use air_script_core::{Constant, ConstantType, TraceAccess};
use air_script_core::{ConstantBinding, ConstantValueExpr, TraceAccess};
use ir::{
constraints::{AlgebraicGraph, ConstraintDomain, Operation},
ConstantValue, IntegrityConstraintDegree, NodeIndex, PeriodicColumn, Value,
Expand Down
4 changes: 3 additions & 1 deletion ir/src/constraint_builder/boundary_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ impl ConstraintBuilder {
// save the constraint information
self.insert_constraint(root, lhs_segment.into(), domain)?
}
BoundaryStmt::Variable(variable) => self.symbol_table.insert_variable(variable)?,
BoundaryStmt::VariableBinding(variable) => {
self.symbol_table.insert_variable(variable)?
}
BoundaryStmt::ConstraintComprehension(_, _) => todo!(),
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{
BTreeMap, ConstraintBuilder, Expression, Identifier, Iterable, ListComprehension, ListFolding,
ListFoldingValueType, SemanticError, Symbol, SymbolType, TraceAccess, TraceBindingAccess,
TraceBindingAccessSize, VariableType, VectorAccess, CURRENT_ROW,
ListFoldingValueExpr, SemanticError, Symbol, SymbolType, TraceAccess, TraceBindingAccess,
TraceBindingAccessSize, VariableValueExpr, VectorAccess, CURRENT_ROW,
};

/// Maps each identifier in the list comprehension to its corresponding [Iterable].
Expand Down Expand Up @@ -185,7 +185,7 @@ impl ConstraintBuilder {
ListFolding::Sum(lf_value_type) | ListFolding::Prod(lf_value_type) => {
let list = self.build_list_from_list_folding_value(lf_value_type)?;
let iterable_context =
if let ListFoldingValueType::ListComprehension(lc) = lf_value_type {
if let ListFoldingValueExpr::ListComprehension(lc) = lf_value_type {
build_iterable_context(lc)?
} else {
BTreeMap::new()
Expand Down Expand Up @@ -238,7 +238,7 @@ impl ConstraintBuilder {
let symbol = self.symbol_table.get_symbol(ident.name())?;
match symbol.symbol_type() {
SymbolType::Variable(variable_type) => match variable_type {
VariableType::Vector(vector) => Ok(vector.len()),
VariableValueExpr::Vector(vector) => Ok(vector.len()),
_ => Err(SemanticError::InvalidListComprehension(format!(
"Variable {} should be a vector for a valid list comprehension.",
symbol.name()
Expand Down Expand Up @@ -312,7 +312,7 @@ fn build_ident_expression(symbol: &Symbol, i: usize) -> Result<Expression, Seman
}
SymbolType::Variable(variable_type) => {
match variable_type {
VariableType::Vector(vector) => {
VariableValueExpr::Vector(vector) => {
validate_access(i, vector.len())?;
Ok(vector[i].clone())
}
Expand Down Expand Up @@ -367,7 +367,7 @@ fn build_slice_ident_expression(
}
SymbolType::Variable(variable) => {
match variable {
VariableType::Vector(vector) => {
VariableValueExpr::Vector(vector) => {
validate_access(i, vector.len())?;
Ok(vector[range_start + i].clone())
}
Expand Down
16 changes: 8 additions & 8 deletions ir/src/constraint_builder/integrity_constraints/list_folding.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
ConstantType, ConstraintBuilder, Expression, ListFoldingValueType, SemanticError, SymbolType,
TraceAccess, VariableType, CURRENT_ROW,
ConstantValueExpr, ConstraintBuilder, Expression, ListFoldingValueExpr, SemanticError,
SymbolType, TraceAccess, VariableValueExpr, CURRENT_ROW,
};

// LIST FOLDING
Expand All @@ -16,17 +16,17 @@ impl ConstraintBuilder {
/// - the list folding value is an identifier that does not refer to a vector
pub fn build_list_from_list_folding_value(
&self,
lf_value_type: &ListFoldingValueType,
lf_value_type: &ListFoldingValueExpr,
) -> Result<Vec<Expression>, SemanticError> {
match lf_value_type {
ListFoldingValueType::Identifier(ident) => {
ListFoldingValueExpr::Identifier(ident) => {
let symbol = self.symbol_table.get_symbol(ident.name())?;
match symbol.symbol_type() {
SymbolType::Constant(ConstantType::Vector(list)) => {
SymbolType::Constant(ConstantValueExpr::Vector(list)) => {
Ok(list.iter().map(|value| Expression::Const(*value)).collect())
}
SymbolType::Variable(variable_type) => {
if let VariableType::Vector(list) = variable_type {
if let VariableValueExpr::Vector(list) = variable_type {
Ok(list.clone())
} else {
Err(SemanticError::invalid_list_folding(
Expand Down Expand Up @@ -61,8 +61,8 @@ impl ConstraintBuilder {
)),
}
}
ListFoldingValueType::Vector(vector) => Ok(vector.clone()),
ListFoldingValueType::ListComprehension(lc) => Ok(self.unfold_lc(lc)?),
ListFoldingValueExpr::Vector(vector) => Ok(vector.clone()),
ListFoldingValueExpr::ListComprehension(lc) => Ok(self.unfold_lc(lc)?),
}
}
}
16 changes: 8 additions & 8 deletions ir/src/constraint_builder/integrity_constraints/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::{
ast::{ConstraintType, IntegrityStmt},
BTreeMap, ConstantType, ConstraintBuilder, ConstraintDomain, Expression, Identifier, Iterable,
ListComprehension, ListFolding, ListFoldingValueType, SemanticError, Symbol, SymbolType,
TraceAccess, TraceBindingAccess, TraceBindingAccessSize, Variable, VariableType, VectorAccess,
CURRENT_ROW,
BTreeMap, ConstantValueExpr, ConstraintBuilder, ConstraintDomain, Expression, Identifier,
Iterable, ListComprehension, ListFolding, ListFoldingValueExpr, SemanticError, Symbol,
SymbolType, TraceAccess, TraceBindingAccess, TraceBindingAccessSize, VariableBinding,
VariableValueExpr, VectorAccess, CURRENT_ROW,
};

mod list_comprehension;
Expand Down Expand Up @@ -42,12 +42,12 @@ impl ConstraintBuilder {
// save the constraint information
self.insert_constraint(root, trace_segment.into(), domain)?;
}
IntegrityStmt::Variable(variable) => {
if let VariableType::ListComprehension(list_comprehension) = variable.value() {
IntegrityStmt::VariableBinding(variable) => {
if let VariableValueExpr::ListComprehension(list_comprehension) = variable.value() {
let vector = self.unfold_lc(list_comprehension)?;
self.symbol_table.insert_variable(Variable::new(
self.symbol_table.insert_variable(VariableBinding::new(
Identifier(variable.name().to_string()),
VariableType::Vector(vector),
VariableValueExpr::Vector(vector),
))?
} else {
self.symbol_table.insert_variable(variable)?
Expand Down
8 changes: 4 additions & 4 deletions ir/src/constraint_builder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::{
ast, AccessType, AlgebraicGraph, BTreeMap, BTreeSet, ConstantType, ConstantValue,
ast, AccessType, AlgebraicGraph, BTreeMap, BTreeSet, ConstantValue, ConstantValueExpr,
ConstraintDomain, ConstraintRoot, Constraints, Declarations, Expression, Identifier, Iterable,
ListComprehension, ListFolding, ListFoldingValueType, MatrixAccess, NodeIndex, Operation,
ListComprehension, ListFolding, ListFoldingValueExpr, MatrixAccess, NodeIndex, Operation,
SemanticError, Symbol, SymbolTable, SymbolType, TraceAccess, TraceBindingAccess,
TraceBindingAccessSize, TraceSegment, ValidateAccess, Value, Variable, VariableType,
VectorAccess, CURRENT_ROW,
TraceBindingAccessSize, TraceSegment, ValidateAccess, Value, VariableBinding,
VariableValueExpr, VectorAccess, CURRENT_ROW,
};

mod boundary_constraints;
Expand Down
Loading