Class: Rigor::Type::Union

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

Overview

A normalized non-empty union of two or more distinct types. Unions are constructed exclusively through Rigor::Type::Combinator.union, which flattens nested unions, deduplicates structurally-equal members, and collapses single-member or empty results to the appropriate scalar type. Direct calls to .new are an internal contract: callers MUST pass an already-normalized members array.

See docs/type-specification/normalization.md.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(members) ⇒ Union

Returns a new instance of Union.



18
19
20
21
22
23
24
25
# File 'lib/rigor/type/union.rb', line 18

def initialize(members)
  unless members.is_a?(Array) && members.size >= 2
    raise ArgumentError, "Union requires at least two members; use Combinator.union for normalization"
  end

  @members = members.freeze
  freeze
end

Instance Attribute Details

#membersObject (readonly)

Returns the value of attribute members.



16
17
18
# File 'lib/rigor/type/union.rb', line 16

def members
  @members
end

Instance Method Details

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



70
71
72
# File 'lib/rigor/type/union.rb', line 70

def ==(other)
  other.is_a?(Union) && members == other.members
end

#accepts(other, mode: :gradual) ⇒ Object



66
67
68
# File 'lib/rigor/type/union.rb', line 66

def accepts(other, mode: :gradual)
  Inference::Acceptance.accepts(self, other, mode: mode)
end

#botObject



58
59
60
# File 'lib/rigor/type/union.rb', line 58

def bot
  Trinary.no
end

#describe(verbosity = :short) ⇒ Object



27
28
29
# File 'lib/rigor/type/union.rb', line 27

def describe(verbosity = :short)
  members.map { |m| m.describe(verbosity) }.join(" | ")
end

#dynamicObject



62
63
64
# File 'lib/rigor/type/union.rb', line 62

def dynamic
  members.any? { |m| m.respond_to?(:dynamic) && m.dynamic.yes? } ? Trinary.maybe : Trinary.no
end

#erase_to_rbsObject

ADR-1 § “RBS round-trip is lossless” + the value-lattice rule ‘untyped | T = untyped` (every `T` is gradually consistent with `untyped`). When any union member erases to `“untyped”`, the whole union erases to `“untyped”` —the RBS surface has no carrier for “Dynamic-origin alongside a static facet”, and the gradual-consistency contract guarantees the substitution is sound at every call site.

Post-erasure dedupe removes ‘String | String` artefacts that arise when two structurally-distinct `Constant` carriers (e.g. `Constant<“Alice”>` / `Constant<“Bob”>`) share an RBS-erased envelope. The members themselves are already structurally deduped at construction by `Type::Combinator.union`, but the post-erase strings can collide.



47
48
49
50
51
52
# File 'lib/rigor/type/union.rb', line 47

def erase_to_rbs
  erased = members.map(&:erase_to_rbs)
  return "untyped" if erased.include?("untyped")

  erased.uniq.join(" | ")
end

#hashObject



75
76
77
# File 'lib/rigor/type/union.rb', line 75

def hash
  [Union, members].hash
end

#inspectObject



79
80
81
# File 'lib/rigor/type/union.rb', line 79

def inspect
  "#<Rigor::Type::Union #{describe(:short)}>"
end

#topObject



54
55
56
# File 'lib/rigor/type/union.rb', line 54

def top
  Trinary.no
end