1
use crate::ast::domains::attrs::MSetAttr;
2
use crate::ast::domains::attrs::SetAttr;
3
use crate::ast::{
4
    DeclarationKind, DomainOpError, Expression, FuncAttr, Literal, Metadata, Moo,
5
    RecordEntryGround, Reference, Typeable,
6
    domains::{
7
        GroundDomain,
8
        domain::{DomainPtr, Int},
9
        range::Range,
10
    },
11
};
12
use crate::{bug, domain_int, matrix_expr, range};
13
use conjure_cp_core::ast::pretty::pretty_vec;
14
use conjure_cp_core::ast::{Name, ReturnType, eval_constant};
15
use itertools::Itertools;
16
use polyquine::Quine;
17
use serde::{Deserialize, Serialize};
18
use std::fmt::{Display, Formatter};
19
use std::iter::zip;
20
use std::ops::Deref;
21
use uniplate::Uniplate;
22

            
23
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Quine, Uniplate)]
24
#[path_prefix(conjure_cp::ast)]
25
#[biplate(to=Expression)]
26
#[biplate(to=Reference)]
27
pub enum IntVal {
28
    Const(Int),
29
    #[polyquine_skip]
30
    Reference(Reference),
31
    Expr(Moo<Expression>),
32
}
33

            
34
impl From<Int> for IntVal {
35
15316
    fn from(value: Int) -> Self {
36
15316
        Self::Const(value)
37
15316
    }
38
}
39

            
40
impl TryInto<Int> for IntVal {
41
    type Error = DomainOpError;
42

            
43
54428
    fn try_into(self) -> Result<Int, Self::Error> {
44
54428
        match self {
45
52972
            IntVal::Const(val) => Ok(val),
46
1456
            _ => Err(DomainOpError::NotGround),
47
        }
48
54428
    }
49
}
50

            
51
impl From<Range<Int>> for Range<IntVal> {
52
7718
    fn from(value: Range<Int>) -> Self {
53
7718
        match value {
54
120
            Range::Single(x) => Range::Single(x.into()),
55
7598
            Range::Bounded(l, r) => Range::Bounded(l.into(), r.into()),
56
            Range::UnboundedL(r) => Range::UnboundedL(r.into()),
57
            Range::UnboundedR(l) => Range::UnboundedR(l.into()),
58
            Range::Unbounded => Range::Unbounded,
59
        }
60
7718
    }
61
}
62

            
63
impl TryInto<Range<Int>> for Range<IntVal> {
64
    type Error = DomainOpError;
65

            
66
29388
    fn try_into(self) -> Result<Range<Int>, Self::Error> {
67
29388
        match self {
68
1980
            Range::Single(x) => Ok(Range::Single(x.try_into()?)),
69
26186
            Range::Bounded(l, r) => Ok(Range::Bounded(l.try_into()?, r.try_into()?)),
70
176
            Range::UnboundedL(r) => Ok(Range::UnboundedL(r.try_into()?)),
71
300
            Range::UnboundedR(l) => Ok(Range::UnboundedR(l.try_into()?)),
72
746
            Range::Unbounded => Ok(Range::Unbounded),
73
        }
74
29388
    }
75
}
76

            
77
impl From<SetAttr<Int>> for SetAttr<IntVal> {
78
    fn from(value: SetAttr<Int>) -> Self {
79
        SetAttr {
80
            size: value.size.into(),
81
        }
82
    }
83
}
84

            
85
impl TryInto<SetAttr<Int>> for SetAttr<IntVal> {
86
    type Error = DomainOpError;
87

            
88
286
    fn try_into(self) -> Result<SetAttr<Int>, Self::Error> {
89
286
        let size: Range<Int> = self.size.try_into()?;
90
286
        Ok(SetAttr { size })
91
286
    }
92
}
93

            
94
impl From<MSetAttr<Int>> for MSetAttr<IntVal> {
95
    fn from(value: MSetAttr<Int>) -> Self {
96
        MSetAttr {
97
            size: value.size.into(),
98
            occurrence: value.occurrence.into(),
99
        }
100
    }
101
}
102

            
103
impl TryInto<MSetAttr<Int>> for MSetAttr<IntVal> {
104
    type Error = DomainOpError;
105

            
106
360
    fn try_into(self) -> Result<MSetAttr<Int>, Self::Error> {
107
360
        let size: Range<Int> = self.size.try_into()?;
108
360
        let occurrence: Range<Int> = self.occurrence.try_into()?;
109
360
        Ok(MSetAttr { size, occurrence })
110
360
    }
111
}
112

            
113
impl From<FuncAttr<Int>> for FuncAttr<IntVal> {
114
    fn from(value: FuncAttr<Int>) -> Self {
115
        FuncAttr {
116
            size: value.size.into(),
117
            partiality: value.partiality,
118
            jectivity: value.jectivity,
119
        }
120
    }
121
}
122

            
123
impl TryInto<FuncAttr<Int>> for FuncAttr<IntVal> {
124
    type Error = DomainOpError;
125

            
126
520
    fn try_into(self) -> Result<FuncAttr<Int>, Self::Error> {
127
520
        let size: Range<Int> = self.size.try_into()?;
128
520
        Ok(FuncAttr {
129
520
            size,
130
520
            jectivity: self.jectivity,
131
520
            partiality: self.partiality,
132
520
        })
133
520
    }
134
}
135

            
136
impl Display for IntVal {
137
3470116
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
138
3470116
        match self {
139
1673578
            IntVal::Const(val) => write!(f, "{val}"),
140
1318178
            IntVal::Reference(re) => write!(f, "{re}"),
141
478360
            IntVal::Expr(expr) => write!(f, "({expr})"),
142
        }
143
3470116
    }
144
}
145

            
146
impl IntVal {
147
916
    pub fn new_ref(re: &Reference) -> Option<IntVal> {
148
916
        match re.ptr.kind().deref() {
149
876
            DeclarationKind::ValueLetting(expr, _)
150
876
            | DeclarationKind::TemporaryValueLetting(expr) => match expr.return_type() {
151
876
                ReturnType::Int => Some(IntVal::Reference(re.clone())),
152
                _ => None,
153
            },
154
            DeclarationKind::Given(dom) => match dom.return_type() {
155
                ReturnType::Int => Some(IntVal::Reference(re.clone())),
156
                _ => None,
157
            },
158
40
            DeclarationKind::Quantified(inner) => match inner.domain().return_type() {
159
40
                ReturnType::Int => Some(IntVal::Reference(re.clone())),
160
                _ => None,
161
            },
162
            DeclarationKind::Find(var) => match var.return_type() {
163
                ReturnType::Int => Some(IntVal::Reference(re.clone())),
164
                _ => None,
165
            },
166
            DeclarationKind::DomainLetting(_) | DeclarationKind::RecordField(_) => None,
167
        }
168
916
    }
169

            
170
560
    pub fn new_expr(value: Moo<Expression>) -> Option<IntVal> {
171
560
        if value.return_type() != ReturnType::Int {
172
            return None;
173
560
        }
174
560
        Some(IntVal::Expr(value))
175
560
    }
176

            
177
150064
    pub fn resolve(&self) -> Option<Int> {
178
150064
        match self {
179
73832
            IntVal::Const(value) => Some(*value),
180
3820
            IntVal::Expr(expr) => eval_expr_to_int(expr),
181
72412
            IntVal::Reference(re) => match re.ptr.kind().deref() {
182
70452
                DeclarationKind::ValueLetting(expr, _)
183
71652
                | DeclarationKind::TemporaryValueLetting(expr) => eval_expr_to_int(expr),
184
                // If this is an int given we will be able to resolve it eventually, but not yet
185
                DeclarationKind::Given(_) => None,
186
                DeclarationKind::Quantified(inner) => {
187
                    if let Some(generator) = inner.generator()
188
                        && let Some(expr) = generator.as_value_letting()
189
                    {
190
                        eval_expr_to_int(&expr)
191
                    } else {
192
                        None
193
                    }
194
                }
195
                // Decision variables inside domains are unresolved until solving.
196
760
                DeclarationKind::Find(_) => None,
197
                DeclarationKind::DomainLetting(_) | DeclarationKind::RecordField(_) => bug!(
198
                    "Expected integer expression, given, or letting inside int domain; Got: {re}"
199
                ),
200
            },
201
        }
202
150064
    }
203
}
204

            
205
75472
fn eval_expr_to_int(expr: &Expression) -> Option<Int> {
206
75472
    match eval_constant(expr)? {
207
75032
        Literal::Int(v) => Some(v),
208
        _ => bug!("Expected integer expression, got: {expr}"),
209
    }
210
75472
}
211

            
212
impl From<IntVal> for Expression {
213
440
    fn from(value: IntVal) -> Self {
214
440
        match value {
215
160
            IntVal::Const(val) => val.into(),
216
180
            IntVal::Reference(re) => re.into(),
217
100
            IntVal::Expr(expr) => expr.as_ref().clone(),
218
        }
219
440
    }
220
}
221

            
222
impl From<IntVal> for Moo<Expression> {
223
    fn from(value: IntVal) -> Self {
224
        match value {
225
            IntVal::Const(val) => Moo::new(val.into()),
226
            IntVal::Reference(re) => Moo::new(re.into()),
227
            IntVal::Expr(expr) => expr,
228
        }
229
    }
230
}
231

            
232
impl std::ops::Neg for IntVal {
233
    type Output = IntVal;
234

            
235
960
    fn neg(self) -> Self::Output {
236
960
        match self {
237
960
            IntVal::Const(val) => IntVal::Const(-val),
238
            IntVal::Reference(_) | IntVal::Expr(_) => {
239
                IntVal::Expr(Moo::new(Expression::Neg(Metadata::new(), self.into())))
240
            }
241
        }
242
960
    }
243
}
244

            
245
impl<T> std::ops::Add<T> for IntVal
246
where
247
    T: Into<Expression>,
248
{
249
    type Output = IntVal;
250

            
251
    fn add(self, rhs: T) -> Self::Output {
252
        let lhs: Expression = self.into();
253
        let rhs: Expression = rhs.into();
254
        let sum = matrix_expr!(lhs, rhs; domain_int!(1..));
255
        IntVal::Expr(Moo::new(Expression::Sum(Metadata::new(), Moo::new(sum))))
256
    }
257
}
258

            
259
impl Range<IntVal> {
260
74372
    pub fn resolve(&self) -> Option<Range<Int>> {
261
74372
        match self {
262
            Range::Single(x) => Some(Range::Single(x.resolve()?)),
263
74372
            Range::Bounded(l, r) => Some(Range::Bounded(l.resolve()?, r.resolve()?)),
264
            Range::UnboundedL(r) => Some(Range::UnboundedL(r.resolve()?)),
265
            Range::UnboundedR(l) => Some(Range::UnboundedR(l.resolve()?)),
266
            Range::Unbounded => Some(Range::Unbounded),
267
        }
268
74372
    }
269
}
270

            
271
impl SetAttr<IntVal> {
272
    pub fn resolve(&self) -> Option<SetAttr<Int>> {
273
        Some(SetAttr {
274
            size: self.size.resolve()?,
275
        })
276
    }
277
}
278

            
279
impl MSetAttr<IntVal> {
280
    pub fn resolve(&self) -> Option<MSetAttr<Int>> {
281
        Some(MSetAttr {
282
            size: self.size.resolve()?,
283
            occurrence: self.occurrence.resolve()?,
284
        })
285
    }
286
}
287

            
288
impl FuncAttr<IntVal> {
289
    pub fn resolve(&self) -> Option<FuncAttr<Int>> {
290
        Some(FuncAttr {
291
            size: self.size.resolve()?,
292
            partiality: self.partiality.clone(),
293
            jectivity: self.jectivity.clone(),
294
        })
295
    }
296
}
297

            
298
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Uniplate, Quine)]
299
#[path_prefix(conjure_cp::ast)]
300
pub struct RecordEntry {
301
    pub name: Name,
302
    pub domain: DomainPtr,
303
}
304

            
305
impl RecordEntry {
306
    pub fn resolve(self) -> Option<RecordEntryGround> {
307
        Some(RecordEntryGround {
308
            name: self.name,
309
            domain: self.domain.resolve()?,
310
        })
311
    }
312
}
313

            
314
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Quine, Uniplate)]
315
#[path_prefix(conjure_cp::ast)]
316
#[biplate(to=Expression)]
317
#[biplate(to=Reference)]
318
#[biplate(to=IntVal)]
319
#[biplate(to=DomainPtr)]
320
#[biplate(to=RecordEntry)]
321
pub enum UnresolvedDomain {
322
    Int(Vec<Range<IntVal>>),
323
    /// A set of elements drawn from the inner domain
324
    Set(SetAttr<IntVal>, DomainPtr),
325
    MSet(MSetAttr<IntVal>, DomainPtr),
326
    /// A n-dimensional matrix with a value domain and n-index domains
327
    Matrix(DomainPtr, Vec<DomainPtr>),
328
    /// A tuple of N elements, each with its own domain
329
    Tuple(Vec<DomainPtr>),
330
    /// A reference to a domain letting
331
    #[polyquine_skip]
332
    Reference(Reference),
333
    /// A record
334
    Record(Vec<RecordEntry>),
335
    /// A function with attributes, domain, and range
336
    Function(FuncAttr<IntVal>, DomainPtr, DomainPtr),
337
}
338

            
339
impl UnresolvedDomain {
340
360676
    pub fn resolve(&self) -> Option<GroundDomain> {
341
360676
        match self {
342
74372
            UnresolvedDomain::Int(rngs) => rngs
343
74372
                .iter()
344
74372
                .map(Range::<IntVal>::resolve)
345
74372
                .collect::<Option<_>>()
346
74372
                .map(GroundDomain::Int),
347
            UnresolvedDomain::Set(attr, inner) => {
348
                Some(GroundDomain::Set(attr.resolve()?, inner.resolve()?))
349
            }
350
            UnresolvedDomain::MSet(attr, inner) => {
351
                Some(GroundDomain::MSet(attr.resolve()?, inner.resolve()?))
352
            }
353
110284
            UnresolvedDomain::Matrix(inner, idx_doms) => {
354
110284
                let inner_gd = inner.resolve()?;
355
110284
                idx_doms
356
110284
                    .iter()
357
110284
                    .map(DomainPtr::resolve)
358
110284
                    .collect::<Option<_>>()
359
110284
                    .map(|idx| GroundDomain::Matrix(inner_gd, idx))
360
            }
361
            UnresolvedDomain::Tuple(inners) => inners
362
                .iter()
363
                .map(DomainPtr::resolve)
364
                .collect::<Option<_>>()
365
                .map(GroundDomain::Tuple),
366
            UnresolvedDomain::Record(entries) => entries
367
                .iter()
368
                .map(|f| {
369
                    f.domain.resolve().map(|gd| RecordEntryGround {
370
                        name: f.name.clone(),
371
                        domain: gd,
372
                    })
373
                })
374
                .collect::<Option<_>>()
375
                .map(GroundDomain::Record),
376
176020
            UnresolvedDomain::Reference(re) => re
377
176020
                .ptr
378
176020
                .as_domain_letting()
379
176020
                .unwrap_or_else(|| {
380
                    bug!("Reference domain should point to domain letting, but got {re}")
381
                })
382
176020
                .resolve()
383
176020
                .map(Moo::unwrap_or_clone),
384
            UnresolvedDomain::Function(attr, dom, cdom) => {
385
                if let Some(attr_gd) = attr.resolve()
386
                    && let Some(dom_gd) = dom.resolve()
387
                    && let Some(cdom_gd) = cdom.resolve()
388
                {
389
                    return Some(GroundDomain::Function(attr_gd, dom_gd, cdom_gd));
390
                }
391
                None
392
            }
393
        }
394
360676
    }
395

            
396
    pub(super) fn union_unresolved(
397
        &self,
398
        other: &UnresolvedDomain,
399
    ) -> Result<UnresolvedDomain, DomainOpError> {
400
        match (self, other) {
401
            (UnresolvedDomain::Int(lhs), UnresolvedDomain::Int(rhs)) => {
402
                let merged = lhs.iter().chain(rhs.iter()).cloned().collect_vec();
403
                Ok(UnresolvedDomain::Int(merged))
404
            }
405
            (UnresolvedDomain::Int(_), _) | (_, UnresolvedDomain::Int(_)) => {
406
                Err(DomainOpError::WrongType)
407
            }
408
            (UnresolvedDomain::Set(_, in1), UnresolvedDomain::Set(_, in2)) => {
409
                Ok(UnresolvedDomain::Set(SetAttr::default(), in1.union(in2)?))
410
            }
411
            (UnresolvedDomain::Set(_, _), _) | (_, UnresolvedDomain::Set(_, _)) => {
412
                Err(DomainOpError::WrongType)
413
            }
414
            (UnresolvedDomain::MSet(_, in1), UnresolvedDomain::MSet(_, in2)) => {
415
                Ok(UnresolvedDomain::MSet(MSetAttr::default(), in1.union(in2)?))
416
            }
417
            (UnresolvedDomain::MSet(_, _), _) | (_, UnresolvedDomain::MSet(_, _)) => {
418
                Err(DomainOpError::WrongType)
419
            }
420
            (UnresolvedDomain::Matrix(in1, idx1), UnresolvedDomain::Matrix(in2, idx2))
421
                if idx1 == idx2 =>
422
            {
423
                Ok(UnresolvedDomain::Matrix(in1.union(in2)?, idx1.clone()))
424
            }
425
            (UnresolvedDomain::Matrix(_, _), _) | (_, UnresolvedDomain::Matrix(_, _)) => {
426
                Err(DomainOpError::WrongType)
427
            }
428
            (UnresolvedDomain::Tuple(lhs), UnresolvedDomain::Tuple(rhs))
429
                if lhs.len() == rhs.len() =>
430
            {
431
                let mut merged = Vec::new();
432
                for (l, r) in zip(lhs, rhs) {
433
                    merged.push(l.union(r)?)
434
                }
435
                Ok(UnresolvedDomain::Tuple(merged))
436
            }
437
            (UnresolvedDomain::Tuple(_), _) | (_, UnresolvedDomain::Tuple(_)) => {
438
                Err(DomainOpError::WrongType)
439
            }
440
            // TODO: Could we support unions of reference domains symbolically?
441
            (UnresolvedDomain::Reference(_), _) | (_, UnresolvedDomain::Reference(_)) => {
442
                Err(DomainOpError::NotGround)
443
            }
444
            // TODO: Could we define semantics for merging record domains?
445
            #[allow(unreachable_patterns)] // Technically redundant but logically makes sense
446
            (UnresolvedDomain::Record(_), _) | (_, UnresolvedDomain::Record(_)) => {
447
                Err(DomainOpError::WrongType)
448
            }
449
            #[allow(unreachable_patterns)]
450
            // Technically redundant but logically clearer to have both
451
            (UnresolvedDomain::Function(_, _, _), _) | (_, UnresolvedDomain::Function(_, _, _)) => {
452
                Err(DomainOpError::WrongType)
453
            }
454
        }
455
    }
456

            
457
    pub fn element_domain(&self) -> Option<DomainPtr> {
458
        match self {
459
            UnresolvedDomain::Set(_, inner_dom) => Some(inner_dom.clone()),
460
            UnresolvedDomain::Matrix(_, _) => {
461
                todo!("Unwrap one dimension of the domain")
462
            }
463
            _ => None,
464
        }
465
    }
466
}
467

            
468
impl Typeable for UnresolvedDomain {
469
28492
    fn return_type(&self) -> ReturnType {
470
28492
        match self {
471
5340
            UnresolvedDomain::Reference(re) => re.return_type(),
472
2024
            UnresolvedDomain::Int(_) => ReturnType::Int,
473
            UnresolvedDomain::Set(_attr, inner) => ReturnType::Set(Box::new(inner.return_type())),
474
            UnresolvedDomain::MSet(_attr, inner) => ReturnType::MSet(Box::new(inner.return_type())),
475
21128
            UnresolvedDomain::Matrix(inner, _idx) => {
476
21128
                ReturnType::Matrix(Box::new(inner.return_type()))
477
            }
478
            UnresolvedDomain::Tuple(inners) => {
479
                let mut inner_types = Vec::new();
480
                for inner in inners {
481
                    inner_types.push(inner.return_type());
482
                }
483
                ReturnType::Tuple(inner_types)
484
            }
485
            UnresolvedDomain::Record(entries) => {
486
                let mut entry_types = Vec::new();
487
                for entry in entries {
488
                    entry_types.push(entry.domain.return_type());
489
                }
490
                ReturnType::Record(entry_types)
491
            }
492
            UnresolvedDomain::Function(_, dom, cdom) => {
493
                ReturnType::Function(Box::new(dom.return_type()), Box::new(cdom.return_type()))
494
            }
495
        }
496
28492
    }
497
}
498

            
499
impl Display for UnresolvedDomain {
500
7937364
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
501
7937364
        match &self {
502
3990100
            UnresolvedDomain::Reference(re) => write!(f, "{re}"),
503
1734698
            UnresolvedDomain::Int(ranges) => {
504
1734698
                if ranges.iter().all(Range::is_lower_or_upper_bounded) {
505
1735058
                    let rngs: String = ranges.iter().map(|r| format!("{r}")).join(", ");
506
1734698
                    write!(f, "int({})", rngs)
507
                } else {
508
                    write!(f, "int")
509
                }
510
            }
511
            UnresolvedDomain::Set(attrs, inner_dom) => write!(f, "set {attrs} of {inner_dom}"),
512
            UnresolvedDomain::MSet(attrs, inner_dom) => write!(f, "mset {attrs} of {inner_dom}"),
513
2212566
            UnresolvedDomain::Matrix(value_domain, index_domains) => {
514
2212566
                write!(
515
2212566
                    f,
516
                    "matrix indexed by {} of {value_domain}",
517
2212566
                    pretty_vec(&index_domains.iter().collect_vec())
518
                )
519
            }
520
            UnresolvedDomain::Tuple(domains) => {
521
                write!(
522
                    f,
523
                    "tuple of ({})",
524
                    pretty_vec(&domains.iter().collect_vec())
525
                )
526
            }
527
            UnresolvedDomain::Record(entries) => {
528
                write!(
529
                    f,
530
                    "record of ({})",
531
                    pretty_vec(
532
                        &entries
533
                            .iter()
534
                            .map(|entry| format!("{}: {}", entry.name, entry.domain))
535
                            .collect_vec()
536
                    )
537
                )
538
            }
539
            UnresolvedDomain::Function(attribute, domain, codomain) => {
540
                write!(f, "function {} {} --> {} ", attribute, domain, codomain)
541
            }
542
        }
543
7937364
    }
544
}