Class: Rigor::Trinary

Inherits:
Object
  • Object
show all
Defined in:
lib/rigor/trinary.rb

Overview

Three-valued logic value object shared by capability queries, relational queries, and any analyzer surface that distinguishes “proven yes”, “proven no”, and “cannot prove either”.

See docs/type-specification/relations-and-certainty.md for semantics and docs/internal-spec/internal-type-api.md for the contract.

Constant Summary collapse

VALUES =
%i[yes no maybe].freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Trinary

Returns a new instance of Trinary.

Raises:

  • (ArgumentError)


37
38
39
40
41
# File 'lib/rigor/trinary.rb', line 37

def initialize(value)
  raise ArgumentError, "unknown trinary value: #{value.inspect}" unless VALUES.include?(value)

  @value = value
end

Class Attribute Details

.maybeObject (readonly)

Returns the value of attribute maybe.



22
23
24
# File 'lib/rigor/trinary.rb', line 22

def maybe
  @maybe
end

.noObject (readonly)

Returns the value of attribute no.



22
23
24
# File 'lib/rigor/trinary.rb', line 22

def no
  @no
end

.yesObject (readonly)

Returns the value of attribute yes.



22
23
24
# File 'lib/rigor/trinary.rb', line 22

def yes
  @yes
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



35
36
37
# File 'lib/rigor/trinary.rb', line 35

def value
  @value
end

Class Method Details

.from_symbol(symbol) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/rigor/trinary.rb', line 24

def from_symbol(symbol)
  case symbol
  when :yes then yes
  when :no then no
  when :maybe then maybe
  else
    raise ArgumentError, "unknown trinary value: #{symbol.inspect}"
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



81
82
83
# File 'lib/rigor/trinary.rb', line 81

def ==(other)
  other.is_a?(Trinary) && value == other.value
end

#and(other) ⇒ Object

Conjunction. yes & yes = yes, no with anything = no, otherwise maybe.



64
65
66
67
68
69
70
# File 'lib/rigor/trinary.rb', line 64

def and(other)
  coerced = coerce(other)
  return self.class.no if no? || coerced.no?
  return self.class.yes if yes? && coerced.yes?

  self.class.maybe
end

#hashObject



86
87
88
# File 'lib/rigor/trinary.rb', line 86

def hash
  value.hash
end

#inspectObject



94
95
96
# File 'lib/rigor/trinary.rb', line 94

def inspect
  "#<Rigor::Trinary #{value}>"
end

#maybe?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/rigor/trinary.rb', line 51

def maybe?
  value == :maybe
end

#negateObject



55
56
57
58
59
60
61
# File 'lib/rigor/trinary.rb', line 55

def negate
  case value
  when :yes then self.class.no
  when :no then self.class.yes
  else self.class.maybe
  end
end

#no?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/rigor/trinary.rb', line 47

def no?
  value == :no
end

#or(other) ⇒ Object

Disjunction. yes with anything = yes, no & no = no, otherwise maybe.



73
74
75
76
77
78
79
# File 'lib/rigor/trinary.rb', line 73

def or(other)
  coerced = coerce(other)
  return self.class.yes if yes? || coerced.yes?
  return self.class.no if no? && coerced.no?

  self.class.maybe
end

#to_sObject



90
91
92
# File 'lib/rigor/trinary.rb', line 90

def to_s
  value.to_s
end

#yes?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rigor/trinary.rb', line 43

def yes?
  value == :yes
end