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
|
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Guido Tack <guido.tack@monash.edu>
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <minizinc/flat_exp.hh>
namespace MiniZinc {
EE flatten_let(EnvI& env, const Ctx& ctx, Expression* e, VarDecl* r, VarDecl* b) {
CallStackItem _csi(env, e);
EE ret;
Let* let = Expression::cast<Let>(e);
std::vector<EE> cs;
std::vector<KeepAlive> flatmap;
{
LetPushBindings lpb(let);
for (unsigned int i = 0; i < let->let().size(); i++) {
Expression* le = let->let()[i];
if (auto* vd = Expression::dynamicCast<VarDecl>(le)) {
Expression* let_e = nullptr;
if (vd->e() != nullptr) {
Ctx nctx = ctx;
BCtx transfer_ctx = let->type().bt() == Type::BT_INT ? nctx.i : nctx.b;
nctx.neg = false;
if (Expression::ann(vd).contains(env.constants.ctx.promise_monotone)) {
if (Expression::type(vd->e()).bt() == Type::BT_BOOL) {
nctx.b = +transfer_ctx;
} else {
nctx.i = +transfer_ctx;
}
} else if (Expression::ann(vd).contains(env.constants.ctx.promise_antitone)) {
if (Expression::type(vd->e()).bt() == Type::BT_BOOL) {
nctx.b = -transfer_ctx;
} else {
nctx.i = -transfer_ctx;
}
} else if (Expression::type(vd->e()).bt() == Type::BT_BOOL) {
nctx.b = C_MIX;
}
CallStackItem csi_vd(env, vd);
EE ee = flat_exp(env, nctx, vd->e(), nullptr, nctx.partialityVar(env));
let_e = ee.r();
cs.push_back(ee);
check_index_sets(env, vd, let_e);
if (vd->ti()->domain() != nullptr) {
GCLock lock;
auto* c = mk_domain_constraint(env, ee.r(), vd->ti()->domain());
if (c != nullptr) {
VarDecl* b_b = (nctx.b == C_ROOT && b == env.constants.varTrue) ? b : nullptr;
VarDecl* r_r = (nctx.b == C_ROOT && b == env.constants.varTrue) ? b : nullptr;
ee = flat_exp(env, nctx, c, r_r, b_b);
cs.push_back(ee);
ee.b = ee.r;
cs.push_back(ee);
}
}
vd->e(let_e);
flatten_vardecl_annotations(env, vd, nullptr, vd);
} else {
if ((ctx.b == C_NEG || ctx.b == C_MIX) &&
!Expression::ann(vd).contains(env.constants.ann.promise_total)) {
CallStackItem csi_vd(env, vd);
throw FlatteningError(env, Expression::loc(vd),
"free variable in non-positive context");
}
CallStackItem csi_vd(env, vd);
GCLock lock;
TypeInst* ti = eval_typeinst(env, ctx, vd);
VarDecl* nvd = new_vardecl(env, ctx, ti, nullptr, vd, nullptr);
let_e = nvd->id();
vd->e(let_e);
}
flatmap.emplace_back(vd->flat());
if (Id* id = Expression::dynamicCast<Id>(let_e)) {
vd->flat(id->decl());
} else {
vd->flat(vd);
}
} else {
if (ctx.b == C_ROOT || Expression::ann(le).contains(env.constants.ann.promise_total)) {
(void)flat_exp(env, Ctx(), le, env.constants.varTrue, env.constants.varTrue);
} else {
Ctx nctx = ctx;
nctx.neg = false;
EE ee = flat_exp(env, nctx, le, nullptr, env.constants.varTrue);
ee.b = ee.r;
cs.push_back(ee);
}
}
}
if (r == env.constants.varTrue && ctx.b == C_ROOT && !ctx.neg) {
ret.b = bind(env, Ctx(), b, env.constants.literalTrue);
(void)flat_exp(env, ctx, let->in(), r, b);
ret.r = conj(env, r, Ctx(), cs);
} else {
Ctx nctx = ctx;
nctx.neg = false;
VarDecl* bb = b;
for (EE& ee : cs) {
if (ee.b() != env.constants.literalTrue) {
bb = nullptr;
break;
}
}
EE ee = flat_exp(env, nctx, let->in(), nullptr, bb);
if (let->type().isbool() && !let->type().isOpt()) {
ee.b = ee.r;
cs.push_back(ee);
ret.r = conj(env, r, ctx, cs);
ret.b = bind(env, Ctx(), b, env.constants.literalTrue);
} else {
cs.push_back(ee);
ret.r = bind(env, Ctx(), r, ee.r());
ret.b = conj(env, b, Ctx(), cs);
}
}
}
// Restore previous mapping
for (unsigned int i = 0, j = 0; i < let->let().size(); i++) {
if (auto* vd = Expression::dynamicCast<VarDecl>(let->let()[i])) {
vd->flat(Expression::cast<VarDecl>(flatmap[j++]()));
}
}
return ret;
}
} // namespace MiniZinc
|