Skip to content

Commit

Permalink
tidy: use simpler Biplate syntax where possible
Browse files Browse the repository at this point in the history
As discussed in conjure-cp/uniplate#19.
  • Loading branch information
niklasdewally committed Jan 30, 2025
1 parent 5d6be1d commit 8a1bf40
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion conjure_oxide/tests/generated_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ fn check_solutions_stage(
}

fn assert_vector_operators_have_partially_evaluated(model: &conjure_core::Model) {
for node in <_ as Biplate<Expression>>::universe_bi(&model.constraints) {
for node in model.constraints.universe_bi() {
use conjure_core::ast::Expression::*;
match node {
Sum(_, ref vec) => assert_constants_leq_one(&node, vec),
Expand Down
5 changes: 3 additions & 2 deletions conjure_oxide/tests/rewrite_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::collections::{BTreeMap, VecDeque};
use std::process::exit;

use conjure_core::rules::eval_constant;
Expand All @@ -13,7 +13,8 @@ use conjure_oxide::{
use uniplate::{Biplate, Uniplate};

fn var_name_from_atom(a: &Atom) -> Name {
<Atom as Biplate<Name>>::universe_bi(a)[0].clone()
let names: VecDeque<Name> = a.universe_bi();
names[0].clone()
}
#[test]
fn rules_present() {
Expand Down
6 changes: 4 additions & 2 deletions crates/conjure_core/src/ast/expressions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::VecDeque;
use std::fmt::{Display, Formatter};
use std::sync::Arc;

Expand Down Expand Up @@ -470,11 +471,12 @@ impl Expression {
}

pub fn get_meta(&self) -> Metadata {
<Expression as Biplate<Metadata>>::children_bi(self)[0].clone()
let metas: VecDeque<Metadata> = self.children_bi();
metas[0].clone()
}

pub fn set_meta(&self, meta: Metadata) {
<Expression as Biplate<Metadata>>::transform_bi(self, Arc::new(move |_| meta.clone()));
self.transform_bi(Arc::new(move |_| meta.clone()));
}

/// Checks whether this expression is safe.
Expand Down
2 changes: 1 addition & 1 deletion crates/conjure_core/src/rule_engine/rewrite_naive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn rewrite_naive<'a>(

// Iterate over rules by priority in descending order.
'top: for (priority, rule_set) in rules_by_priority.iter().rev() {
for (expr, ctx) in Biplate::<Expr>::contexts_bi(&model.get_constraints_vec()) {
for (expr, ctx) in <_ as Biplate<Expr>>::contexts_bi(&model.get_constraints_vec()) {
// Clone expr and ctx so they can be reused
let expr = expr.clone();
let ctx = ctx.clone();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Generic normalising rules for associative-commutative operators.
use std::collections::VecDeque;
use std::mem::Discriminant;

use conjure_core::ast::Expression as Expr;
Expand Down Expand Up @@ -34,7 +35,7 @@ fn normalise_associative_commutative(expr: &Expr, _: &Model) -> ApplicationResul
return vec![expr];
}

let child_vecs = <_ as Biplate<Vec<Expr>>>::children_bi(&expr);
let child_vecs: VecDeque<Vec<Expr>> = expr.children_bi();

// empty expression
if child_vecs.is_empty() {
Expand All @@ -56,7 +57,7 @@ fn normalise_associative_commutative(expr: &Expr, _: &Model) -> ApplicationResul
new_children
}

let child_vecs = <_ as Biplate<Vec<Expr>>>::children_bi(expr);
let child_vecs: VecDeque<Vec<Expr>> = expr.children_bi();
if child_vecs.is_empty() {
return Err(RuleNotApplicable);
}
Expand Down
3 changes: 2 additions & 1 deletion crates/conjure_core/src/rules/normalisers/neg_minus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use Expr::*;

use crate::ast::{Atom, Literal as Lit};
use crate::metadata::Metadata;
use std::collections::VecDeque;

/// Eliminates double negation
///
Expand All @@ -62,7 +63,7 @@ fn distribute_negation_over_sum(expr: &Expr, _: &Model) -> ApplicationResult {
_ => Err(RuleNotApplicable),
}?;

let mut child_vecs = <_ as Biplate<Vec<Expr>>>::children_bi(&inner_expr);
let mut child_vecs: VecDeque<Vec<Expr>> = inner_expr.children_bi();

if child_vecs.is_empty() {
return Err(RuleNotApplicable);
Expand Down
2 changes: 1 addition & 1 deletion crates/conjure_core/src/rules/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn is_flat(expr: &Expr) -> bool {

/// True if the entire AST is constants.
pub fn is_all_constant(expression: &Expr) -> bool {
for atom in <Expr as Biplate<Atom>>::universe_bi(expression) {
for atom in expression.universe_bi() {
match atom {
Atom::Literal(_) => {}
Atom::Reference(_) => {
Expand Down

0 comments on commit 8a1bf40

Please sign in to comment.