Class: TypedRb::Types::TyGenericFunction

Inherits:
TyFunction show all
Defined in:
lib/typed/types/ty_generic_function.rb

Instance Attribute Summary collapse

Attributes inherited from TyFunction

#arity, #block_type, #from, #min_arity, #name, #parameters_info, #to

Attributes inherited from Type

#node

Instance Method Summary collapse

Methods inherited from TyFunction

#<=>, #apply_bindings, #arg_compatible?, #deconstruct_from_arguments, #dynamic?, #to_s, #with_block_type

Methods included from Comparable

#<=>

Methods inherited from Type

#either?, #stack_jump?

Constructor Details

#initialize(from, to, parameters_info = nil, node = nil) ⇒ TyGenericFunction

Returns a new instance of TyGenericFunction.



6
7
8
9
# File 'lib/typed/types/ty_generic_function.rb', line 6

def initialize(from, to, parameters_info = nil, node = nil)
  super(from, to, parameters_info, node)
  @application_count = 0
end

Instance Attribute Details

#local_typing_contextObject

Returns the value of attribute local_typing_context.



4
5
6
# File 'lib/typed/types/ty_generic_function.rb', line 4

def local_typing_context
  @local_typing_context
end

Instance Method Details

#check_args_application(actual_arguments, context) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/typed/types/ty_generic_function.rb', line 46

def check_args_application(actual_arguments, context)
  if @local_typing_context.kind != :lambda
    super(actual_arguments, context)
  else
    materialize do |materialized_function|
      materialized_function.check_args_application(actual_arguments, context)
    end
  end
end

#compatible?(other_type, relation = :lt) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/typed/types/ty_generic_function.rb', line 56

def compatible?(other_type, relation = :lt)
  materialize do |materialized_function|
    materialized_function.compatible?(other_type, relation)
  end
end

#free_type_variables(klass) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/typed/types/ty_generic_function.rb', line 34

def free_type_variables(klass)
  return type_variables if klass == :main
  class_type = Runtime::TypeParser.parse_singleton_object_type(klass.name)
  if class_type.generic?
    type_variables.reject do |type_var|
      class_type.type_vars.detect { |class_type_var| class_type_var.variable == type_var.variable }
    end
  else
    type_variables
  end
end

#generic?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/typed/types/ty_generic_function.rb', line 11

def generic?
  true
end

#materializeObject

Creates a new instance of a generic function with fresh type variables. Yields the function so new constraints can be added. Finally, it runs unification on the function typing context and returns the materialized function with the bound variables.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/typed/types/ty_generic_function.rb', line 19

def materialize
  TypedRb.log binding, :debug, "Materialising function '#{self}'"
  applied_typing_context, substitutions = create_context
  materialized_function = clone_with_substitutions(self, substitutions)
  TypingContext.with_context(applied_typing_context) do
    # Adding constraints for the generic vriables in the materialised function
    yield  materialized_function
  end
  # got all the constraints here
  # do something with the context -> unification? merge context?
  unification = Polymorphism::Unification.new(applied_typing_context.all_constraints).run
  applied_typing_context.unlink # these constraints have already been satisfied
  materialized_function.apply_bindings(unification.bindings_map)
end