Macro add_primitive

add_primitive!() { /* proc-macro */ }
Expand description

This macro lets the user declare custom egglog primitives. It supports a few special features:

  • Failure: using -?> instead of -> before the return type will make the primitive fallible, requiring the body to return an Option<T> instead of just T.

  • Varargs: using [xs: T] as the argument list will cause the primitive to take a variable number of arguments, which will be passed to be body as an Iterator<Item=T>.

  • Polymorphism: using # as a type will allow any type during typechecking. These will be given type Value in the body.

  • Containers: you must put an @ in front of container types.

  • Specialized Constraints: using T (E) as a type will use (E:expr).clone() in the type constraint but T:ty in the body. This is necessary because the relationship between Rust types and egglog sorts is not 1-to-1.

  • Context: sometimes you want your primitive to reference information from the scope of the add_primitive! call. Putting {x: T} between the = and the argument list will let you access the expression x of type T from inside the body as self.ctx. T must be the real Rust type of x. T must be Clone and 'static.