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
use std::fmt;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Expr<T> {
    Any(Any<T>),
    All(All<T>),
    Not(Not<T>),
    Var(Var<T>),
    Const(bool),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Any<T>(pub Vec<Expr<T>>);

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct All<T>(pub Vec<Expr<T>>);

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Not<T>(pub Box<Expr<T>>);

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Var<T>(pub T);

pub fn expr<T>(x: impl Into<Expr<T>>) -> Expr<T> {
    x.into()
}

pub fn any<T>(x: impl Into<Any<T>>) -> Any<T> {
    x.into()
}

pub fn all<T>(x: impl Into<All<T>>) -> All<T> {
    x.into()
}

pub fn not<T>(x: impl Into<Not<T>>) -> Not<T> {
    x.into()
}

pub fn var<T>(x: impl Into<Var<T>>) -> Var<T> {
    x.into()
}

pub fn const_<T>(x: bool) -> Expr<T> {
    x.into()
}

impl<T> From<Any<T>> for Expr<T> {
    fn from(any: Any<T>) -> Self {
        Expr::Any(any)
    }
}

impl<T> From<All<T>> for Expr<T> {
    fn from(all: All<T>) -> Self {
        Expr::All(all)
    }
}

impl<T> From<Not<T>> for Expr<T> {
    fn from(not: Not<T>) -> Self {
        Expr::Not(not)
    }
}

impl<T> From<Var<T>> for Expr<T> {
    fn from(var: Var<T>) -> Self {
        Expr::Var(var)
    }
}

impl<T> From<bool> for Expr<T> {
    fn from(b: bool) -> Self {
        Expr::Const(b)
    }
}

impl<T> From<Vec<Expr<T>>> for Any<T> {
    fn from(exprs: Vec<Expr<T>>) -> Self {
        Any(exprs)
    }
}

impl<T> From<Vec<Expr<T>>> for All<T> {
    fn from(exprs: Vec<Expr<T>>) -> Self {
        All(exprs)
    }
}

impl<T> From<Box<Expr<T>>> for Not<T> {
    fn from(expr: Box<Expr<T>>) -> Self {
        Not(expr)
    }
}

impl<T> From<Expr<T>> for Not<T> {
    fn from(expr: Expr<T>) -> Self {
        Not(Box::new(expr))
    }
}

impl<T> From<T> for Var<T> {
    fn from(var: T) -> Self {
        Var(var)
    }
}

macro_rules! impl_from_tuple {
    ($ty:ident, ($tt:tt,)) => {
        impl_from_tuple!(@expand $ty, ($tt,));
    };
    ($ty:ident, ($x:tt, $($xs:tt,)+)) => {
        impl_from_tuple!($ty, ($($xs,)+));
        impl_from_tuple!(@expand $ty, ($x, $($xs,)+));
    };
    (@expand $ty:ident, ($($tt:tt,)+)) => {
        #[doc(hidden)] // too ugly
        #[allow(non_camel_case_types)]
        impl<T, $($tt),+> From<($($tt,)+)>  for $ty<T>
        where
            $($tt: Into<Expr<T>>,)+
        {
            fn from(($($tt,)+): ($($tt,)+)) -> Self {
                Self::from(vec![$(Into::into($tt)),+])
            }
        }
    };
}

impl_from_tuple!(
    Any,
    (
        x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, //
        x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23, //
        x24, x25, x26, x27, x28, x29, x30, x31, x32, x33, x34, x35,
    )
);

impl_from_tuple!(
    All,
    (
        x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, //
        x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23, //
        x24, x25, x26, x27, x28, x29, x30, x31, x32, x33, x34, x35,
    )
);

impl<T> fmt::Display for Expr<T>
where
    T: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Expr::Any(Any(any)) => fmt_list(f, "any", any),
            Expr::All(All(all)) => fmt_list(f, "all", all),
            Expr::Not(Not(not)) => write!(f, "not({not})"),
            Expr::Var(Var(x)) => write!(f, "{x}"),
            Expr::Const(b) => write!(f, "{b}"),
        }
    }
}

fn fmt_list<T>(f: &mut fmt::Formatter<'_>, name: &str, list: &[Expr<T>]) -> fmt::Result
where
    T: fmt::Display,
{
    write!(f, "{name}(")?;
    for (i, e) in list.iter().enumerate() {
        if i != 0 {
            write!(f, ", ")?;
        }
        write!(f, "{e}")?;
    }
    write!(f, ")")
}