From 2cd2c347d0b129c08cff3f0bb4aef597fcb432fb Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Fri, 29 Mar 2019 20:28:04 -0500 Subject: [PATCH] Refactor AnonymousType typecheck --- lib/dhall/typecheck.rb | 18 ++++++------------ lib/dhall/util.rb | 6 ++++++ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/dhall/typecheck.rb b/lib/dhall/typecheck.rb index b7ae6d7..31b705b 100644 --- a/lib/dhall/typecheck.rb +++ b/lib/dhall/typecheck.rb @@ -500,20 +500,14 @@ module Dhall TypeChecker.for(mtype).annotate(context).type end - if (bad = kinds.find { |t| !KINDS.include?(t) }) - raise TypeError, "AnonymousType field kind #{bad} "\ - "not one of #{KINDS}" - end + TypeChecker.assert (kinds - KINDS), [], + "AnonymousType field kind not one of #{KINDS}" - if (bad = kinds.find { |t| t != kinds.first }) - raise TypeError, "AnonymousType field kind #{bad} "\ - "does not match #{kinds.first}" - end + TypeChecker.assert kinds, Util::ArrayAllTheSame, + "AnonymousType field kinds not all the same" - Dhall::TypeAnnotation.new( - value: @type, - type: kinds.first || KINDS.first - ) + type = kinds.first || KINDS.first + Dhall::TypeAnnotation.new(value: @type, type: type) end end diff --git a/lib/dhall/util.rb b/lib/dhall/util.rb index 0f95225..46e4f66 100644 --- a/lib/dhall/util.rb +++ b/lib/dhall/util.rb @@ -35,6 +35,12 @@ module Dhall end end + module ArrayAllTheSame + def self.===(other) + Array === other && other.all? { |x| x == other.first } + end + end + def self.match_results(xs=nil, ys=nil) Array(xs).each_with_index.map do |r, idx| yield r, ys[idx] -- 2.38.5