Module: Rucas::Rewrite

Defined in:
lib/rucas/rewrite.rb

Overview

Tools for recursively rewriting expressions based on rules.

Class Method Summary collapse

Class Method Details

.make_rules(&block) ⇒ Object

Construct an ordered hash of rewrite rules.

Example:

my_rules = make_rules {
  var :x
  rule x + 0 => x
  rule 0 + x => x
}


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rucas/rewrite.rb', line 19

def self.make_rules &block
  s = Scope.new
  class <<s
    def dict
      @dict ||= Dictionary[]
    end
    def rule rule
      raise unless rule.size == 1
      dict[rule.keys.first] = Symbolic::Expr.make(rule.values.first)
    end
  end
  s.rucas(&block)
  s.dict
end