Class: RDL::Type::TupleType

Inherits:
Type
  • Object
show all
Defined in:
lib/rdl/types/tuple.rb

Overview

A specialized GenericType for tuples, i.e., fixed-sized arrays

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

leq, #nil_type?, #optional?, #to_contract, #vararg?

Constructor Details

#initialize(*params) ⇒ TupleType

no caching because array might be mutated

Raises:

  • (RuntimeError)


10
11
12
13
14
15
16
17
18
# File 'lib/rdl/types/tuple.rb', line 10

def initialize(*params)
  raise RuntimeError, "Attempt to create tuple type with non-type param" unless params.all? { |p| p.is_a? Type }
  @params = params
  @array = nil # emphasize initially this is a tuple, not an array
  @cant_promote = false
  @ubounds = []
  @lbounds = []
  super()
end

Instance Attribute Details

#arrayObject (readonly)

either nil or array type if self has been promoted to array



5
6
7
# File 'lib/rdl/types/tuple.rb', line 5

def array
  @array
end

#lboundsObject

lower bounds…



7
8
9
# File 'lib/rdl/types/tuple.rb', line 7

def lbounds
  @lbounds
end

#paramsObject (readonly)

Returns the value of attribute params.



4
5
6
# File 'lib/rdl/types/tuple.rb', line 4

def params
  @params
end

#uboundsObject

upper bounds this tuple has been compared with using <=



6
7
8
# File 'lib/rdl/types/tuple.rb', line 6

def ubounds
  @ubounds
end

Instance Method Details

#<=(other) ⇒ Object



60
61
62
# File 'lib/rdl/types/tuple.rb', line 60

def <=(other)
  return Type.leq(self, other)
end

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

:nodoc:



30
31
32
33
34
35
# File 'lib/rdl/types/tuple.rb', line 30

def ==(other) # :nodoc:
  return false if other.nil?
  return (@array == other) if @array
  other = other.canonical
  return (other.instance_of? TupleType) && (other.params == @params)
end

#canonicalObject



20
21
22
23
# File 'lib/rdl/types/tuple.rb', line 20

def canonical
  return @array if @array
  return self
end

#cant_promote!Object

Raises:

  • (RuntimeError)


55
56
57
58
# File 'lib/rdl/types/tuple.rb', line 55

def cant_promote!
  raise RuntimeError, "already promoted!" if @array
  @cant_promote = true
end

#hashObject



77
78
79
80
# File 'lib/rdl/types/tuple.rb', line 77

def hash
  # note don't change hash value if @array becomes non-nil
  73 * @params.hash
end

#instantiate(inst) ⇒ Object



72
73
74
75
# File 'lib/rdl/types/tuple.rb', line 72

def instantiate(inst)
  return @array.instantiate(inst) if @array
  return TupleType.new(*@params.map { |t| t.instantiate(inst) })
end

#match(other) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/rdl/types/tuple.rb', line 39

def match(other)
  return @array.match(other) if @array
  other = other.canonical
  other = other.type if other.instance_of? AnnotatedArgType
  return true if other.instance_of? WildQuery
  return (other.instance_of? TupleType) && (@params.length == other.params.length) && (@params.zip(other.params).all? { |t,o| t.match(o) })
end

#member?(obj, *args) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
# File 'lib/rdl/types/tuple.rb', line 64

def member?(obj, *args)
  return @array.member?(obj, *args) if @array
  t = RDL::Util.rdl_type obj
  return t <= self if t
  return false unless obj.instance_of?(Array) && obj.size == @params.size
  return @params.zip(obj).all? { |formal, actual| formal.member?(actual, *args) }
end

#promote!Object



47
48
49
50
51
52
53
# File 'lib/rdl/types/tuple.rb', line 47

def promote!
  return false if @cant_promote
  @array = GenericType.new(RDL::Globals.types[:array], UnionType.new(*@params))
  # note since we promoted this, lbounds and ubounds will be ignored in future constraints, which
  # is good because otherwise we'd get infinite loops
  return (@lbounds.all? { |lbound| lbound <= self }) && (@ubounds.all? { |ubound| self <= ubound })
end

#to_sObject



25
26
27
28
# File 'lib/rdl/types/tuple.rb', line 25

def to_s
  return @array.to_s if @array
  return "[#{@params.map { |t| t.to_s }.join(', ')}]"
end