Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for macros to reduce code duplication #356

Open
segeljakt opened this issue Mar 13, 2024 · 3 comments
Open

Support for macros to reduce code duplication #356

segeljakt opened this issue Mar 13, 2024 · 3 comments

Comments

@segeljakt
Copy link

segeljakt commented Mar 13, 2024

Hi, currently when using egglog there is some code duplication in my programs. For example:

(push)
;; 2 * (x + 3) = 6 + 2 * x
(let a0 (Mul (I64 2) (Add (Var "x") (I64 3))))
(let b0 (Add (I64 6) (Mul (I64 2) (Var "x"))))

(run-schedule
  (seq
    (run desugar)
    (saturate (run arith))))
(check (= a1 b1))
(pop)

(push)
;; 6 + 2 * 3 = 12
(let a1 (Add (I64 6) (Mul (I64 2) (I64 3))))
(let b1 (I64 12))

(run-schedule
  (seq
    (run desugar)
    (saturate (run arith))))
(check (= a0 b0))
(pop)

If I am not missing something, it is currently not possible to parameterise commands/expressions. The only workaround I see is to write a program in Rust/Python which calls into Egglog.

Would it be possible to add basic support for macros that rely on textual substitution in egglog? This would allow reusing commands such as:

; Define a macro which takes 2 parameters e0 and e1
(macro test (e0 e1)
  ((push)
   (let a e0)
   (let b e1)

   (run-schedule
     (seq
       (run desugar)
       (saturate
         (run-schedule
           (seq
             (run desugar)
             (saturate (run arith))))
   (check (= a b))
   (pop)))
   
; Use the macros
(call-macro test
  ((Mul (I64 2) (Add (Var "x") (I64 3)))
   (Add (I64 6) (Mul (I64 2) (Var "x")))))
  
(call-macro test
  ((Add (I64 6) (Mul (I64 2) (I64 3)))
   (I64 12)))
@oflatt
Copy link
Member

oflatt commented Mar 15, 2024

I agree that macros would be quite useful in egglog. However, text-based macros are usually a terrible source of bugs.
A more promising direction would be to re-use an existing macro system, such as rust's or racket's.
A #lang egglog would work well
This is related to #232

@segeljakt
Copy link
Author

Hmm, I haven't tried Racket but it looks like a good fit. I think there is also a lot of potential in making Egglog more powerful on its own, but maybe this is not important if Egglog is aimed to be a target for code generation.

@oflatt
Copy link
Member

oflatt commented Mar 18, 2024

Yeah, I think egglog as a target vs a language is up in the air right now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants