jump-dev / Convex.jl

A Julia package for disciplined convex programming
https://jump.dev/Convex.jl/stable/
Other
557 stars 119 forks source link

Support chained inequalities? #651

Closed ericphanson closed 1 month ago

ericphanson commented 1 month ago
julia> x = Variable()
Variable
size: (1, 1)
sign: real
vexity: affine
id: 970…293

julia> 0 <= x <= 1
ERROR: TypeError: non-boolean (Convex.GenericConstraint{MathOptInterface.Nonpositives}) used in boolean context
Stacktrace:
 [1] top-level scope
   @ REPL[44]:1

It would be nice if this worked.

ericphanson commented 1 month ago

I actually don't think this is possible (outside of a macro):

julia> Meta.@lower 1 <= x <= 1
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = 1 <= x
└──      goto #3 if not %1
2 ─ %3 = x <= 1
└──      return %3
3 ─      return false
))))

This syntax seems to lower directly to a short-circuiting expression, meaning we have no way to hook into it.