Class: Typisch::Type::Tuple

Inherits:
Constructor show all
Defined in:
lib/typisch/tuple.rb

Constant Summary

Constants inherited from Constructor

Constructor::CONSTRUCTOR_TYPE_SUBCLASSES

Instance Attribute Summary collapse

Attributes inherited from Typisch::Type

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Constructor

#alternative_types, inherited, #type_lattice

Methods inherited from Typisch::Type

#<, #<=, #<=>, #==, #===, #>, #>=, #alternative_types, #annotations, #annotations=, #excluding_null, #inspect, #recursive?, subtype?, #target, #to_s

Constructor Details

#initialize(*types) ⇒ Tuple

Returns a new instance of Tuple.



31
32
33
# File 'lib/typisch/tuple.rb', line 31

def initialize(*types)
  @types = types
end

Instance Attribute Details

#typesObject (readonly) Also known as: subexpression_types

Returns the value of attribute types.



49
50
51
# File 'lib/typisch/tuple.rb', line 49

def types
  @types
end

Class Method Details

.check_subtype(x, y, &recursively_check_subtype) ⇒ Object



8
9
10
11
12
# File 'lib/typisch/tuple.rb', line 8

def check_subtype(x, y, &recursively_check_subtype)
  if x.length >= y.length
    (0...y.length).all? {|i| recursively_check_subtype[x[i], y[i]]}
  end
end

.least_upper_bounds_for_union(*tuples) ⇒ Object

This l.u.b. isn’t as tight as it could be; (Int, Int) union (Bool, Bool) is a strict subset of (Int union Bool, Int union Bool), see eg (1, true).

It makes life simpler to do it this way though; if you want to keep the distinction, try using Object types with different type tags.



21
22
23
24
25
26
27
# File 'lib/typisch/tuple.rb', line 21

def least_upper_bounds_for_union(*tuples)
  min_length = tuples.map(&:length).min
  unions = Array.new(min_length) do |i|
    Type::Union.union(*tuples.map {|t| t[i]})
  end
  [new(*unions)]
end

.top_type(overall_top) ⇒ Object



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

def top_type(overall_top)
  new()
end

Instance Method Details

#[](n) ⇒ Object



56
57
58
# File 'lib/typisch/tuple.rb', line 56

def [](n)
  @types[n]
end

#canonicalize!Object



70
71
72
# File 'lib/typisch/tuple.rb', line 70

def canonicalize!
  @types.map!(&:target)
end

#check_type(instance, &recursively_check_type) ⇒ Object

For now we’re only allowing Array as a tuple. We could allow any Enumerable, say, but a tuple is really not supposed to be in any way a lazy data structure, it’s something of fixed (usually short) length.



38
39
40
41
42
# File 'lib/typisch/tuple.rb', line 38

def check_type(instance, &recursively_check_type)
  ::Array === instance &&
  instance.length == @types.length &&
  @types.zip(instance).all?(&recursively_check_type)
end

#lengthObject



52
53
54
# File 'lib/typisch/tuple.rb', line 52

def length
  @types.length
end

#shallow_check_type(instance) ⇒ Object



44
45
46
# File 'lib/typisch/tuple.rb', line 44

def shallow_check_type(instance)
  ::Array === instance && instance.length == @types.length
end

#tagObject



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

def tag
  "Tuple"
end

#to_string(depth, indent) ⇒ Object



64
65
66
67
68
# File 'lib/typisch/tuple.rb', line 64

def to_string(depth, indent)
  next_indent = "#{indent}  "
  types = @types.map {|t| t.to_s(depth+1, next_indent)}
  "tuple(\n#{next_indent}#{types.join(",\n#{next_indent}")}\n#{indent})"
end