New Proposal: EasyMacros
Richard Eisenberg
eir at cis.upenn.edu
Wed May 18 21:09:31 UTC 2016
This strikes me as a more powerful form of RebindableSyntax, blazing new territory. (Existing RebindableSyntax never use Template Haskell.)
I assume you've emailed the Haskell Prime list because you wish this to be considered for inclusion in the next version of the standard. However, an extension this large would need to be vetted by implementation in a compiler (assumedly, GHC) before we can consider inclusion in a standard. If you wish to pursue this, I recommend generating a groundswell of interest from the community and writing a more formal proposal detailing exactly how such macros should be desugared into Template Haskell. You give an example below, and that is helpful, but we would need a more formal description.
This is, certainly, an interesting idea and would allow for yet more flexible extensions to the language, all without changing the compiler.
Thanks,
Richard
On May 18, 2016, at 3:10 PM, Anton Felix Lorenzen <anfelor at posteo.de> wrote:
> Hello list,
>
> I want to propose a new way of writing template-haskell macros.
> It is based on the idea, that syntax like this:
> do {
> x <- getLine;
> putStrLn x;
> }
> should be parsed into:
> $( do
> [ [| x <- getLine |]
> , [| putStrLn x |]
> ] )
> Then a function (do :: NonEmpty ExpQ -> ExpQ) will be called
> and executed.
>
> This would allow for a couple of useful macros:
> - do
> - case
> - fold -- fold the ExpQs into one, see below for details
> - eval -- evaluate ExpQs at compile-time
> - (maybe even) hamlet -- evaluate hamlet QQ
> - CPP replacements?
>
> Nothing will be added which isn't possible today.
> Nonetheless it will allow for a much cleaner syntax
> and by this make things usable which weren't usable before.
>
> Also it might be easier to implement new features.
> For example ApplicativeDo and RecursiveDo
> could have been implemented by a hackage package.
> MultiWayIf could be a function, too.
>
> apply
> [1..(10*365)]
> map (replicate 3)
> concat
>
> conc
> "Hello World! \n"
> "How are you? \n"
>
> Greetings,
> Anton
>
> ===== Example Macros =====
>
> see in context: https://github.com/anfelor/EasyMacros
>
> conc = fold [| (++) |]
> apply = fold [|flip ($)|]
>
> fold :: ExpQ -- (a -> b -> c)
> -> NonEmpty (Q Exp) -> Q Exp
> fold _ (a :| []) = a
> fold fn (a :| b : xs) = fold fn (([| fn $a $b |]) :| xs)
>
> do_ :: NonEmpty (Q Stmt) -> Q Exp
> do_ (x :| []) = x >>= \case
> BindS _ _ -> fail "The last statement in a 'do' block must be an expression"
> NoBindS e -> return e
> do_ (x :| xs) = x >>= \case
> BindS pat exp -> [| $(return exp) >>= \ $(return pat) -> $(do_ (fromList xs)) |]
> NoBindS exp -> [| $(return exp) >> $(do_ (fromList xs)) |]
> _______________________________________________
> Haskell-prime mailing list
> Haskell-prime at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-prime
More information about the Haskell-prime
mailing list