~singpolyma/dhall-ruby

b561310f04f8e0b358b39679da096e1d5eb953aa — Stephen Paul Weber 4 years ago c960200
as_dhall for Proc

Cannot actually serialize this, but can be useful for passing functions
in to Dhall functions, etc.
3 files changed, 39 insertions(+), 2 deletions(-)

M lib/dhall/as_dhall.rb
M lib/dhall/ast.rb
M lib/dhall/normalize.rb
M lib/dhall/as_dhall.rb => lib/dhall/as_dhall.rb +6 -0
@@ 220,5 220,11 @@ module Dhall
				)
			end
		end

		refine ::Proc do
			def as_dhall
				FunctionProxy.new(self)
			end
		end
	end
end

M lib/dhall/ast.rb => lib/dhall/ast.rb +22 -0
@@ 188,6 188,28 @@ module Dhall
		end
	end

	class FunctionProxy < Function
		def initialize(callable, curry: true)
			@callable = if !curry
				callable
			elsif callable.respond_to?(:curry)
				callable.curry
			elsif callable.respond_to?(:to_proc)
				callable.to_proc.curry
			else
				callable.method(:call).to_proc.curry
			end
		end

		def call(*args, &block)
			@callable.call(*args.map { |arg| arg&.as_dhall }, &block).as_dhall
		end

		def as_json
			raise "Cannot serialize #{self}"
		end
	end

	class Bool < Expression
		include(ValueSemantics.for_attributes do
			value Bool()

M lib/dhall/normalize.rb => lib/dhall/normalize.rb +11 -2
@@ 120,9 120,18 @@ module Dhall
		end
	end

	class Forall; end
	class FunctionProxy
		def shift(*)
			self
		end

		def substitute(*)
			raise "Cannot substitute #{self}"
		end

	class Bool
		def normalize
			self
		end
	end

	class Variable