Class: LyambdaGem::Abstraction
- Defined in:
- lib/lyambda_gem/abstraction.rb
Overview
Implementation of an abstraction (lambda function) in the lambda calculus
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#parameter ⇒ Object
readonly
Returns the value of attribute parameter.
Instance Method Summary collapse
-
#free_variables ⇒ Object
Список свободных переменных.
-
#fresh_variable(term) ⇒ Object
Получение новой переменной относительно терма (для правила 7).
-
#initialize(parameter, body) ⇒ Abstraction
constructor
A new instance of Abstraction.
- #reduce(strategy: :normal_order) ⇒ Object
- #reduceable? ⇒ Boolean
-
#substitute(term, variable) ⇒ Object
Подстановка.
- #to_s ⇒ Object
Constructor Details
#initialize(parameter, body) ⇒ Abstraction
Returns a new instance of Abstraction.
6 7 8 9 |
# File 'lib/lyambda_gem/abstraction.rb', line 6 def initialize(parameter, body) @parameter = parameter @body = body end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
4 5 6 |
# File 'lib/lyambda_gem/abstraction.rb', line 4 def body @body end |
#parameter ⇒ Object (readonly)
Returns the value of attribute parameter.
4 5 6 |
# File 'lib/lyambda_gem/abstraction.rb', line 4 def parameter @parameter end |
Instance Method Details
#free_variables ⇒ Object
Список свободных переменных
12 13 14 |
# File 'lib/lyambda_gem/abstraction.rb', line 12 def free_variables @body.free_variables - Set.new([@parameter]) end |
#fresh_variable(term) ⇒ Object
Получение новой переменной относительно терма (для правила 7)
17 18 19 20 21 22 23 24 25 |
# File 'lib/lyambda_gem/abstraction.rb', line 17 def fresh_variable(term) cnt = 1 fv = term.free_variables while fv.any?{|variable| variable.name == "z#{cnt}"} cnt += 1 end return Variable.new("z#{cnt}") end |
#reduce(strategy: :normal_order) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/lyambda_gem/abstraction.rb', line 44 def reduce(strategy: :normal_order) #puts " abs:#{self.to_s} | r-able: #{reduceable?}" return self unless reduceable? return Abstraction.new(@parameter, @body.reduce) end |
#reduceable? ⇒ Boolean
40 41 42 |
# File 'lib/lyambda_gem/abstraction.rb', line 40 def reduceable? @body.reduceable? end |
#substitute(term, variable) ⇒ Object
Подстановка
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/lyambda_gem/abstraction.rb', line 28 def substitute(term, variable) #puts "p:#{@parameter} | body:#{@body} | t:#{term} | v:#{variable}" return self if variable == @parameter return self if !@body.free_variables.include?(variable) return Abstraction.new(@parameter, @body.substitute(term, variable)) if !term.free_variables.include?(@parameter) new_variable = fresh_variable((Application.new(term, @body))) return Abstraction.new(new_variable, @body.substitute(new_variable, @parameter).substitute(term, variable)) end |
#to_s ⇒ Object
51 52 53 |
# File 'lib/lyambda_gem/abstraction.rb', line 51 def to_s "(λ#{parameter}.#{body})" end |