1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
#[allow(unused_imports)]
use crate::*;
/** A macro to easily create a [`Language`].
`define_language` derives `Debug`, `PartialEq`, `Eq`, `PartialOrd`, `Ord`,
`Hash`, and `Clone` on the given `enum` so it can implement [`Language`].
The macro also implements [`Display`] and [`FromOp`] for the `enum`
based on either the data of variants or the provided strings.
The final variant **must have a trailing comma**; this is due to limitations in
macro parsing.
See [`LanguageChildren`] for acceptable types of children `Id`s.
Note that you can always implement [`Language`] yourself by just not using this
macro.
Presently, the macro does not support data variant with children, but that may
be added later.
# Example
The following macro invocation shows the the accepted forms of variants:
```
# use egg::*;
define_language! {
enum SimpleLanguage {
// string variant with no children
"pi" = Pi,
// string variants with an array of child `Id`s (any static size)
// any type that implements LanguageChildren may be used here
"+" = Add([Id; 2]),
"-" = Sub([Id; 2]),
"*" = Mul([Id; 2]),
// can also do a variable number of children in a boxed slice
// this will only match if the lengths are the same
"list" = List(Box<[Id]>),
// string variants with a single child `Id`
// note that this is distinct from `Sub`, even though it has the same
// string, because it has a different number of children
"-" = Neg(Id),
// data variants with a single field
// this field must implement `FromStr` and `Display`
Num(i32),
// language items are parsed in order, and we want symbol to
// be a fallback, so we put it last
Symbol(Symbol),
// This is the ultimate fallback, it will parse any operator (as a string)
// and any number of children.
// Note that if there were 0 children, the previous branch would have succeeded
Other(Symbol, Vec<Id>),
}
}
```
[`Display`]: std::fmt::Display
**/
#[macro_export]
macro_rules! define_language {
($(#[$meta:meta])* $vis:vis enum $name:ident $variants:tt) => {
$crate::__define_language!($(#[$meta])* $vis enum $name $variants -> {} {} {} {} {} {});
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __define_language {
($(#[$meta:meta])* $vis:vis enum $name:ident {} ->
$decl:tt {$($matches:tt)*} $children:tt $children_mut:tt
$display:tt {$($from_op:tt)*}
) => {
$(#[$meta])*
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
$vis enum $name $decl
impl $crate::Language for $name {
#[inline(always)]
fn matches(&self, other: &Self) -> bool {
::std::mem::discriminant(self) == ::std::mem::discriminant(other) &&
match (self, other) { $($matches)* _ => false }
}
fn children(&self) -> &[Id] { match self $children }
fn children_mut(&mut self) -> &mut [Id] { match self $children_mut }
}
impl ::std::fmt::Display for $name {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
// We need to pass `f` to the match expression for hygiene
// reasons.
match (self, f) $display
}
}
impl $crate::FromOp for $name {
type Error = $crate::FromOpError;
fn from_op(op: &str, children: ::std::vec::Vec<$crate::Id>) -> ::std::result::Result<Self, Self::Error> {
match (op, children) {
$($from_op)*
(op, children) => Err($crate::FromOpError::new(op, children)),
}
}
}
};
($(#[$meta:meta])* $vis:vis enum $name:ident
{
$string:literal = $variant:ident,
$($variants:tt)*
} ->
{ $($decl:tt)* } { $($matches:tt)* } { $($children:tt)* } { $($children_mut:tt)* }
{ $($display:tt)* } { $($from_op:tt)* }
) => {
$crate::__define_language!(
$(#[$meta])* $vis enum $name
{ $($variants)* } ->
{ $($decl)* $variant, }
{ $($matches)* ($name::$variant, $name::$variant) => true, }
{ $($children)* $name::$variant => &[], }
{ $($children_mut)* $name::$variant => &mut [], }
{ $($display)* ($name::$variant, f) => f.write_str($string), }
{ $($from_op)* ($string, children) if children.is_empty() => Ok($name::$variant), }
);
};
($(#[$meta:meta])* $vis:vis enum $name:ident
{
$string:literal = $variant:ident ($ids:ty),
$($variants:tt)*
} ->
{ $($decl:tt)* } { $($matches:tt)* } { $($children:tt)* } { $($children_mut:tt)* }
{ $($display:tt)* } { $($from_op:tt)* }
) => {
$crate::__define_language!(
$(#[$meta])* $vis enum $name
{ $($variants)* } ->
{ $($decl)* $variant($ids), }
{ $($matches)* ($name::$variant(l), $name::$variant(r)) => $crate::LanguageChildren::len(l) == $crate::LanguageChildren::len(r), }
{ $($children)* $name::$variant(ids) => $crate::LanguageChildren::as_slice(ids), }
{ $($children_mut)* $name::$variant(ids) => $crate::LanguageChildren::as_mut_slice(ids), }
{ $($display)* ($name::$variant(..), f) => f.write_str($string), }
{ $($from_op)* (op, children) if op == $string && <$ids as $crate::LanguageChildren>::can_be_length(children.len()) => {
let children = <$ids as $crate::LanguageChildren>::from_vec(children);
Ok($name::$variant(children))
},
}
);
};
($(#[$meta:meta])* $vis:vis enum $name:ident
{
$variant:ident ($data:ty),
$($variants:tt)*
} ->
{ $($decl:tt)* } { $($matches:tt)* } { $($children:tt)* } { $($children_mut:tt)* }
{ $($display:tt)* } { $($from_op:tt)* }
) => {
$crate::__define_language!(
$(#[$meta])* $vis enum $name
{ $($variants)* } ->
{ $($decl)* $variant($data), }
{ $($matches)* ($name::$variant(data1), $name::$variant(data2)) => data1 == data2, }
{ $($children)* $name::$variant(_data) => &[], }
{ $($children_mut)* $name::$variant(_data) => &mut [], }
{ $($display)* ($name::$variant(data), f) => ::std::fmt::Display::fmt(data, f), }
{ $($from_op)* (op, children) if op.parse::<$data>().is_ok() && children.is_empty() => Ok($name::$variant(op.parse().unwrap())), }
);
};
($(#[$meta:meta])* $vis:vis enum $name:ident
{
$variant:ident ($data:ty, $ids:ty),
$($variants:tt)*
} ->
{ $($decl:tt)* } { $($matches:tt)* } { $($children:tt)* } { $($children_mut:tt)* }
{ $($display:tt)* } { $($from_op:tt)* }
) => {
$crate::__define_language!(
$(#[$meta])* $vis enum $name
{ $($variants)* } ->
{ $($decl)* $variant($data, $ids), }
{ $($matches)* ($name::$variant(d1, l), $name::$variant(d2, r)) => d1 == d2 && $crate::LanguageChildren::len(l) == $crate::LanguageChildren::len(r), }
{ $($children)* $name::$variant(_, ids) => $crate::LanguageChildren::as_slice(ids), }
{ $($children_mut)* $name::$variant(_, ids) => $crate::LanguageChildren::as_mut_slice(ids), }
{ $($display)* ($name::$variant(data, _), f) => ::std::fmt::Display::fmt(data, f), }
{ $($from_op)* (op, children) if op.parse::<$data>().is_ok() && <$ids as $crate::LanguageChildren>::can_be_length(children.len()) => {
let data = op.parse::<$data>().unwrap();
let children = <$ids as $crate::LanguageChildren>::from_vec(children);
Ok($name::$variant(data, children))
},
}
);
};
}
/** A macro to easily make [`Rewrite`]s.
The `rewrite!` macro greatly simplifies creating simple, purely
syntactic rewrites while also allowing more complex ones.
This panics if [`Rewrite::new`](Rewrite::new()) fails.
The simplest form `rewrite!(a; b => c)` creates a [`Rewrite`]
with name `a`, [`Searcher`] `b`, and [`Applier`] `c`.
Note that in the `b` and `c` position, the macro only accepts a single
token tree (see the [macros reference][macro] for more info).
In short, that means you should pass in an identifier, literal, or
something surrounded by parentheses or braces.
If you pass in a literal to the `b` or `c` position, the macro will
try to parse it as a [`Pattern`] which implements both [`Searcher`]
and [`Applier`].
The macro also accepts any number of `if <expr>` forms at the end,
where the given expression should implement [`Condition`].
For each of these, the macro will wrap the given applier in a
[`ConditionalApplier`] with the given condition, with the first condition being
the outermost, and the last condition being the innermost.
# Example
```
# use egg::*;
use std::borrow::Cow;
use std::sync::Arc;
define_language! {
enum SimpleLanguage {
Num(i32),
"+" = Add([Id; 2]),
"-" = Sub([Id; 2]),
"*" = Mul([Id; 2]),
"/" = Div([Id; 2]),
}
}
type EGraph = egg::EGraph<SimpleLanguage, ()>;
let mut rules: Vec<Rewrite<SimpleLanguage, ()>> = vec![
rewrite!("commute-add"; "(+ ?a ?b)" => "(+ ?b ?a)"),
rewrite!("commute-mul"; "(* ?a ?b)" => "(* ?b ?a)"),
rewrite!("mul-0"; "(* ?a 0)" => "0"),
rewrite!("silly"; "(* ?a 1)" => { MySillyApplier("foo") }),
rewrite!("something_conditional";
"(/ ?a ?b)" => "(* ?a (/ 1 ?b))"
if is_not_zero("?b")),
];
// rewrite! supports bidirectional rules too
// it returns a Vec of length 2, so you need to concat
rules.extend(vec![
rewrite!("add-0"; "(+ ?a 0)" <=> "?a"),
rewrite!("mul-1"; "(* ?a 1)" <=> "?a"),
].concat());
#[derive(Debug)]
struct MySillyApplier(&'static str);
impl Applier<SimpleLanguage, ()> for MySillyApplier {
fn apply_one(&self, _: &mut EGraph, _: Id, _: &Subst, _: Option<&PatternAst<SimpleLanguage>>, _: Symbol) -> Vec<Id> {
panic!()
}
}
// This returns a function that implements Condition
fn is_not_zero(var: &'static str) -> impl Fn(&mut EGraph, Id, &Subst) -> bool {
let var = var.parse().unwrap();
let zero = SimpleLanguage::Num(0);
move |egraph, _, subst| !egraph[subst[var]].nodes.contains(&zero)
}
```
[macro]: https://doc.rust-lang.org/stable/reference/macros-by-example.html#metavariables
**/
#[macro_export]
macro_rules! rewrite {
(
$name:expr;
$lhs:tt => $rhs:tt
$(if $cond:expr)*
) => {{
let searcher = $crate::__rewrite!(@parse Pattern $lhs);
let core_applier = $crate::__rewrite!(@parse Pattern $rhs);
let applier = $crate::__rewrite!(@applier core_applier; $($cond,)*);
$crate::Rewrite::new($name.to_string(), searcher, applier).unwrap()
}};
(
$name:expr;
$lhs:tt <=> $rhs:tt
$(if $cond:expr)*
) => {{
let name = $name;
let name2 = String::from(name.clone()) + "-rev";
vec![
$crate::rewrite!(name; $lhs => $rhs $(if $cond)*),
$crate::rewrite!(name2; $rhs => $lhs $(if $cond)*)
]
}};
}
/** A macro to easily make [`Rewrite`]s using [`MultiPattern`]s.
Similar to the [`rewrite!`] macro,
this macro uses the form `multi_rewrite!(name; multipattern => multipattern)`.
String literals will be parsed a [`MultiPattern`]s.
**/
#[macro_export]
macro_rules! multi_rewrite {
// limited multipattern support
(
$name:expr;
$lhs:tt => $rhs:tt
) => {{
let searcher = $crate::__rewrite!(@parse MultiPattern $lhs);
let applier = $crate::__rewrite!(@parse MultiPattern $rhs);
$crate::Rewrite::new($name.to_string(), searcher, applier).unwrap()
}};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __rewrite {
(@parse $t:ident $rhs:literal) => {
$rhs.parse::<$crate::$t<_>>().unwrap()
};
(@parse $t:ident $rhs:expr) => { $rhs };
(@applier $applier:expr;) => { $applier };
(@applier $applier:expr; $cond:expr, $($conds:expr,)*) => {
$crate::ConditionalApplier {
condition: $cond,
applier: $crate::__rewrite!(@applier $applier; $($conds,)*)
}
};
}
#[cfg(test)]
mod tests {
use crate::*;
define_language! {
enum Simple {
"+" = Add([Id; 2]),
"-" = Sub([Id; 2]),
"*" = Mul([Id; 2]),
"-" = Neg(Id),
"list" = List(Box<[Id]>),
"pi" = Pi,
Int(i32),
Var(Symbol),
}
}
#[test]
fn modify_children() {
let mut add = Simple::Add([0.into(), 0.into()]);
add.for_each_mut(|id| *id = 1.into());
assert_eq!(add, Simple::Add([1.into(), 1.into()]));
}
#[test]
fn some_rewrites() {
let mut rws: Vec<Rewrite<Simple, ()>> = vec![
// here it should parse the rhs
rewrite!("rule"; "cons" => "f"),
// here it should just accept the rhs without trying to parse
rewrite!("rule"; "f" => { "pat".parse::<Pattern<_>>().unwrap() }),
];
rws.extend(rewrite!("two-way"; "foo" <=> "bar"));
}
#[test]
#[should_panic(expected = "refers to unbound var ?x")]
fn rewrite_simple_panic() {
let _: Rewrite<Simple, ()> = rewrite!("bad"; "?a" => "?x");
}
#[test]
#[should_panic(expected = "refers to unbound var ?x")]
fn rewrite_conditional_panic() {
let x: Pattern<Simple> = "?x".parse().unwrap();
let _: Rewrite<Simple, ()> = rewrite!(
"bad"; "?a" => "?a" if ConditionEqual::new(x.clone(), x)
);
}
}