Class: Jade::Frontend::TypeChecking::Substitution

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

Constant Summary collapse

EMPTY =
Substitution[{}].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mappings: {}) ⇒ Substitution

Returns a new instance of Substitution.



5
6
7
# File 'lib/jade/frontend/type_checking/substitution.rb', line 5

def initialize(mappings: {})
  super
end

Instance Attribute Details

#mappingsObject (readonly)

Returns the value of attribute mappings

Returns:

  • (Object)

    the current value of mappings



4
5
6
# File 'lib/jade/frontend/type_checking/substitution.rb', line 4

def mappings
  @mappings
end

Class Method Details

.emptyObject



13
14
15
# File 'lib/jade/frontend/type_checking/substitution.rb', line 13

def self.empty
  EMPTY
end

Instance Method Details

#apply(type) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/jade/frontend/type_checking/substitution.rb', line 17

def apply(type)
  case type
  in Type::Constraint(type: constraint_type)
    type.with(type: apply(constraint_type))

  in Type::Function(args:, return_type:)
    type.with(
      args: args.map { apply(it) },
      return_type: apply(return_type),
    )

  in Type::Constructor
    type

  in Type::Var(id:)
    mapping = mappings[id]

    return type unless mapping

    case mapping
    in Type::Var(id: ^id)
      mapping

    else
      apply(mapping)
    end

  in Type::PartialApplication(constructor:, args:)
    type.with(constructor: apply(constructor), args: args.map { apply(it) })

  in Type::Application(args:)
    case apply(type.constructor)
    in Type::PartialApplication(constructor:, args: tail_args)
      Type::Application[constructor, args.map { apply(it) } + tail_args]

    in constructor
      type.with(constructor:, args: args.map { apply(it) })
    end

  in Type::AnonymousRecord(fields:, row_var:)
    applied_fields = fields.transform_values { apply(it) }

    return type.with(fields: applied_fields) if type.closed?

    case apply(row_var)
    in Type::AnonymousRecord(fields: extra_fields, row_var: new_row_var)
      Type.anonymous_record(applied_fields.merge(extra_fields), new_row_var)

    in Type::Var => applied_row_var
      type.with(fields: applied_fields, row_var: applied_row_var)

    in Type::Application => struct
      # `{γ | fields}` whose row resolved to a nominal struct
      # collapses to the struct — the open record was always a
      # structural query against it.
      struct

    end
  end
end

#bind(name, type) ⇒ Object



78
79
80
# File 'lib/jade/frontend/type_checking/substitution.rb', line 78

def bind(name, type)
  with(mappings: mappings.merge(name => type))
end

#compose(other) ⇒ Object



82
83
84
85
86
87
# File 'lib/jade/frontend/type_checking/substitution.rb', line 82

def compose(other)
  return self if other.mappings.empty?
  return other if mappings.empty?

  Substitution[mappings.merge(other.mappings)]
end

#empty?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/jade/frontend/type_checking/substitution.rb', line 9

def empty?
  mappings.empty?
end