Class: Dhall::Function

Inherits:
Expression show all
Defined in:
lib/dhall/ast.rb,
lib/dhall/binary.rb,
lib/dhall/normalize.rb

Direct Known Subclasses

Forall, FunctionProxyRaw

Constant Summary collapse

@@alpha_normalization =
true

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Expression

#&, #*, #+, #as_dhall, #cache_key, #concat, #deep_merge, #deep_merge_type, #dhall_eq, #digest, #fetch, #fusion, #merge, #resolve, #slice, #to_binary, #to_cbor, #to_proc, #to_s, #|

Class Method Details

.decode(var_or_type, type_or_body, body_or_nil = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dhall/binary.rb', line 63

def self.decode(var_or_type, type_or_body, body_or_nil=nil)
	type_or_body = Dhall.decode(type_or_body)

	if body_or_nil.nil?
		of_arguments(Dhall.decode(var_or_type), body: type_or_body)
	else
		raise ArgumentError, "explicit var named _" if var_or_type == "_"

		body_or_nil = Dhall.decode(body_or_nil)
		new(var: var_or_type, type: type_or_body, body: body_or_nil)
	end
end

.disable_alpha_normalization!Object



76
77
78
# File 'lib/dhall/normalize.rb', line 76

def self.disable_alpha_normalization!
	@@alpha_normalization = false
end

.enable_alpha_normalization!Object



80
81
82
# File 'lib/dhall/normalize.rb', line 80

def self.enable_alpha_normalization!
	@@alpha_normalization = true
end

.of_arguments(*types, body:) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/dhall/ast.rb', line 156

def self.of_arguments(*types, body:)
	types.reverse.reduce(body) do |inner, type|
		new(
			var:  "_",
			type: type,
			body: inner
		)
	end
end

Instance Method Details

#<<(other) ⇒ Object



180
181
182
183
184
185
# File 'lib/dhall/ast.rb', line 180

def <<(other)
	FunctionProxy.new(
		->(*args, &block) { call(other.call(*args, &block)) },
		curry: false
	)
end

#>>(other) ⇒ Object



187
188
189
190
191
192
# File 'lib/dhall/ast.rb', line 187

def >>(other)
	FunctionProxy.new(
		->(*args, &block) { other.call(call(*args, &block)) },
		curry: false
	)
end

#as_jsonObject



202
203
204
205
206
207
208
# File 'lib/dhall/ast.rb', line 202

def as_json
	if var == "_"
		[1, type.as_json, body.as_json]
	else
		[1, var, type.as_json, body.as_json]
	end
end

#bindingObject



194
195
196
# File 'lib/dhall/ast.rb', line 194

def binding
	to_proc.binding
end

#call(*args, &block) ⇒ Object Also known as: [], ===



166
167
168
169
170
171
172
173
174
175
# File 'lib/dhall/ast.rb', line 166

def call(*args, &block)
	args += [block] if block
	args.map! { |arg| arg&.as_dhall }
	return super if args.length > 1

	body.substitute(
		Variable.new(name: var),
		args.first.shift(1, var, 0)
	).shift(-1, var, 0).normalize
end

#curryObject



198
199
200
# File 'lib/dhall/ast.rb', line 198

def curry
	self
end

#normalizeObject



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dhall/normalize.rb', line 103

def normalize
	return super unless alpha_normalize?
	with(
		var:  "_",
		type: type&.normalize,
		body: body
		      .shift(1, "_", 0)
		      .substitute(Variable[var], Variable["_"])
		      .shift(-1, var, 0)
		      .normalize
	)
end

#shift(amount, name, min_index) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/dhall/normalize.rb', line 84

def shift(amount, name, min_index)
	return super unless var == name

	with(
		type: type.shift(amount, name, min_index),
		body: body.shift(amount, name, min_index + 1)
	)
end

#substitute(svar, with_expr) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/dhall/normalize.rb', line 93

def substitute(svar, with_expr)
	with(
		type: type&.substitute(svar, with_expr),
		body: body.substitute(
			var == svar.name ? svar.with(index: svar.index + 1) : svar,
			with_expr.shift(1, var, 0)
		)
	)
end