~singpolyma/dhall-ruby

9e53452cce1f3204c45f6b3f796eff8c6d5fb98f — Stephen Paul Weber 3 years ago 2b8f19b
Remove multi-argument applications

They only exist in binary encoding, so let's keep them there.
5 files changed, 63 insertions(+), 63 deletions(-)

M lib/dhall/ast.rb
M lib/dhall/binary.rb
M lib/dhall/builtins.rb
M lib/dhall/normalize.rb
M lib/dhall/typecheck.rb
M lib/dhall/ast.rb => lib/dhall/ast.rb +5 -5
@@ 10,7 10,7 @@ module Dhall
	class Expression
		def call(*args)
			args.reduce(self) { |f, arg|
				Application.new(function: f, arguments: [arg])
				Application.new(function: f, argument: arg)
			}.normalize
		end



@@ 108,11 108,11 @@ module Dhall
	class Application < Expression
		include(ValueSemantics.for_attributes do
			function Expression
			arguments Util::ArrayOf.new(Expression, min: 1)
			argument Expression
		end)

		def as_json
			[0, function.as_json, *arguments.map(&:as_json)]
			[0, function.as_json, argument.as_json]
		end
	end



@@ 1018,12 1018,12 @@ module Dhall
		def desugar
			lets.reverse.reduce(body) do |inside, let|
				Application.new(
					function:  Function.new(
					function: Function.new(
						var:  let.var,
						type: let.type,
						body: inside
					),
					arguments: [let.assign]
					argument: let.assign
				)
			end
		end

M lib/dhall/binary.rb => lib/dhall/binary.rb +3 -4
@@ 37,10 37,9 @@ module Dhall

	class Application
		def self.decode(f, *args)
			new(
				function:  Dhall.decode(f),
				arguments: args.map(&Dhall.method(:decode))
			)
			args.map(&Dhall.method(:decode)).reduce(Dhall.decode(f)) { |f, arg|
				new(function: f, argument: arg)
			}
		end
	end


M lib/dhall/builtins.rb => lib/dhall/builtins.rb +13 -12
@@ 9,7 9,7 @@ module Dhall
		def call(*args)
			# Do not auto-normalize builtins to avoid recursion loop
			args.reduce(self) do |f, arg|
				Application.new(function: f, arguments: [arg])
				Application.new(function: f, argument: arg)
			end
		end



@@ 82,9 82,8 @@ module Dhall
			def fusion(arg, *bogus)
				if bogus.empty? &&
				   arg.is_a?(Application) &&
				   arg.function == Natural_fold.new &&
				   arg.arguments.length == 1
					arg.arguments.first
				   arg.function == Natural_fold.new
					arg.argument
				else
					super
				end


@@ 179,9 178,8 @@ module Dhall
				_, arg, = args
				if arg.is_a?(Application) &&
				   arg.function.is_a?(Application) &&
				   arg.function.function == List_fold.new &&
				   arg.arguments.length == 1
					arg.arguments.first
				   arg.function.function == List_fold.new
					arg.argument
				else
					super
				end


@@ 334,9 332,8 @@ module Dhall
				_, arg, = args
				if arg.is_a?(Application) &&
				   arg.function.is_a?(Application) &&
				   arg.function.function == Optional_fold.new &&
				   arg.arguments.length == 1
					arg.arguments.first
				   arg.function.function == Optional_fold.new
					arg.argument
				else
					super
				end


@@ 373,8 370,12 @@ module Dhall
				f        Either(nil, Expression), default: nil
			end)

			def call(arg)
				fill_or_call(arg) { @optional.reduce(arg, &f) } || super
			def call(*args)
				args.reduce(self) do |fold, arg|
					fold.fill_or_call(arg) {
						fold.optional.reduce(arg, &fold.f)
					} || super
				end
			end
		end


M lib/dhall/normalize.rb => lib/dhall/normalize.rb +3 -3
@@ 48,7 48,7 @@ module Dhall

			if normalized.function.is_a?(Builtin) ||
			   normalized.function.is_a?(Function)
				return normalized.function.call(*normalized.arguments)
				return normalized.function.call(normalized.argument)
			end

			normalized


@@ 56,11 56,11 @@ module Dhall

		def fuse
			if function.is_a?(Application)
				@fuse ||= function.function.fusion(*function.arguments, *arguments)
				@fuse ||= function.function.fusion(function.argument, argument)
				return @fuse if @fuse
			end

			@fuse ||= function.fusion(*arguments)
			@fuse ||= function.fusion(argument)
		end
	end


M lib/dhall/typecheck.rb => lib/dhall/typecheck.rb +39 -39
@@ 129,8 129,8 @@ module Dhall
					var:  "A",
					type: Dhall::Variable["Type"],
					body: Dhall::Application.new(
						function:  Dhall::Variable["Optional"],
						arguments: [Dhall::Variable["A"]]
						function: Dhall::Variable["Optional"],
						argument: Dhall::Variable["A"]
					)
				)
			}.freeze


@@ 391,8 391,8 @@ module Dhall
				Dhall::TypeAnnotation.new(
					value: @expr,
					type:  Dhall::Application.new(
						function:  Dhall::Variable["List"],
						arguments: [@expr.type]
						function: Dhall::Variable["List"],
						argument: @expr.type
					)
				)
			end


@@ 421,8 421,8 @@ module Dhall
				Dhall::TypeAnnotation.new(
					value: list,
					type:  Dhall::Application.new(
						function:  Dhall::Variable["List"],
						arguments: [list.type]
						function: Dhall::Variable["List"],
						argument: list.type
					)
				)
			end


@@ 442,8 442,8 @@ module Dhall
				Dhall::TypeAnnotation.new(
					value: @expr,
					type:  Dhall::Application.new(
						function:  Dhall::Variable["Optional"],
						arguments: [@expr.type]
						function: Dhall::Variable["Optional"],
						argument: @expr.type
					)
				)
			end


@@ 468,8 468,8 @@ module Dhall
				Dhall::TypeAnnotation.new(
					value: some,
					type:  Dhall::Application.new(
						function:  Dhall::Variable["Optional"],
						arguments: [some.type]
						function: Dhall::Variable["Optional"],
						argument: some.type
					)
				)
			end


@@ 762,7 762,7 @@ module Dhall
			def initialize(app)
				@app = app
				@func = TypeChecker.for(app.function)
				@arg = TypeChecker.for(app.arguments.first)
				@arg = TypeChecker.for(app.argument)
			end

			def annotate(context)


@@ 784,7 784,7 @@ module Dhall
				).shift(-1, afunc.type.var, 0)

				Dhall::TypeAnnotation.new(
					value: @app.with(function: afunc, arguments: [aarg]),
					value: @app.with(function: afunc, argument: aarg),
					type: type
				)
			end


@@ 936,8 936,8 @@ module Dhall
							)
						),
						body: Dhall::Application.new(
							function:  Dhall::Variable["List"],
							arguments: [Dhall::Variable["a"]]
							function: Dhall::Variable["List"],
							argument: Dhall::Variable["a"]
						)
					)
				),


@@ 946,8 946,8 @@ module Dhall
					type: Dhall::Variable["Type"],
					body: Dhall::Forall.of_arguments(
						Dhall::Application.new(
							function:  Dhall::Variable["List"],
							arguments: [Dhall::Variable["a"]]
							function: Dhall::Variable["List"],
							argument: Dhall::Variable["a"]
						),
						body: Dhall::Forall.new(
							var:  "list",


@@ 973,8 973,8 @@ module Dhall
					type: Dhall::Variable["Type"],
					body: Dhall::Forall.of_arguments(
						Dhall::Application.new(
							function:  Dhall::Variable["List"],
							arguments: [Dhall::Variable["a"]]
							function: Dhall::Variable["List"],
							argument: Dhall::Variable["a"]
						),
						body: Dhall::Variable["Natural"]
					)


@@ 984,12 984,12 @@ module Dhall
					type: Dhall::Variable["Type"],
					body: Dhall::Forall.of_arguments(
						Dhall::Application.new(
							function:  Dhall::Variable["List"],
							arguments: [Dhall::Variable["a"]]
							function: Dhall::Variable["List"],
							argument: Dhall::Variable["a"]
						),
						body: Dhall::Application.new(
							function:  Dhall::Variable["Optional"],
							arguments: [Dhall::Variable["a"]]
							function: Dhall::Variable["Optional"],
							argument: Dhall::Variable["a"]
						)
					)
				),


@@ 998,12 998,12 @@ module Dhall
					type: Dhall::Variable["Type"],
					body: Dhall::Forall.of_arguments(
						Dhall::Application.new(
							function:  Dhall::Variable["List"],
							arguments: [Dhall::Variable["a"]]
							function: Dhall::Variable["List"],
							argument: Dhall::Variable["a"]
						),
						body: Dhall::Application.new(
							function:  Dhall::Variable["Optional"],
							arguments: [Dhall::Variable["a"]]
							function: Dhall::Variable["Optional"],
							argument: Dhall::Variable["a"]
						)
					)
				),


@@ 1012,15 1012,15 @@ module Dhall
					type: Dhall::Variable["Type"],
					body: Dhall::Forall.of_arguments(
						Dhall::Application.new(
							function:  Dhall::Variable["List"],
							arguments: [Dhall::Variable["a"]]
							function: Dhall::Variable["List"],
							argument: Dhall::Variable["a"]
						),
						body: Dhall::Application.new(
							function:  Dhall::Variable["List"],
							arguments: [Dhall::RecordType.new(record: {
							function: Dhall::Variable["List"],
							argument: Dhall::RecordType.new(record: {
								"index" => Dhall::Variable["Natural"],
								"value" => Dhall::Variable["a"]
							})]
							})
						)
					)
				),


@@ 1029,12 1029,12 @@ module Dhall
					type: Dhall::Variable["Type"],
					body: Dhall::Forall.of_arguments(
						Dhall::Application.new(
							function:  Dhall::Variable["List"],
							arguments: [Dhall::Variable["a"]]
							function: Dhall::Variable["List"],
							argument: Dhall::Variable["a"]
						),
						body: Dhall::Application.new(
							function:  Dhall::Variable["List"],
							arguments: [Dhall::Variable["a"]]
							function: Dhall::Variable["List"],
							argument: Dhall::Variable["a"]
						)
					)
				),


@@ 1043,8 1043,8 @@ module Dhall
					type: Dhall::Variable["Type"],
					body: Dhall::Forall.of_arguments(
						Dhall::Application.new(
							function:  Dhall::Variable["Optional"],
							arguments: [Dhall::Variable["a"]]
							function: Dhall::Variable["Optional"],
							argument: Dhall::Variable["a"]
						),
						body: Dhall::Forall.new(
							var:  "optional",


@@ 1085,8 1085,8 @@ module Dhall
							)
						),
						body: Dhall::Application.new(
							function:  Dhall::Variable["Optional"],
							arguments: [Dhall::Variable["a"]]
							function: Dhall::Variable["Optional"],
							argument: Dhall::Variable["a"]
						)
					)
				),