module TypedRb::Types::Polymorphism::GenericComparisons

Public Instance Methods

!=(other) click to toggle source
# File lib/typed/types/polymorphism/generic_comparisons.rb, line 11
def !=(other)
  ! (self == other)
end
<(other) click to toggle source
# File lib/typed/types/polymorphism/generic_comparisons.rb, line 15
def <(other)
  self.compatible?(other, :lt)
end
<=(other) click to toggle source
# File lib/typed/types/polymorphism/generic_comparisons.rb, line 23
def <=(other)
  self == other || self.compatible?(other, :lt)
end
==(other) click to toggle source
# File lib/typed/types/polymorphism/generic_comparisons.rb, line 5
def ==(other)
  return false if !other.is_a?(TyObject) || !other.generic?
  return false if other.ruby_type != ruby_type
  to_s == other.to_s
end
>(other) click to toggle source
# File lib/typed/types/polymorphism/generic_comparisons.rb, line 19
def >(other)
  self.compatible?(other, :gt)
end
>=(other) click to toggle source
# File lib/typed/types/polymorphism/generic_comparisons.rb, line 27
def >=(other)
  self == other || self.compatible?(other, :gt)
end
add_type_var_constraint(type_var, other_type_var, relation) click to toggle source
# File lib/typed/types/polymorphism/generic_comparisons.rb, line 136
def add_type_var_constraint(type_var, other_type_var, relation)
  if type_var.bound
    type_var, other_type_var = other_type_var, type_var
    #relation = relation == :lt ? :gt : :lt
  end
  [:lt, :gt].each do |relation|
    type_var.add_constraint(relation, other_type_var.bound)
  end
end
check_generic_type_relation(other_type, relation) click to toggle source
# File lib/typed/types/polymorphism/generic_comparisons.rb, line 82
def check_generic_type_relation(other_type, relation)
  if relation == :gt
    to_ty_object(self)  >= to_ty_object(other_type)
  else
    to_ty_object(self)  <= to_ty_object(other_type)
  end
end
check_inferior_or_equal_binding(binding_a, binding_b) click to toggle source

nil < Type_i < Object

# File lib/typed/types/polymorphism/generic_comparisons.rb, line 124
def check_inferior_or_equal_binding(binding_a, binding_b)
  if binding_a.nil? && binding_b.nil?
    true
  elsif binding_a.nil? && !binding_b.nil?
    true
  elsif !binding_a.nil? && binding_b.nil?
    false
  else
    binding_a <= binding_b
  end
end
check_type_var_inclusion(type_var, other_type_var, relation) click to toggle source
# File lib/typed/types/polymorphism/generic_comparisons.rb, line 101
def check_type_var_inclusion(type_var, other_type_var, relation)
  if (!type_var.wildcard? && !type_var.fully_bound?) ||
     (!other_type_var.wildcard? && !other_type_var.fully_bound?)
    add_type_var_constraint(type_var, other_type_var, relation)
  else
    superset, subset = relation == :lt ? [other_type_var, type_var] : [type_var, other_type_var]

    super_min, super_max, sub_min, sub_max = [superset.lower_bound, superset.upper_bound, subset.lower_bound, subset.upper_bound]. map do |bound|
      if bound.nil?
        nil
      else
        bound
      end
    end
    super_max ||= TyObject.new(Object)
    sub_max ||= TyObject.new(Object)

    check_inferior_or_equal_binding(super_min, sub_min) &&
      check_inferior_or_equal_binding(sub_max, super_max)
  end
end
compatible?(other_type, relation = :lt) click to toggle source
# File lib/typed/types/polymorphism/generic_comparisons.rb, line 31
def compatible?(other_type, relation = :lt)
  if other_type.is_a?(TyDynamic) || other_type.is_a?(TyError)
    true
  elsif other_type.is_a?(TyGenericObject) || other_type.is_a?(TyGenericSingletonObject) || other_type.is_a?(TyGenericExistentialType)
    if check_generic_type_relation(other_type, relation)
      type_vars.each_with_index do |type_var, i|
        other_type_var = other_type.type_vars[i]
        compatible = if incompatible_free_type_vars?(type_var, other_type_var)
                       false
                     elsif compatible_free_type_vars?(type_var, other_type_var)
                       true
                     else
                       check_type_var_inclusion(type_var, other_type_var, relation)
                     end
        return false unless compatible
      end
      true
    else
      false
    end
  elsif other_type.is_a?(TyObject)
    check_generic_type_relation(other_type, relation)
  else
    other_type.compatible?(self, relation == :lt ? :gt : :lt)
  end
rescue ArgumentError
  raise TypedRb::Types::UncomparableTypes.new(self, other_type)
end
compatible_free_type_vars?(type_var, other_type_var) click to toggle source
# File lib/typed/types/polymorphism/generic_comparisons.rb, line 71
def compatible_free_type_vars?(type_var, other_type_var)
  left_var = type_var.bound || type_var.lower_bound || type_var.upper_bound || type_var
  right_var = other_type_var.bound || other_type_var.lower_bound || other_type_var.upper_bound || other_type_var

  left_var.is_a?(Polymorphism::TypeVariable) &&
    right_var.is_a?(Polymorphism::TypeVariable) &&
    left_var.variable == right_var.variable &&
    (TypingContext.bound_generic_type_var?(left_var) &&
     TypingContext.bound_generic_type_var?(right_var))
end
incompatible_free_type_vars?(type_var, other_type_var) click to toggle source
# File lib/typed/types/polymorphism/generic_comparisons.rb, line 60
def incompatible_free_type_vars?(type_var, other_type_var)
  left_var = type_var.bound || type_var.lower_bound || type_var.upper_bound || type_var
  right_var = other_type_var.bound || other_type_var.lower_bound || other_type_var.upper_bound || other_type_var

  left_var.is_a?(Polymorphism::TypeVariable) &&
    right_var.is_a?(Polymorphism::TypeVariable) &&
    left_var.variable != right_var.variable &&
    (TypingContext.bound_generic_type_var?(left_var) &&
     TypingContext.bound_generic_type_var?(right_var))
end
to_ty_object(type) click to toggle source
# File lib/typed/types/polymorphism/generic_comparisons.rb, line 90
def to_ty_object(type)
  ty_object = TyObject.new(type.ruby_type)
  if type.is_a?(Types::TySingletonObject) ||
     type.is_a?(Types::TyGenericSingletonObject) ||
     type.is_a?(Types::TyExistentialType) ||
     type.is_a?(Types::TyGenericExistentialType)
    ty_object.hierarchy = type.ruby_type.meta_ancestors
  end
  ty_object
end