Class: So::Exists

Inherits:
Expr show all
Defined in:
lib/spec_object.rb

Instance Method Summary collapse

Methods inherited from Expr

#!, #<, #==, #>, #[], #assert_time, #assert_value, #to_so_expr

Constructor Details

#initialize(variable, expr) ⇒ Exists

Returns a new instance of Exists.



286
287
288
289
290
291
# File 'lib/spec_object.rb', line 286

def initialize(variable, expr)
  raise "expected variable" unless variable.is_a?(Variable)

  @variable = variable
  @expr = expr.to_so_expr
end

Instance Method Details

#evaluate(calls) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/spec_object.rb', line 302

def evaluate(calls)
  if @variable.time?
    posibilities =
      (0...calls.size).map do |t|
        v = @expr.substitute(@variable, Time.new(t)).evaluate(calls)
      end

    t =
      posibilities.any? do |v|
        v.kind_of?(Const) && v.value
      end

    if t
      true.to_so_expr
    else
      f =
        posibilities.all? do |v|
          v.kind_of?(Const) && !(v.value)
        end

      if f
        false.to_so_expr
      else
        self
      end
    end
  elsif @variable.value?
    self
  else
    raise "cannot infer the type of #{@variable.pp(0)}"
  end
end

#pp(n) ⇒ Object



293
294
295
# File 'lib/spec_object.rb', line 293

def pp(n)
  "#{' '*n}(exists #{@variable.pp(0)}\n#{@expr.pp(n+2)})"
end

#substitute(v, e) ⇒ Object



297
298
299
300
# File 'lib/spec_object.rb', line 297

def substitute(v, e)
  raise "bad thing happ(n)ened" if v.object_id == @variable.object_id
  Exists.new(@variable, @expr.substitute(v, e))
end