Class: Dhall::LetIn

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Expression

#&, #*, #+, #annotate, #as_dhall, #cache_key, #call, #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(*parts) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/dhall/binary.rb', line 278

def self.decode(*parts)
  body = Dhall.decode(parts.pop)
  parts.each_slice(3).map { |(var, type, assign)|
    Let.new(
      var:    var,
      assign: Dhall.decode(assign),
      type:   type.nil? ? nil : Dhall.decode(type)
    )
  }.reverse.reduce(body) do |inside, let|
    LetIn.new(let: let, body: inside)
  end
end

Instance Method Details

#as_jsonObject



1818
1819
1820
# File 'lib/dhall/ast.rb', line 1818

def as_json
  flatten.as_json
end

#desugarObject



1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
# File 'lib/dhall/ast.rb', line 1800

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

#eliminateObject



1811
1812
1813
1814
1815
1816
# File 'lib/dhall/ast.rb', line 1811

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

#flattenObject



1791
1792
1793
1794
1795
1796
1797
1798
# File 'lib/dhall/ast.rb', line 1791

def flatten
  flattened = body.is_a?(LetIn) ? body.flatten : body
  if flattened.is_a?(LetBlock)
    LetBlock.new(lets: lets + flattened.lets, body: flattened.body)
  else
    LetBlock.new(lets: lets, body: body)
  end
end

#letsObject



1787
1788
1789
# File 'lib/dhall/ast.rb', line 1787

def lets
  [let]
end

#normalizeObject



435
436
437
# File 'lib/dhall/normalize.rb', line 435

def normalize
  desugar.normalize
end

#shift(amount, name, min_index) ⇒ Object



439
440
441
442
443
444
445
446
# File 'lib/dhall/normalize.rb', line 439

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



448
449
450
451
452
453
454
455
456
457
# File 'lib/dhall/normalize.rb', line 448

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