• Stars
    star
    489
  • Rank 87,169 (Top 2 %)
  • Language
    Ruby
  • License
    Other
  • Created about 11 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Example code for Understanding Computation

Understanding Computation example code

This is the example code for Understanding Computation, an O’Reilly book about computation theory. (Here’s a sample chapter.) Ruby 1.9 or 2.0 is required.

Right now it’s a pretty rough dump of code from the book. Each chapter has its own directory:

Each directory contains definitions of the classes implemented in that chapter. There’s also a file named after each chapter (e.g. just_add_power.rb) that can be required to load all the code for that chapter.

For example:

$ irb -I.
>> require 'universality_is_everywhere'
=> true
>> identity = SKICall.new(SKICall.new(S, K), SKICall.new(K, K))
=> S[K][K[K]]
>> x = SKISymbol.new(:x)
=> x
>> expression = SKICall.new(identity, x)
=> S[K][K[K]][x]
>> while expression.reducible?; puts expression; expression = expression.reduce; end; puts expression
S[K][K[K]][x]
K[x][K[K][x]]
K[x][K]
x
=> nil

If you run bundle install to install Treetop, you can try out the parsers:

$ bundle exec irb -I.
>> require 'treetop'
=> true
>> Treetop.load('the_meaning_of_programs/parser/simple')
=> SimpleParser
>> require 'the_meaning_of_programs'
=> true
>> program = SimpleParser.new.parse('while (x < 5) { x = x * 3 }').to_ast
=> «while (x < 5) { x = x * 3 }»
>> program.reduce(x: Number.new(3))
=> [«if (x < 5) { x = x * 3; while (x < 5) { x = x * 3 } } else { do-nothing }», {:x=>«3»}]
>> program.evaluate(x: Number.new(3))
=> {:x=>«9»}
>> program.to_ruby
=> "-> e { while (-> e { (-> e { e[:x] }).call(e) < (-> e { 5 }).call(e) }).call(e); e = (-> e { e.merge({ :x => (-> e { (-> e { e[:x] }).call(e) * (-> e { 3 }).call(e) }).call(e) }) }).call(e); end; e }"
>> eval(program.to_ruby).call(x: 3)
=> {:x=>9}
$ bundle exec irb -I.
>> require 'treetop'
=> true
>> Treetop.load('programming_with_nothing/lambda_calculus/lambda_calculus')
=> LambdaCalculusParser
>> require 'programming_with_nothing'
=> true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[ɩ[ɩ[ɩ]]]]][ɩ[ɩ]]]][ɩ[ɩ[ɩ[ɩ]]][ɩ[ɩ]]]][inc][zero]
>> expression = expression.reduce while expression.reducible?
=> nil
>> expression
=> inc[inc[zero]]

If you have any questions, please get in touch via Twitter or email. If you find any bugs or other programs with the code, please open an issue.

More Repositories

1

monads

Simple Ruby implementations of some common monads.
Ruby
596
star
2

nothing

Programming with Nothing
Ruby
241
star
3

utf-8-challenges

A short tutorial on UTF-8. Run with `ruby utf_8_challenges.rb`, unskip each test and make it pass!
Ruby
47
star
4

kanren

An example Ruby implementation of μKanren.
Ruby
22
star
5

tradfri

A Ruby interface to IKEA’s smart lighting system
Ruby
18
star
6

dual_number

A Ruby implementation of dual numbers.
Ruby
17
star
7

vector_space

A Ruby library for treating multidimensional values as elements of a vector space.
Ruby
13
star
8

wasminna

Live coding a WebAssembly interpreter in pure Ruby with no dependencies, guided by the Wasm spec’s test suite
Ruby
13
star
9

something

Programming with Something
Ruby
9
star
10

little_scheme

Growing a little Scheme interpreter, guided by The Little Schemer
Ruby
9
star
11

inference-rules

A simple implementation of generic inference rules
Ruby
7
star
12

react-workshop

What Even Is A React (And So Can You!)
JavaScript
6
star
13

govuk-exhibit

A GOV.UK exhibit
Ruby
4
star
14

neural-network

HTML
2
star
15

subsequence_matchers

Ruby
2
star
16

lox

An implementation of the Lox language from Robert Nystrom’s “Crafting Interpreters”
Ruby
2
star
17

genetic-algorithms

A scrappy genetic algorithm visualisation for London Computation Club
HTML
1
star
18

capybara-envjs-button-bug

Ruby
1
star
19

rubyforge-redirects

Crowdsourced redirects for old RubyForge URLs
Ruby
1
star
20

recursion-workshop

JavaScript
1
star
21

mandelbrot

Some scrappy Mandelbrot visualisations for London Computation Club
HTML
1
star
22

heylist

Ruby
1
star
23

negative-numbers

Representing negative numbers in Ruby
Ruby
1
star
24

tapl

A Ruby implementation of typecheckers from Types and Programming Languages
Ruby
1
star
25

hoas

An implementation of higher-order abstract syntax in Ruby
Ruby
1
star