Class: Dhall::AsDhall::UnionInferer

Inherits:
Object
  • Object
show all
Defined in:
lib/dhall/as_dhall.rb

Instance Method Summary collapse

Constructor Details

#initialize(tagged = {}) ⇒ UnionInferer

Returns a new instance of UnionInferer.



52
53
54
# File 'lib/dhall/as_dhall.rb', line 52

def initialize(tagged={})
  @tagged = tagged
end

Instance Method Details

#disambiguate_against(tag, anno) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/dhall/as_dhall.rb', line 80

def disambiguate_against(tag, anno)
  self.class.new(
    @tagged.reject { |k, _| k == tag }.merge(
      "#{tag}_#{@tagged[tag].type.digest.hexdigest}" => @tagged[tag],
      "#{tag}_#{anno.type.digest.hexdigest}"         => anno
    )
  )
end

#union_for(expr) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/dhall/as_dhall.rb', line 60

def union_for(expr)
  if expr.is_a?(Enum)
    tag = expr.tag
    expr = nil
  else
    tag = @tagged.keys.find { |k| @tagged[k].exprs.include?(expr) }
  end
  expr = expr.extract if expr.is_a?(Union)
  Union.from(union_type, tag, expr)
end

#union_typeObject



56
57
58
# File 'lib/dhall/as_dhall.rb', line 56

def union_type
  UnionType.new(alternatives: Hash[@tagged.map { |k, v| [k, v.type] }])
end

#with(tag, type_annotation) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/dhall/as_dhall.rb', line 71

def with(tag, type_annotation)
  anno = AnnotatedExpressionList.from(type_annotation)
  if @tagged.key?(tag) && @tagged[tag].type != anno.type
    disambiguate_against(tag, anno)
  else
    self.class.new(@tagged.merge(tag => anno) { |_, x, y| x + y })
  end
end