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

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)


39
40
41
42
43
# File 'lib/rigor/trinary.rb', line 39

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

  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Class Method Details

.from_symbol(symbol) ⇒ Object



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

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

.maybeObject



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

def maybe
  @maybe ||= new(:maybe).freeze
end

.noObject



18
19
20
# File 'lib/rigor/trinary.rb', line 18

def no
  @no ||= new(:no).freeze
end

.yesObject



14
15
16
# File 'lib/rigor/trinary.rb', line 14

def yes
  @yes ||= new(:yes).freeze
end

Instance Method Details

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



83
84
85
# File 'lib/rigor/trinary.rb', line 83

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

#and(other) ⇒ Object

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



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

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



88
89
90
# File 'lib/rigor/trinary.rb', line 88

def hash
  value.hash
end

#inspectObject



96
97
98
# File 'lib/rigor/trinary.rb', line 96

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

#maybe?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/rigor/trinary.rb', line 53

def maybe?
  value == :maybe
end

#negateObject



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

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

#no?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/rigor/trinary.rb', line 49

def no?
  value == :no
end

#or(other) ⇒ Object

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



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

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



92
93
94
# File 'lib/rigor/trinary.rb', line 92

def to_s
  value.to_s
end

#yes?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/rigor/trinary.rb', line 45

def yes?
  value == :yes
end