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

#&, #*, #+, #annotate, #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



87
88
89
# File 'lib/dhall/normalize.rb', line 87

def self.disable_alpha_normalization!
	@@alpha_normalization = false
end

.enable_alpha_normalization!Object



91
92
93
# File 'lib/dhall/normalize.rb', line 91

def self.enable_alpha_normalization!
	@@alpha_normalization = true
end

.of_arguments(*types, body:) ⇒ Object



160
161
162
163
164
165
166
167
168
# File 'lib/dhall/ast.rb', line 160

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



184
185
186
187
188
189
# File 'lib/dhall/ast.rb', line 184

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

#>>(other) ⇒ Object



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

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

#as_jsonObject



206
207
208
209
210
211
212
# File 'lib/dhall/ast.rb', line 206

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

#bindingObject



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

def binding
	to_proc.binding
end

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



170
171
172
173
174
175
176
177
178
179
# File 'lib/dhall/ast.rb', line 170

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



202
203
204
# File 'lib/dhall/ast.rb', line 202

def curry
	self
end

#normalizeObject



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/dhall/normalize.rb', line 114

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



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

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



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

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