Class: Dhall::LetIn

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

Instance Method Summary collapse

Methods inherited from Expression

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

Instance Method Details

#as_jsonObject



1637
1638
1639
# File 'lib/dhall/ast.rb', line 1637

def as_json
	[25, *let.as_json, body.as_json]
end

#desugarObject



1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
# File 'lib/dhall/ast.rb', line 1619

def desugar
	Application.new(
		function: Function.new(
			var:  let.var,
			type: let.type,
			body: body
		),
		argument: let.assign
	)
end

#eliminateObject



1630
1631
1632
1633
1634
1635
# File 'lib/dhall/ast.rb', line 1630

def eliminate
	body.substitute(
		Dhall::Variable[let.var],
		let.assign.shift(1, let.var, 0)
	).shift(-1, let.var, 0)
end

#flattenObject



1610
1611
1612
1613
1614
1615
1616
1617
# File 'lib/dhall/ast.rb', line 1610

def flatten
	flattened = body.is_a?(LetIn) ? body.flatten : body
	if flattened.is_a?(LetIn) || flattened.is_a?(LetBlock)
		LetBlock.for(lets: [let] + flattened.lets, body: flattened.body)
	else
		self
	end
end

#letsObject



1606
1607
1608
# File 'lib/dhall/ast.rb', line 1606

def lets
	[let]
end

#normalizeObject



393
394
395
# File 'lib/dhall/normalize.rb', line 393

def normalize
	desugar.normalize
end

#shift(amount, name, min_index) ⇒ Object



397
398
399
400
401
402
403
404
# File 'lib/dhall/normalize.rb', line 397

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

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

#substitute(svar, with_expr) ⇒ Object



406
407
408
409
410
411
412
413
414
415
# File 'lib/dhall/normalize.rb', line 406

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