From 2b29af325093994cc48a6b3f6b63b04dfb5254f4 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Sun, 21 Jul 2019 21:36:24 -0500 Subject: [PATCH] List stores full type, allows for unusual annotation --- lib/dhall/ast.rb | 41 ++++++++++++++++++++++++++++++++--------- lib/dhall/binary.rb | 12 +++++++++--- lib/dhall/normalize.rb | 2 +- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/lib/dhall/ast.rb b/lib/dhall/ast.rb index b0444b1..745886f 100644 --- a/lib/dhall/ast.rb +++ b/lib/dhall/ast.rb @@ -373,9 +373,18 @@ module Dhall include(ValueSemantics.for_attributes do elements Util::ArrayOf.new(Expression, min: 1) - element_type Either(nil, Expression), default: nil + type Either(nil, Expression), default: nil end) + def initialize(attrs) + if attrs.key?(:element_type) + et = attrs.delete(:element_type) + attrs[:type] = self.class.as_dhall.call(et) if et + end + + super + end + def self.of(*args, type: nil) if args.empty? EmptyList.new(element_type: type) @@ -388,11 +397,13 @@ module Dhall Builtins[:List] end - def type - Dhall::Application.new( - function: self.class.as_dhall, - argument: element_type - ) + def element_type + if type.nil? + elsif type.is_a?(Application) && type.function == Builtins[:List] + type.argument + else + raise "Cannot get element_type of: #{type.inspect}" + end end def as_json @@ -400,9 +411,10 @@ module Dhall end def map(type: nil, &block) + type = type.nil? ? nil : Builtins[:List].call(type.as_dhall) with( - elements: elements.each_with_index.map(&block), - element_type: type&.as_dhall + elements: elements.each_with_index.map(&block), + type: type ) end @@ -450,11 +462,22 @@ module Dhall class EmptyList < List include(ValueSemantics.for_attributes do - element_type Either(nil, Expression) + type Either(nil, Expression) end) + def initialize(attrs) + if attrs.key?(:element_type) + et = attrs.delete(:element_type) + attrs[:type] = self.class.as_dhall.call(et) if et + end + + super + end + def as_json [4, element_type.as_json] + rescue + [28, type.as_json] end def map(type: nil) diff --git a/lib/dhall/binary.rb b/lib/dhall/binary.rb index 9bdfb85..c544ce9 100644 --- a/lib/dhall/binary.rb +++ b/lib/dhall/binary.rb @@ -85,15 +85,21 @@ module Dhall class List def self.decode(type, *els) - type = type.nil? ? nil : Dhall.decode(type) + type = type.nil? ? nil : Builtins[:List].call(Dhall.decode(type)) if els.empty? - EmptyList.new(element_type: type) + EmptyList.new(type: type) else - List.new(elements: els.map(&Dhall.method(:decode)), element_type: type) + List.new(elements: els.map(&Dhall.method(:decode)), type: type) end end end + class EmptyList + def self.decode(type) + EmptyList.new(type: Dhall.decode(type)) + end + end + class Optional def self.decode(type, value=nil) if value.nil? diff --git a/lib/dhall/normalize.rb b/lib/dhall/normalize.rb index 86b50c4..f5d102f 100644 --- a/lib/dhall/normalize.rb +++ b/lib/dhall/normalize.rb @@ -245,7 +245,7 @@ module Dhall class EmptyList def normalize - super.with(element_type: element_type.normalize) + super.with(type: type.normalize) end end -- 2.34.5