Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save practicingruby/112055 to your computer and use it in GitHub Desktop.

Select an option

Save practicingruby/112055 to your computer and use it in GitHub Desktop.
# http://blog.sigfpe.com/2009/05/three-projections-of-doctor-futamura.html
Coin = Struct.new(:currency)
def minter(blank)
blank.currency = :dollars
end
def interpreter(blank, currency)
blank.currency = currency
end
def compiler(currency)
lambda { |b| b.currency = currency }
end
def proj1(inter, pattern)
lambda { |x| inter[x, pattern] }
end
def proj2(spec, inter)
lambda { |pattern| spec[inter, pattern] }
end
def proj3(spec_spec, spec)
lambda { |inter| spec_spec[inter, spec] }
end
# PROJ 3 EXAMPLE
=begin
c = Coin.new("undefined")
my_spec = lambda { |inter, pattern| lambda { |x| inter[x, pattern] } }
my_inter = lambda { |blank, currency| blank.currency = "#{currency}" }
my_spec_spec = lambda { |i, s| lambda { |x| s[i, x] } }
my_compiler_generator = proj3(my_spec_spec, my_spec)
my_compiler = my_compiler_generator[my_inter]
my_minter = my_compiler[:dollars]
my_compiler2 = my_compiler_generator[lambda { |x,y| x + y }]
plus10 = my_compiler2[10]
puts plus10[17]
puts "before entering minter: #{c.currency}"
my_minter[c]
puts "after leaving minter: #{c.currency}"
=end
# PROJ 2 EXAMPLE
=begin
c = Coin.new("undefined")
my_spec = lambda { |inter, pattern| lambda { |x| inter[x, "#{pattern} from spec"] } }
my_inter = lambda { |blank, currency| blank.currency = "#{currency}" }
my_inter2 = lambda { |blank, currency| blank.currency = "#{currency} logo"}
my_compiler = proj2(my_spec, my_inter)
my_minter = my_compiler[:dollars]
my_compiler2 = proj2(my_spec, my_inter2)
my_minter2 = my_compiler2[:dollars]
puts "before entering minter: #{c.currency}"
my_minter[c]
puts "after leaving minter: #{c.currency}"
=end
# PROJ 1 EXAMPLE
=begin
c = Coin.new("undefined")
my_inter = lambda { |blank, currency| blank.currency = "#{currency}" }
my_inter2 = lambda { |blank, currency| blank.currency = "#{currency} logo"}
my_minter = proj1(my_inter, :dollars)
my_minter2 = proj1(my_inter2, :dollars)
puts "before entering minter: #{c.currency}"
my_minter2[c]
puts "after leaving minter: #{c.currency}"
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment