conjure_cp_core/lib.rs
1#[cfg(all(target_family = "wasm", not(feature = "sat-batsat")))]
2compile_error!("Wasm requires sat-batsat; use --no-default-features --features sat-batsat.");
3
4#[doc(hidden)]
5pub extern crate self as conjure_cp_core;
6
7#[doc(hidden)]
8pub use ast::Model;
9
10pub mod ast;
11
12// NOTE: this module defines the bug! macro, which is exported at the crate level, and has no other
13// contents.
14pub mod bug;
15
16pub mod context;
17pub mod domain_tightening;
18pub mod error;
19pub mod instantiate;
20pub mod objective;
21pub mod parse;
22pub mod representation;
23pub mod rule_engine;
24pub mod settings;
25pub mod solver;
26pub mod stats;
27
28// Various internal helper functions
29pub mod utils;
30
31/// Creates a [`Domain::Int`](ast::Domain::Int).
32///
33/// # Examples
34/// ```
35/// use conjure_cp_core::{domain_int,range,ast::Domain};
36///
37/// let a = 2*10;
38/// assert_eq!(domain_int!(1..5,a+2,), Domain::int(vec![range!(1..5),range!(a+2)]));
39/// assert_eq!(domain_int!(), Domain::int_ground(vec![]))
40/// ```
41#[macro_export]
42macro_rules! domain_int {
43 () => {$crate::ast::Domain::int_ground(vec![])};
44
45 // when parsing expressions, rust groups 1..2 into a single token tree, (1..2)
46 // however, we want it to be three seperate token trees [1,..,2] for parsing.
47 // use defile to turn it back into 3 token trees
48 ($($e:expr),+ $(,)?) => {::defile::defile! { $crate::ast::Domain::int(vec![$($crate::range!(@$e)),+]) } };
49}
50
51/// Creates a [`GroundDomain::Int`]
52#[macro_export]
53macro_rules! domain_int_ground {
54 () => {$crate::ast::GroundDomain::Int(vec![]).into()};
55
56 // when parsing expressions, rust groups 1..2 into a single token tree, (1..2)
57 // however, we want it to be three seperate token trees [1,..,2] for parsing.
58 // use defile to turn it back into 3 token trees
59 ($($e:expr),+ $(,)?) => {::defile::defile! { $crate::ast::GroundDomain::Int(vec![$($crate::range!(@$e)),+], None).into() } };
60}
61
62/// Creates a [`Range`](ast::Range).
63///
64/// # Examples
65///
66/// ```
67/// use conjure_cp_core::{range,ast::Range};
68///
69/// let a = 2*10;
70/// assert_eq!(range!(..a),Range::UnboundedL(a));
71/// assert_eq!(range!(2*5..),Range::UnboundedR(10));
72/// assert_eq!(range!(..10),Range::UnboundedL(10));
73/// assert_eq!(range!(1..5),Range::Bounded(1,5));
74/// assert_eq!(range!(Some(10).unwrap()),Range::Single(10));
75/// ```
76#[macro_export]
77macro_rules! range {
78 // decl macros have no lookahead, hence this nonsense with pushdown automata.
79
80 // @hasLowerbound: have atleast one token of the lower bound on the stack
81 (@hasLowerBound [$($lower:tt)+] -> ..) => {$crate::ast::Range::UnboundedR($crate::as_expr!($($lower)+))};
82 (@hasLowerBound [$($lower:tt)+] -> .. $($tail:tt)+) => {$crate::ast::Range::Bounded($crate::as_expr!($($lower)+),$crate::as_expr!($($tail)+))};
83 (@hasLowerBound [$($lower:tt)+] -> $b:tt $($tail:tt)*) => {range!(@hasLowerBound [$($lower)+ $b] -> $($tail)*)};
84 (@hasLowerBound [$($lower:tt)+] ->) => {$crate::ast::Range::Single($crate::as_expr!($($lower)+))};
85
86 // initial tokens
87 (.. $($a:tt)+) => {$crate::ast::Range::UnboundedL($crate::as_expr!($($a)+))};
88
89 ($a:tt $($tail:tt)*) => {range!(@hasLowerBound [$a] -> $($tail)*)};
90
91}
92
93// coorce a tt fragment into a expr fragment
94// https://lukaswirth.dev/tlborm/decl-macros/building-blocks/ast-coercion.html
95#[macro_export]
96#[doc(hidden)]
97macro_rules! as_expr {
98 ($e:expr) => {
99 $e
100 };
101}