Class: Dhall::TypeChecker::Merge::AnnotatedMerge

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

Instance Method Summary collapse

Constructor Details

#initialize(merge:, record:, input:) ⇒ AnnotatedMerge

Returns a new instance of AnnotatedMerge.



696
697
698
699
700
701
702
703
704
705
706
# File 'lib/dhall/typecheck.rb', line 696

def initialize(merge:, record:, input:)
  @merge = merge.with(record: record, input: input)
  @handlers = Handlers.new(record)
  @record = record
  @union = input

  TypeChecker.assert @union.type, Dhall::UnionType,
                     "Merge expected Union got: #{@union.type}"

  assert_union_and_handlers_match
end

Instance Method Details

#annotationObject



708
709
710
711
712
713
# File 'lib/dhall/typecheck.rb', line 708

def annotation
  Dhall::TypeAnnotation.new(
    value: @merge,
    type:  type
  )
end

#assert_kind(context) ⇒ Object



719
720
721
722
723
724
725
726
727
728
729
# File 'lib/dhall/typecheck.rb', line 719

def assert_kind(context)
  kind = TypeChecker.for(type).annotate(context).type

  TypeChecker.assert(
    kind,
    Builtins[:Type],
    "Merge must have kind Type"
  )

  kind
end

#assert_union_and_handlers_matchObject



731
732
733
734
735
736
737
738
739
740
741
742
743
# File 'lib/dhall/typecheck.rb', line 731

def assert_union_and_handlers_match
  extras = @handlers.keys - @union.type.alternatives.keys
  TypeChecker.assert extras, [],
                     "Merge handlers unknown alternatives: #{extras}"

  @union.type.alternatives.each do |k, atype|
    atype.nil? || TypeChecker.assert(
      @handlers.fetch_input_type(k),
      atype,
      "Handler argument does not match alternative type: #{atype}"
    )
  end
end

#typeObject



715
716
717
# File 'lib/dhall/typecheck.rb', line 715

def type
  @type ||= @handlers.output_type(@merge.type)
end