Giter Club home page Giter Club logo

babble's Introduction

Babble ci-status-badge license-badge

Looking for the POPL 23 artifact? Head to the POPL23.md file on the popl23 branch.

Experimental library learning using anti-unification of e-graphs.

Building

$ git clone https://github.com/dcao/babble.git
$ cd babble
$ make

Examples

Learning filter:

$ cargo run --release --bin=list -- examples/filter-list.bab

Learning nested functions:

$ cargo run --release --bin=smiley -- examples/nested-functions.bab

How it works

As a simple example, consider the following list program (with size 29):

(list
 (cons 0 (cons 0 (cons 0 empty)))
 (cons 1 (cons 1 (cons 1 empty)))
 (cons 2 (cons 2 (cons 2 empty)))
 (cons 3 (cons 3 (cons 3 empty))))

Here babble learns the following compressed program (with size 23):

(lib f8 (λ (cons $0 (cons $0 (cons $0 empty)))) 
  (list (@ f8 0) (@ f8 1) (@ f8 2) (@ f8 3)))

To this end, it first it adds the initial expression to an e-graph, and then goes through the following steps.

Step 1: Anti-unification

Babble anti-unifies (AU) each pair of e-nodes in the e-graph in a bottom-up fashion to avoid recomputing AU for subterms (this is actually based on DFTA instersection). For example:

AU empty                            empty                            = empty
AU 0                                1                                = ?x01            -- indexed by the e-classes of 0 and 1
AU (cons 0 empty)                   (cons 1 empty)                   = cons ?x01 empty -- reuses AU results for subterms
...
AU (cons 0 (cons 0 (cons 0 empty))) (cons 1 (cons 1 (cons 1 empty))) = (cons ?x01 (cons ?x01 (cons ?x01 empty)))

Step 2: Generate abstraction rewrites

Now for each anti-unification result that is not just a variable or constant term, babble generates a rewrite rule that would turn matching terms into an application of a newly-defined library function.

For example, the AU result (cons ?x01 (cons ?x01 (cons ?x01 empty))) gives rise to the rewrite rule:

(cons ?x01 (cons ?x01 (cons ?x01 empty)))  =>  (fun f (lambda (cons $0 (cons $0 (cons $0 empty)))) (f ?x01))

(the name f will be fresh for each rule). This rule, when applied, will result in rewriting each one of the three-element lists in the original term into a definition of a new library function followed by its application to the appropriate argument (i.e. 0, 1, 2).

Step 3: Merging function definitions

In the next step, we give egg all the generated abstraction rewrites together with some fixed rules that propagate fun definitions up the term and merge different funs with the same definition when they are propagated from two sides of a cons, list, or some other constructor.

For example, this rule propagates fun on top of cons:

(cons (fun ?name ?def ?body) ?expr)   =>   (fun ?name ?def (cons ?body ?expr))      if ?name not free in ?expr

And this one merges two funs with the same definition from two sides of cons:

(cons (fun ?name ?def ?body1) (fun ?name ?def ?body2)) => (fun ?name ?def (cons ?body1 ?body2))

babble's People

Contributors

chandrakananandi avatar dcao avatar mwillsey avatar nadia-polikarpova avatar rosekunkel avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

babble's Issues

Identical expressions are not compressed.

On 9cf4759, the following is totally uncompressed:

(list
  (@ foo (@ (@ + (@ f x)) (@ f x)))
  (@ foo (@ (@ + (@ f x)) (@ f x)))
)

Changing basically anything about one of the inputs, (foo -> bar, x -> y) makes it work again.

Exporting compression results in DreamCoder format

Hello,
using the code from compression, we can read and compress inputs in the DreamCoder format. This seems to work, but am I overlooking any functionality for exporting compression results in the same format (e.g. as CompressionOutput as defined in dreamcoder/json.rs? This seems like basic functionality, but I'm confused by all the different experiment classes. My current understanding is that this is not implemented, and I'll have to manually post-process the ExperimentResult. This is of course possible, but appears a bit indirect - is there some simple way to access the invented library functions (and rewritten frontiers) such that we can parse them into the correct format?

Thanks for your help!

AU does not extract all the common structure

Running smiley on the following input:

(+ (move -20 0 (+ (scale 5 circle)
                  (move -2 -1.5 (rotate 90 (scale 2 line)))
                  (move 2 -1.5 (rotate 90 (scale 2 line)))
                  (move 0 2 (scale 4 line))))
   (move -7.5 0 (+ (scale 5 circle)
                   (move -2 -1.5 (rotate 90 (scale 2 line)))
                   (move 2 -1.5 (rotate 90 (scale 2 line)))
                   (move 0 2 (scale 1 circle))))
   (move 5 0 (+ (scale 5 circle)
                (move -2 -1.5 (rotate 90 (scale 1 circle)))
                (move 2 -1.5 (rotate 90 (scale 1 circle)))
                (move 0 2 (scale 1 circle))))
   (move 17.5 0 (+ (scale 5 circle)
                   (move -2 -1.5 (rotate 90 (scale 1 circle)))
                   (move 2 -1.5 (rotate 90 (scale 1 circle)))
                   (move 0 2 (scale 4 line)))))

produces the following output:

lib l15 =
  λx1 x2 x3 -> [scale 5 circle,
    move <-2, -1.5> (rotate 90 (scale x3 x2)),
    move <2, -1.5> (rotate 90 (scale x3 x2)),
    x1]
in
  [move <-20, 0> (l15 (move <0, 2> (scale 4 line)) line 2),
    move <-7.5, 0> (l15 (move <0, 2> (scale 1 circle)) line 2),
    move <5, 0> (l15 (move <0, 2> (scale 1 circle)) circle 1),
    move <17.5, 0> (l15 (move <0, 2> (scale 4 line)) circle 1)]

The issue here is that the four expressions of the form (move <0, 2> (scale ? ?)) are all abstracted into x1, which is incorrect AU, since they share top-level structure. The suspicious rewrite shows up in the log as "anti-unify 6".

DSRs mess up anti-unification

Running smiley on the following example (without DSRs):

(+ 
   (move 1 0 circle)
   (move 2 0 circle)
   (move 3 0 circle)
   (move 4 0 circle)
   (move 5 0 circle)
   (move 6 0 circle)
)

produces the expected result:

lib l0 =
  λx1 -> move <x1, 0> circle
in
  [l0 1,
    l0 2,
    l0 3,
    l0 4,
    l0 5,
    l0 6]

Adding a DSR "circle" => "(scale 1 circle)" works well, and even allows to obtain the above result if some of the circles in the input are scaled by 1.

However, adding a more general rewrite ?x -> (scale 1 ?x) results in not learning any libraries, and moreover returning an expression that is larger than the original:

scale 1 [move <1, 0> circle,
  move <2, 0> circle,
  move <3, 0> circle,
  move <4, 0> circle,
  move <5, 0> circle,
  move <6, 0> circle]

There might be two bugs here:

  1. AU always returns empty for all pairs of states except the diagonal (might have to do with the e-graph being too loopy?)
  2. Extraction also picks the wrong expression (not the original)

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.