Class: Jade::Frontend::TypeChecking::Env

Inherits:
Data
  • Object
show all
Defined in:
lib/jade/frontend/type_checking/env.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bindingsObject (readonly)

Returns the value of attribute bindings

Returns:

  • (Object)

    the current value of bindings



8
9
10
# File 'lib/jade/frontend/type_checking/env.rb', line 8

def bindings
  @bindings
end

#definitionsObject (readonly)

Returns the value of attribute definitions

Returns:

  • (Object)

    the current value of definitions



8
9
10
# File 'lib/jade/frontend/type_checking/env.rb', line 8

def definitions
  @definitions
end

#entry_nameObject (readonly)

Returns the value of attribute entry_name

Returns:

  • (Object)

    the current value of entry_name



8
9
10
# File 'lib/jade/frontend/type_checking/env.rb', line 8

def entry_name
  @entry_name
end

#node_typesObject (readonly)

Returns the value of attribute node_types

Returns:

  • (Object)

    the current value of node_types



8
9
10
# File 'lib/jade/frontend/type_checking/env.rb', line 8

def node_types
  @node_types
end

#substitutionObject (readonly)

Returns the value of attribute substitution

Returns:

  • (Object)

    the current value of substitution



8
9
10
# File 'lib/jade/frontend/type_checking/env.rb', line 8

def substitution
  @substitution
end

#var_genObject (readonly)

Returns the value of attribute var_gen

Returns:

  • (Object)

    the current value of var_gen



8
9
10
# File 'lib/jade/frontend/type_checking/env.rb', line 8

def var_gen
  @var_gen
end

Class Method Details

.empty(var_gen = VarGen.new) ⇒ Object



12
13
14
# File 'lib/jade/frontend/type_checking/env.rb', line 12

def self.empty(var_gen = VarGen.new)
  Env[nil, {}, Substitution::EMPTY, {}, var_gen, {}]
end

Instance Method Details

#bind(key, value) ⇒ Object



20
21
22
23
24
# File 'lib/jade/frontend/type_checking/env.rb', line 20

def bind(key, value)
  bindings
    .merge(key => value)
    .then { with(bindings: it) }
end

#canonicalize_node_typesObject

Post-finalize, apply the env’s substitution to every pinned type so stored types are canonical (no leftover Type::Var that the substitution would resolve).



33
34
35
36
37
# File 'lib/jade/frontend/type_checking/env.rb', line 33

def canonicalize_node_types
  node_types
    .transform_values { substitution.apply(it) }
    .then { with(node_types: it) }
end

#composose_substitution(sub) ⇒ Object



73
74
75
# File 'lib/jade/frontend/type_checking/env.rb', line 73

def composose_substitution(sub)
  with(substitution: substitution.compose(sub))
end

#define(key, value) ⇒ Object



39
40
41
42
43
# File 'lib/jade/frontend/type_checking/env.rb', line 39

def define(key, value)
  definitions
    .merge(key => value)
    .then { with(definitions: it) }
end

#free_varsObject



65
66
67
68
69
70
71
# File 'lib/jade/frontend/type_checking/env.rb', line 65

def free_vars
  bindings
    .values
    .flat_map(&:free_vars)
    .flat_map { substitution.apply(it).unbound_vars }
    .uniq(&:id)
end

#freshObject



16
17
18
# File 'lib/jade/frontend/type_checking/env.rb', line 16

def fresh
  var_gen.fresh
end

#lookup(key) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jade/frontend/type_checking/env.rb', line 45

def lookup(key)
  binding = bindings[key]

  type, constraints =
    case bindings[key]
    in Scheme => scheme
      Instantiation.instantiate(scheme, var_gen)

    in Placeholder => placeholder
      Scheme[placeholder.free_vars, placeholder.type, placeholder.constraints]
        .then { Instantiation.instantiate(it, var_gen) }
    end

  Result.init(type, constraints)
end

#lookup_def(key) ⇒ Object



61
62
63
# File 'lib/jade/frontend/type_checking/env.rb', line 61

def lookup_def(key)
  definitions[key]
end

#pin_type(node_id, type) ⇒ Object



26
27
28
# File 'lib/jade/frontend/type_checking/env.rb', line 26

def pin_type(node_id, type)
  with(node_types: node_types.merge(node_id => type))
end