Module: Jade::Frontend::TypeChecking::Generalizer

Extended by:
Generalizer
Includes:
Inference::Helpers
Included in:
Generalizer
Defined in:
lib/jade/frontend/type_checking/generalizer.rb

Instance Method Summary collapse

Methods included from Inference::Helpers

#check, #instantiate, #type_from_symbol, #unify

Instance Method Details

#generalize(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jade/frontend/type_checking/generalizer.rb', line 8

def generalize(env)
  env
    .bindings
    .reduce(env) do |e, (k, binding)|
      case binding
      in Placeholder(type:, constraints:)
        unbound_cs = constraints
          .map { e.substitution.apply(it) }
          .uniq
          .select { it.unbound_vars.any? }

        Generalization.
          generalize(
            # Remove the current placeholder and all Scheme bindings (local
            # params/variables) so they don't pollute free_vars during
            # generalization. Only other Placeholders contribute free_vars.
            e.with(bindings: e.bindings.except(k).select { |_, v| v.is_a?(Placeholder) }),
            e.substitution.apply(type),
            unbound_cs,
          )
          .then { e.bind(k, it) }
      else

        next e
      end
    end
end