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

Add a template haskell procedure to unify the construction of values and values in UnionM #116

Closed
lsrcz opened this issue Aug 23, 2023 · 1 comment
Assignees
Labels
feature request New feature request

Comments

@lsrcz
Copy link
Owner

lsrcz commented Aug 23, 2023

Suppose we have a type:

data AST
  = Add (UnionM AST) (UnionM AST)
  | Mul (UnionM AST) (UnionM AST)
  | Lit SymInteger
   deriving (Show, Generic)
   deriving (Mergeable) via (Default AST)

Consider how to construct a value of this type. To construct the value (a + b) * (c + d), we may have to write:

Mul
  (mrgReturn $ Add (mrgReturn $ Lit "a") (mrgReturn $ Lit "b"))
  (mrgReturn $ Add (mrgReturn $ Lit "c") (mrgReturn $ Lit "d"))

This is ridiculously verbose.

A better way to construct this is to provide a type class that constructs an AST or a UnionM AST in a unified way:

class ASTBuilder ast where
  add :: UnionM AST -> UnionM AST -> ast
  mul :: UnionM AST -> UnionM AST -> ast
  lit :: SymInteger -> ast

and implement the following two instances:

instance ASTBuilder AST where
  add = Add
  mul = Mul
  lit = Lit

instance ASTBuilder (UnionM AST) where
  add l r = mrgReturn $ Add l r
  mul l r = mrgReturn $ Mul l r
  lit v = mrgReturn $ Lit v

Then we can construct (a + b) * (c + d) by

Mul (add (lit "a") (lit "b")) (add (lit "c") (lit "d"))

as expected.

This can be done with Template Haskell. It may be a good idea to have a way to configure the function names to avoid name clash.

@lsrcz lsrcz added the feature request New feature request label Aug 23, 2023
@lsrcz lsrcz self-assigned this Aug 23, 2023
@lsrcz
Copy link
Owner Author

lsrcz commented Jun 24, 2024

This issue is superseded by our design on unified interfaces.

@lsrcz lsrcz closed this as completed Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature request
Projects
None yet
Development

No branches or pull requests

1 participant