<commit_before>RDL.nowrap :Hash

RDL.type_params :Hash, [:k, :v], :all?

def Hash.output_type(trec, targs, meth_name, default1, default2=default1, nil_default: false, use_sing_val: true)
  case trec
  when RDL::Type::FiniteHashType    
    if targs.empty? || targs.all? { |t| t.is_a?(RDL::Type::SingletonType) }
      vals = RDL.type_cast((if use_sing_val then targs.map { |t| RDL.type_cast(t, "RDL::Type::SingletonType").val } else targs end), "Array<%any>", force: true)
      res = RDL.type_cast(trec.elts.send(meth_name, *vals), "Object", force: true)
      if nil_default && res.nil?
        if default1 == :promoted_val
          return trec.promote.params[1]
        elsif default1 == :promoted_key
          return trec.promote.params[0]
        else
          return RDL::Globals.parser.scan_str "#T #{default1}"
        end
      end
      to_type(res)
    else
      if default1 == :promoted_val
        x = trec.promote.params[1]
        return x
      elsif default1 == :promoted_key
        return trec.promote.params[0]
      else
        RDL::Globals.parser.scan_str "#T #{default1}"
      end
    end
  else
    RDL::Globals.parser.scan_str "#T #{default2}"
  end
end
RDL.type Hash, 'self.output_type', "(RDL::Type::Type, Array<RDL::Type::Type>, Symbol, Symbol or String, ?(Symbol or String), { nil_default: ?%bool, use_sing_val: ?%bool } ) -> RDL::Type::Type", typecheck: :type_code, wrap: false, effect: [:+, :+]


def Hash.to_type(t)
  case t
  when RDL::Type::Type
    t
  when Array
    RDL::Type::TupleType.new(*(t.map { |i| to_type(i) }))
  else
    ## symbols, ints, nil, ...
    RDL::Type::SingletonType.new(t)
  end
end
RDL.type Hash, 'self.to_type', "(%any) -> RDL::Type::Type", typecheck: :type_code, wrap: false, effect: [:+, :+]

def Hash.any_or_k(trec)
  case trec
  when RDL::Type::FiniteHashType
    RDL::Globals.types[:top]
  else
    RDL::Globals.parser.scan_str "#T k"
  end
end
RDL.type Hash, 'self.any_or_k', "(RDL::Type::Type) -> RDL::Type::Type", typecheck: :type_code, wrap: false, effect: [:+, :+]

def Hash.any_or_v(trec)
  case trec
  when RDL::Type::FiniteHashType
    RDL::Globals.types[:top]
  else
    RDL::Globals.parser.scan_str "#T v"
  end
end
RDL.type Hash, 'self.any_or_v', "(RDL::Type::Type) -> RDL::Type::Type", typecheck: :type_code, wrap: false, effect: [:+, :+]

def Hash.promoted_or_v(trec)
  case trec
  when RDL::Type::FiniteHashType
    trec.promote.params[1]
  else
    RDL::Globals.parser.scan_str "#T v"
  end
end
RDL.type Hash, 'self.promoted_or_v', "(RDL::Type::Type) -> RDL::Type::Type", typecheck: :type_code, wrap: false, effect: [:+, :+]


def Hash.weak_promote(val)
  case val
  when RDL::Type::UnionType
    if val.types.all? { |t| t.is_a?(RDL::Type::SingletonType) }
      klass = RDL.type_cast(val.types[0], "RDL::Type::SingletonType", force: true).nominal.klass
      if val.types.all? { |t| RDL.type_cast(t, "RDL::Type::SingletonType", force: true).nominal.klass == klass }
        return RDL::Type::NominalType.new(klass)
      else
        return val
      end
    else
      return val
    end
  else
    val
  end
end
RDL.type Hash, 'self.weak_promote', "(RDL::Type::Type) -> RDL::Type::Type", typecheck: :type_code, wrap: false, effect: [:+, :+]

#RDL.type :Hash, 'self.[]', '(*%any) -> ``hash_create_output_from_list(targs)``'
RDL.type :Hash, 'self.[]', '(*%any) -> ``hash_create_output(targs)``'

def Hash.hash_create_output_from_list(targs)
  raise RDL::Typecheck::StaticTypeError, "Hash[...] expect only 1 argument. Have #{targs}." if targs.size > 1
  raise RDL::Typecheck::StaticTypeError, "The argument has to be an array or tuple" unless (targs[0].is_a?(RDL::Type::GenericType) && targs[0].base.klass == Array)

  case targs[0].params[0]
  when RDL::Type::GenericType
    return RDL::Globals.parser.scan_str "#T Hash<#{targs[0].params[0].params[0]}, #{targs[0].params[0].params[0]}>"
  when RDL::Type::TupleType
    return RDL::Type::GenericType.new(RDL::Type::NominalType.new(Hash), targs[0].params[0].params[0], targs[0].params[0].params[1])
  end
end

def Hash.hash_create_output(targs)
  raise RDL::Typecheck::StaticTypeError, "Hash.[] expects an even number of arguments. Have #{targs}." if targs.size.odd?
  args = RDL.type_cast([], "Array<%any>", force: true)
  i = -1
  args = targs.map { |a| i = i+1 ; if i.even? && a.is_a?(RDL::Type::SingletonType) then RDL.type_cast(a, "RDL::Type::SingletonType", force: true).val else a end }
=begin                                     
  targs.each_with_index { |a, i|
    args[i] = (if i.even? && a.is_a?(RDL::Type::SingletonType) then RDL.type_cast(a, "RDL::Type::SingletonType", force: true).val else a end)
  }
=end
  RDL::Type::FiniteHashType.new(RDL.type_cast(Hash[*args], "Hash<%any, RDL::Type::Type>", force: true), nil)
end
RDL.type Hash, 'self.hash_create_output', "(Array<RDL::Type::Type>) -> RDL::Type::Type", typecheck: :type_code, wrap: false, effect: [:+, :+]

RDL.type :Hash, :[], '(``any_or_k(trec)``) -> ``output_type(trec, targs, :[], :promoted_val, "v", nil_default: true)``', effect: [:+, :+]

RDL.type :Hash, :[]=, '(``any_or_k(trec)``, ``any_or_v(trec)``) -> ``assign_output(trec, targs)``'


def Hash.assign_output(trec, targs)
  case trec
  when RDL::Type::FiniteHashType
    case targs[0]<commit_msg>comp type update for Hash.[], required for wikipedia<commit_after>RDL.nowrap :Hash

RDL.type_params :Hash, [:k, :v], :all?

def Hash.output_type(trec, targs, meth_name, default1, default2=default1, nil_default: false, use_sing_val: true)
  case trec
  when RDL::Type::FiniteHashType    
    if targs.empty? || targs.all? { |t| t.is_a?(RDL::Type::SingletonType) }
      vals = RDL.type_cast((if use_sing_val then targs.map { |t| RDL.type_cast(t, "RDL::Type::SingletonType").val } else targs end), "Array<%any>", force: true)
      res = RDL.type_cast(trec.elts.send(meth_name, *vals), "Object", force: true)
      if nil_default && res.nil?
        if default1 == :promoted_val
          return trec.promote.params[1]
        elsif default1 == :promoted_key
          return trec.promote.params[0]
        else
          return RDL::Globals.parser.scan_str "#T #{default1}"
        end
      end
      to_type(res)
    else
      if default1 == :promoted_val
        x = trec.promote.params[1]
        return x
      elsif default1 == :promoted_key
        return trec.promote.params[0]
      else
        RDL::Globals.parser.scan_str "#T #{default1}"
      end
    end
  else
    RDL::Globals.parser.scan_str "#T #{default2}"
  end
end
RDL.type Hash, 'self.output_type', "(RDL::Type::Type, Array<RDL::Type::Type>, Symbol, Symbol or String, ?(Symbol or String), { nil_default: ?%bool, use_sing_val: ?%bool } ) -> RDL::Type::Type", typecheck: :type_code, wrap: false, effect: [:+, :+]


def Hash.to_type(t)
  case t
  when RDL::Type::Type
    t
  when Array
    RDL::Type::TupleType.new(*(t.map { |i| to_type(i) }))
  else
    ## symbols, ints, nil, ...
    RDL::Type::SingletonType.new(t)
  end
end
RDL.type Hash, 'self.to_type', "(%any) -> RDL::Type::Type", typecheck: :type_code, wrap: false, effect: [:+, :+]

def Hash.any_or_k(trec)
  case trec
  when RDL::Type::FiniteHashType
    RDL::Globals.types[:top]
  else
    RDL::Globals.parser.scan_str "#T k"
  end
end
RDL.type Hash, 'self.any_or_k', "(RDL::Type::Type) -> RDL::Type::Type", typecheck: :type_code, wrap: false, effect: [:+, :+]

def Hash.any_or_v(trec)
  case trec
  when RDL::Type::FiniteHashType
    RDL::Globals.types[:top]
  else
    RDL::Globals.parser.scan_str "#T v"
  end
end
RDL.type Hash, 'self.any_or_v', "(RDL::Type::Type) -> RDL::Type::Type", typecheck: :type_code, wrap: false, effect: [:+, :+]

def Hash.promoted_or_v(trec)
  case trec
  when RDL::Type::FiniteHashType
    trec.promote.params[1]
  else
    RDL::Globals.parser.scan_str "#T v"
  end
end
RDL.type Hash, 'self.promoted_or_v', "(RDL::Type::Type) -> RDL::Type::Type", typecheck: :type_code, wrap: false, effect: [:+, :+]


def Hash.weak_promote(val)
  case val
  when RDL::Type::UnionType
    if val.types.all? { |t| t.is_a?(RDL::Type::SingletonType) }
      klass = RDL.type_cast(val.types[0], "RDL::Type::SingletonType", force: true).nominal.klass
      if val.types.all? { |t| RDL.type_cast(t, "RDL::Type::SingletonType", force: true).nominal.klass == klass }
        return RDL::Type::NominalType.new(klass)
      else
        return val
      end
    else
      return val
    end
  else
    val
  end
end
RDL.type Hash, 'self.weak_promote', "(RDL::Type::Type) -> RDL::Type::Type", typecheck: :type_code, wrap: false, effect: [:+, :+]

#RDL.type :Hash, 'self.[]', '(*%any) -> ``hash_create_output_from_list(targs)``'
RDL.type :Hash, 'self.[]', '(*%any) -> ``hash_create_output(targs)``'

def Hash.hash_create_output_from_list(targs)
  raise RDL::Typecheck::StaticTypeError, "Hash[...] expect only 1 argument. Have #{targs}." if targs.size > 1
  raise RDL::Typecheck::StaticTypeError, "The argument has to be an array or tuple" unless (targs[0].is_a?(RDL::Type::GenericType) && targs[0].base.klass == Array)

  case targs[0].params[0]
  when RDL::Type::GenericType
    return RDL::Globals.parser.scan_str "#T Hash<#{targs[0].params[0].params[0]}, #{targs[0].params[0].params[0]}>"
  when RDL::Type::TupleType
    return RDL::Type::GenericType.new(RDL::Type::NominalType.new(Hash), targs[0].params[0].params[0], targs[0].params[0].params[1])
  end
end

def Hash.hash_create_output(targs)
  return hash_create_output_from_list(targs) if targs.size == 1

  raise RDL::Typecheck::StaticTypeError, "Hash.[] expects an even number of arguments. Have #{targs}." if targs.size.odd?
  args = RDL.type_cast([], "Array<%any>", force: true)
  i = -1
  args = targs.map { |a| i = i+1 ; if i.even? && a.is_a?(RDL::Type::SingletonType) then RDL.type_cast(a, "RDL::Type::SingletonType", force: true).val else a end }
=begin                                     
  targs.each_with_index { |a, i|
    args[i] = (if i.even? && a.is_a?(RDL::Type::SingletonType) then RDL.type_cast(a, "RDL::Type::SingletonType", force: true).val else a end)
  }
=end
  RDL::Type::FiniteHashType.new(RDL.type_cast(Hash[*args], "Hash<%any, RDL::Type::Type>", force: true), nil)
end
RDL.type Hash, 'self.hash_create_output', "(Array<RDL::Type::Type>) -> RDL::Type::Type", typecheck: :type_code, wrap: false, effect: [:+, :+]

RDL.type :Hash, :[], '(``any_or_k(trec)``) -> ``output_type(trec, targs, :[], :promoted_val, "v", nil_default: true)``', effect: [:+, :+]

RDL.type :Hash, :[]=, '(``any_or_k(trec)``, ``any_or_v(trec)``) -> ``assign_output(trec, targs)``'


def Hash.assign_output(trec, targs)
  case trec
  when RDL::Type::FiniteHashType
    case targs[0]
{'fraction_non_alphanumeric': 0.18632430398796088, 'fraction_numerical': 0.007054176072234763, 'mean_word_length': 4.070577014783024, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6575306', 'n_tokens_mistral': 4218, 'n_tokens_neox': 4204, 'n_words': 835}