Class: Steep::AST::Types::Intersection

Inherits:
Object
  • Object
show all
Includes:
Helper::ChildrenLevel
Defined in:
lib/steep/ast/types/intersection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper::ChildrenLevel

#level_of_children

Constructor Details

#initialize(types:) ⇒ Intersection

Returns a new instance of Intersection.



7
8
9
# File 'lib/steep/ast/types/intersection.rb', line 7

def initialize(types:)
  @types = types
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



5
6
7
# File 'lib/steep/ast/types/intersection.rb', line 5

def types
  @types
end

Class Method Details

.build(types:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/steep/ast/types/intersection.rb', line 11

def self.build(types:)
  types.flat_map do |type|
    if type.is_a?(Intersection)
      type.types
    else
      [type]
    end
  end.map do |type|
    case type
    when AST::Types::Any
      return AST::Types::Any.instance()
    when AST::Types::Bot
      return AST::Types::Bot.instance
    when AST::Types::Top
      nil
    else
      type
    end
  end.compact.yield_self do |tys|
    dups = Set.new(tys)

    case dups.size
    when 0
      AST::Types::Top.instance
    when 1
      tys.first || raise
    else
      new(types: dups.to_a)
    end
  end
end

Instance Method Details

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



43
44
45
# File 'lib/steep/ast/types/intersection.rb', line 43

def ==(other)
  other.is_a?(Intersection) && other.types == types
end

#each_child(&block) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/steep/ast/types/intersection.rb', line 73

def each_child(&block)
  if block
    types.each(&block)
  else
    types.each
  end
end

#free_variablesObject



61
62
63
64
65
66
67
68
69
# File 'lib/steep/ast/types/intersection.rb', line 61

def free_variables()
  @fvs ||= begin
             set = Set.new
             types.each do |type|
               set.merge(type.free_variables)
             end
             set
           end
end

#hashObject



47
48
49
# File 'lib/steep/ast/types/intersection.rb', line 47

def hash
  @hash ||= self.class.hash ^ types.hash
end

#levelObject



87
88
89
# File 'lib/steep/ast/types/intersection.rb', line 87

def level
  [0] + level_of_children(types)
end

#map_type(&block) ⇒ Object



81
82
83
84
85
# File 'lib/steep/ast/types/intersection.rb', line 81

def map_type(&block)
  self.class.build(
    types: each_child.map(&block)
  )
end

#subst(s) ⇒ Object



53
54
55
# File 'lib/steep/ast/types/intersection.rb', line 53

def subst(s)
  self.class.build(types: types.map {|ty| ty.subst(s) })
end

#to_sObject



57
58
59
# File 'lib/steep/ast/types/intersection.rb', line 57

def to_s
  "(#{types.map(&:to_s).join(" & ")})"
end