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, FunctionProxy

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_cbor, #to_proc, #to_s, #|

Class Method Details

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



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dhall/binary.rb', line 58

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



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

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



178
179
180
181
182
183
# File 'lib/dhall/ast.rb', line 178

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

#>>(other) ⇒ Object



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

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

#as_jsonObject



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

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

#bindingObject



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

def binding
  to_proc.binding
end

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



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

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



196
197
198
# File 'lib/dhall/ast.rb', line 196

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