Class: Rigor::Trinary

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

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 =

Returns:

%i[yes no maybe].freeze
YES =

Returns:

NO =

Returns:

MAYBE =

Returns:

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.

Parameters:

Raises:

  • (ArgumentError)


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

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

  @value = value
end

Class Attribute Details

.maybeTrinary (readonly)

Returns the value of attribute maybe.

Returns:



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

def maybe
  @maybe
end

.noTrinary (readonly)

Returns the value of attribute no.

Returns:



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

def no
  @no
end

.yesTrinary (readonly)

Returns the value of attribute yes.

Returns:



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

def yes
  @yes
end

Instance Attribute Details

#valuevalue (readonly)

Returns the value of attribute value.

Returns:



31
32
33
# File 'lib/rigor/trinary.rb', line 31

def value
  @value
end

Class Method Details

.from_symbol(symbol) ⇒ Trinary

Parameters:

  • symbol (Symbol)

Returns:



20
21
22
23
24
25
26
27
28
# File 'lib/rigor/trinary.rb', line 20

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) ⇒ Boolean Also known as: eql?

Parameters:

  • other (Object)

Returns:

  • (Boolean)


77
78
79
# File 'lib/rigor/trinary.rb', line 77

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

#and(other) ⇒ Trinary

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

Parameters:

Returns:



60
61
62
63
64
65
66
# File 'lib/rigor/trinary.rb', line 60

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

#hashInteger

Returns:

  • (Integer)


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

def hash
  value.hash
end

#inspectString

Returns:

  • (String)


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

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

#maybe?Boolean

Returns:

  • (Boolean)


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

def maybe?
  value == :maybe
end

#negateTrinary

Returns:



51
52
53
54
55
56
57
# File 'lib/rigor/trinary.rb', line 51

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)


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

def no?
  value == :no
end

#or(other) ⇒ Trinary

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

Parameters:

Returns:



69
70
71
72
73
74
75
# File 'lib/rigor/trinary.rb', line 69

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_sString

Returns:

  • (String)


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

def to_s
  value.to_s
end

#yes?Boolean

Returns:

  • (Boolean)


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

def yes?
  value == :yes
end