Giter Site home page Giter Site logo

rewrite.jl's Introduction

Rewrite.jl

Travis Build Status AppVeyor Build Status Coverage Status

Rewrite.jl is an efficient symbolic term rewriting engine.


There are three primary steps in the development and use of a rewriting program:

  1. Map each relevant function symbol to an equational theory. For example, we might specify that + is associative and commutative.
  2. Define a system of rules to rewrite with respect to. For example, we might describe a desired rewrite from x + 0 to x, for all x.
  3. Rewrite a concrete term using the rules.

Example

Theory Definition

In this example, we'll simplify boolean propositions.

First, we'll define the theories which each function symbol belongs to. "Free" symbols follow no special axioms during matching, whereas AC symbols will match under associativity and commutativity.

@theory! begin
    F => FreeTheory()
    T => FreeTheory()
    (&) => ACTheory()
    (|) => ACTheory()
    (!) => FreeTheory()
end

Using the @theory! macro, we associate each of our symbols with a theory. Note that F and T will be a nullary (zero-argument) function, so we assign it the FreeTheory.

Rules Definition

Given the defined theory, we now want to describe the rules which govern boolean logic. We include a handful of cases:

@rules Prop [x, y] begin
    x & F := F
    x & T := x

    x | F := x
    x | T := T

    !T := F
    !F := T

    !(x & y) := !x | !y
    !(x | y) := !x & !y
    !(!x)    := x
end

Naming the rewriting system Prop and using x as a variable, we define many rules. To verbalize a few of them:

  • "x and false" is definitely false.
  • "not true" is definitely false.
  • "not (x and y)" is equivalent to "(not x) or (not y)".
  • "not (not x)" is equivalent to whatever x is.

Under the hood, a custom function called Prop was defined, optimized for rewriting with these specific rules.

Rewriting

Let's test it out on some concrete terms. First, we can evaluate some expressions which are based on constants:

julia> @rewrite(Prop, !(T & F) | !(!F))
@term(T())

julia> @rewrite(Prop, !(T & T) | !(!F | F))
@term(F())

We can also bring in our own custom symbols, which the system knows nothing about:

julia> @rewrite(Prop, a & (T & b) & (c & !F))
@term(&(a(), b(), c()))

julia> @rewrite(Prop, F | f(!(b & T)))
@term(f(!(b())))

Success! We've rewritten some boolean terms.

rewrite.jl's People

Contributors

dilumaluthge avatar harrisongrodin avatar masonprotter avatar yingboma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rewrite.jl's Issues

Term printing is not unicode compatible

Currently, the printing of Terms assumes that the string representation of the term may be sanely indexed which is not actually the case whenever there is unicode in the Term.

MWE:

julia> using Terms

julia> @term 
Error showing value of type Term:
ERROR: StringIndexError(":(@term :ω)", 11)
Stacktrace:
 [1] string_index_err(::String, ::Int64) at ./strings/string.jl:12
 [2] getindex at ./strings/string.jl:248 [inlined]
 [3] show(::IOContext{REPL.Terminals.TTYTerminal}, ::Term) at /Users/mason/.julia/packages/Terms/rb9Kk/src/term.jl:58
 [4] show(::IOContext{REPL.Terminals.TTYTerminal}, ::MIME{Symbol("text/plain")}, ::Term) at ./sysimg.jl:194
 [5] display(::REPL.REPLDisplay, ::MIME{Symbol("text/plain")}, ::Any) at /Users/mason/julia/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:131
 [6] display(::REPL.REPLDisplay, ::Any) at /Users/mason/julia/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:135
 [7] display(::Any) at ./multimedia.jl:287
 [8] #invokelatest#1 at ./essentials.jl:742 [inlined]
 [9] invokelatest at ./essentials.jl:741 [inlined]
 [10] print_response(::IO, ::Any, ::Any, ::Bool, ::Bool, ::Any) at /Users/mason/julia/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:155
 [11] print_response(::REPL.AbstractREPL, ::Any, ::Any, ::Bool, ::Bool) at /Users/mason/julia/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:140
 [12] (::getfield(REPL, Symbol("#do_respond#38")){Bool,getfield(REPL, Symbol("##48#57")){REPL.LineEditREPL,REPL.REPLHistoryProvider},REPL.LineEditREPL,REPL.LineEdit.Prompt})(::Any, ::Any, ::Any) at /Users/mason/julia/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:714
 [13] #invokelatest#1 at ./essentials.jl:742 [inlined]
 [14] invokelatest at ./essentials.jl:741 [inlined]
 [15] run_interface(::REPL.Terminals.TextTerminal, ::REPL.LineEdit.ModalInterface, ::REPL.LineEdit.MIState) at /Users/mason/julia/usr/share/julia/stdlib/v1.1/REPL/src/LineEdit.jl:2273
 [16] run_frontend(::REPL.LineEditREPL, ::REPL.REPLBackendRef) at /Users/mason/julia/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:1035
 [17] run_repl(::REPL.AbstractREPL, ::Any) at /Users/mason/julia/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:192
 [18] (::getfield(Base, Symbol("##734#736")){Bool,Bool,Bool,Bool})(::Module) at ./client.jl:362
 [19] #invokelatest#1 at ./essentials.jl:742 [inlined]
 [20] invokelatest at ./essentials.jl:741 [inlined]
 [21] run_main_repl(::Bool, ::Bool, ::Bool, ::Bool, ::Bool) at ./client.jl:346
 [22] exec_options(::Base.JLOptions) at ./client.jl:284
 [23] _start() at ./client.jl:436

Here is some stuff to show some intuition for the problem:

str = "a ω b"

julia> length(str)
5

julia> str[1]
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

julia> str[2]
' ': ASCII/Unicode U+0020 (category Zs: Separator, space)

julia> str[3]
'ω': Unicode U+03c9 (category Ll: Letter, lowercase)

julia> str[4]
ERROR: StringIndexError("a ω b", 4)
Stacktrace:
 [1] string_index_err(::String, ::Int64) at ./strings/string.jl:12
 [2] getindex_continued(::String, ::Int64, ::UInt32) at ./strings/string.jl:218
 [3] getindex(::String, ::Int64) at ./strings/string.jl:211
 [4] top-level scope at none:0

julia> eachindex(str)
Base.EachStringIndex{String}("a ω b")

julia> for i in eachindex(str)
       println(str[i])
       end
a

ω

b

Cannot install package

Hi,

just tried to install your package following your JuliaCon talk. But I hit this rather strange problem:

(computations) pkg> add https://github.com/HarrisonGrodin/Rewrite.jl.git
  Updating git-repo `https://github.com/HarrisonGrodin/Rewrite.jl.git`
 Resolving package versions...
  Updating `~/Dropbox/math/computations/Project.toml`
  [faceda62] + Rewrite v0.1.0 #master (https://github.com/HarrisonGrodin/Rewrite.jl.git)
  Updating `~/Dropbox/math/computations/Manifest.toml`
  [864edb3b] + DataStructures v0.17.0
  [faceda62] + Rewrite v0.1.0 #master (https://github.com/HarrisonGrodin/Rewrite.jl.git)

julia> using Rewrite
[ Info: Precompiling Rewrite [faceda62-1099-11e9-2df8-4d2b97a880ac]
ERROR: LoadError: LoadError: LoadError: cannot replace module Rewrite during compilation
Stacktrace:
 [1] include at ./boot.jl:326 [inlined]
 [2] include_relative(::Module, ::String) at ./loading.jl:1038
 [3] include at ./sysimg.jl:29 [inlined]
 [4] include(::String) at /Users/sascha/.julia/packages/Rewrite/DD0FH/src/Rewrite.jl:1
 [5] top-level scope at none:0
 [6] include at ./boot.jl:326 [inlined]
 [7] include_relative(::Module, ::String) at ./loading.jl:1038
 [8] include at ./sysimg.jl:29 [inlined]
 [9] include(::String) at /Users/sascha/.julia/packages/Rewrite/DD0FH/src/Rewrite.jl:1
 [10] top-level scope at none:0
 [11] include at ./boot.jl:326 [inlined]
 [12] include_relative(::Module, ::String) at ./loading.jl:1038
 [13] include(::Module, ::String) at ./sysimg.jl:29
 [14] top-level scope at none:2
 [15] eval at ./boot.jl:328 [inlined]
 [16] eval(::Expr) at ./client.jl:404
 [17] top-level scope at ./none:3
in expression starting at /Users/sascha/.julia/packages/Rewrite/DD0FH/src/rewrite.jl:1
in expression starting at /Users/sascha/.julia/packages/Rewrite/DD0FH/src/core.jl:19
in expression starting at /Users/sascha/.julia/packages/Rewrite/DD0FH/src/Rewrite.jl:3
ERROR: Failed to precompile Rewrite [faceda62-1099-11e9-2df8-4d2b97a880ac] to /Users/sascha/.julia/compiled/v1.1/Rewrite/Xb8Ps.ji.
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] compilecache(::Base.PkgId, ::String) at ./loading.jl:1197
 [3] _require(::Base.PkgId) at ./loading.jl:960
 [4] require(::Base.PkgId) at ./loading.jl:858
 [5] require(::Module, ::Symbol) at ./loading.jl:853

Typed Theories

I'm definitely out on a limb here, but if I understand the idea of Rewrite.jl and Catlab.jl correctly, they are both building out systems for rewriting in General Algebraic Theories. In the formulation necessary for Category Theory, every term has an objected associated with it and every expression has an two objects associated with it (domain and codomain), which can be thought of as a types. And you are only allowed to build expressions that "typecheck" under the rules of function composition f(::A)::B \circ g(::B)::C = fg(::A)::C

I understand that Rewrite is focused on symbolic algebra for numerical methods, which is done completely in categories with only one object, typically Z, R, or C, but having the ability to include typed expressions would be really helpful for more complex modeling activities.

Integration of MatchPy's algorithms

MatchPy is a library claiming to implement state-of-the-art associative-commutative pattern matching in Python.

In particular, they claim:

We use the Hopcroft-Karp algorithm [HK73] to find an initial maximum
matching. However, since we are also interested in all matches
and the initial matching might have incompatible substitutions, we
use the algorithm described by Uno, Fukuda and Matsui [FM94],
[Uno97] to enumerate all maximum matchings.

MatchPy has been integrated into SymPy. MatchPy is also able to generate Python decision tree. See the generated RUBI decision tree. It would be nice to see how it would work with Julia's metaprogramming.

MatchPy's code generator has been ported into C++ as part of SymEngine (see the matchPyCpp subfolder on github).

The Hopcroft-Karp and Uno-Fukuda-Matsui algorithms have been ported into C++ (although the current lack of support for coroutines makes the code pretty slow). See the Hopcroft-Karp implementation and the Uno-Fukuda-Matsui implementation in SymEngine, which are translations of MatchPy's Python code.

Is there the possibility of integrating these algorithms into Rewrite.jl?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.