Class: LyambdaGem::Abstraction

Inherits:
Term
  • Object
show all
Defined in:
lib/lyambda_gem/abstraction.rb

Overview

Implementation of an abstraction (lambda function) in the lambda calculus

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject (readonly)

Returns the value of attribute body.



4
5
6
# File 'lib/lyambda_gem/abstraction.rb', line 4

def body
  @body
end

#parameterObject (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_variablesObject

Список свободных переменных



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

Returns:

  • (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_sObject



51
52
53
# File 'lib/lyambda_gem/abstraction.rb', line 51

def to_s
  "#{parameter}.#{body})"
end