class TypedRb::Model::TmMlhs

Deconstructing arg variables

Attributes

args[RW]
lambda_args[RW]

Public Class Methods

new(args, node) click to toggle source
Calls superclass method TypedRb::Model::Expr::new
# File lib/typed/model/tm_mlhs.rb, line 9
def initialize(args, node)
  super(node)
  @args = args
end

Public Instance Methods

check_type(actual_argument, context) click to toggle source
# File lib/typed/model/tm_mlhs.rb, line 14
def check_type(actual_argument, context)
  return process_lambda_args(context) if actual_argument == :lambda
  if pair_argument?(actual_argument)
    process_pair(actual_argument, context)
  elsif array_argument?(actual_argument)
    process_array(actual_argument, context)
  else
    fail TypeCheckError.new("Error type checking function MLHS term: Type is not subtype of Array:  #{actual_argument}", node)
  end
end
compatible?(other_type, relation = :lt) click to toggle source
# File lib/typed/model/tm_mlhs.rb, line 25
def compatible?(other_type, relation = :lt)
  if other_type.generic? && other_type.ruby_type.ancestors.include?(Array)
    if other_type.type_vars.size == 1
      @lambda_args.each do |lambda_arg|
        lambda_arg.compatible?(other_type.type_vars.first, relation)
      end
    elsif other_type.type_vars.size == @lambda_args.size
      @lambda_args.each_with_index do |lambda_arg, i|
        lambda_arg.compatible?(other_type.type_vars[i], relation)
      end
    else
      false
    end
  else
    false
  end
end

Protected Instance Methods

array_argument?(argument) click to toggle source
# File lib/typed/model/tm_mlhs.rb, line 76
def array_argument?(argument)
  argument.is_a?(Types::TyGenericObject) &&
    argument.ruby_type.ancestors.include?(Array)
end
pair_argument?(argument) click to toggle source
# File lib/typed/model/tm_mlhs.rb, line 81
def pair_argument?(argument)
  argument.is_a?(Types::TyGenericObject) &&
    argument.ruby_type.ancestors.include?(Pair)
end
process_array(actual_argument, context) click to toggle source
# File lib/typed/model/tm_mlhs.rb, line 55
def process_array(actual_argument, context)
  type_var = actual_argument.type_vars[0]
  args.each { |arg| context = context.add_binding(arg, type_var) }
  context
end
process_lambda_args(context) click to toggle source
# File lib/typed/model/tm_mlhs.rb, line 45
def process_lambda_args(context)
  @lambda_args = args.map do |arg|
    type_var = Types::TypingContext.type_variable_for_abstraction(:lambda, arg.to_s, context)
    type_var.node = node
    context = context.add_binding(arg, type_var)
    type_var
  end
  context
end
process_pair(actual_argument, context) click to toggle source
# File lib/typed/model/tm_mlhs.rb, line 61
def process_pair(actual_argument, context)
  args.each_with_index do |arg, i|
    type = case i
           when 0
             actual_argument.type_vars[0]
           when 1
             actual_argument.type_vars[1]
           else
             Types::TyUnit.new(node)
           end
    context = context.add_binding(arg, type)
  end
  context
end