An idea for a different style of metaprogramming evaluation using the optimiser
Matthew Pickering
matthewtpickering at gmail.com
Wed Feb 28 11:26:35 UTC 2018
You have identified a small inaccuracy in my original email.
It is true that ultimately 3 == 0 will be evaluated but these constant
folding opportunities are only applied later after other inlining
takes place.
It becomes quite crucial under this scheme when different
optimisations happen because when we inline a recursive function, we
have to make sure to apply the right evaluation steps before trying to
simplify the function body.
In my hacked together implementation of this, constant folding happens
too late so only the algebraic version teminated.
Cheers,
Matt
On Tue, Feb 27, 2018 at 5:01 PM, Joachim Breitner
<mail at joachim-breitner.de> wrote:
> Hi,
>
> something like this would be great. I don’t have a sense yet of what
> “something” should be like.
>
>
> Am Dienstag, den 27.02.2018, 09:59 +0000 schrieb Matthew Pickering:
>> To go back to the power example, the recursive
>> condition would have to be an inductively defined natural (data N = Z
>> | S N) rather than an Int as the comparison operator for integers
>> can't be evaluated by the optimiser.
>
> Sure they can:
>
> $ cat ConstantFolding.hs
> {-# LANGUAGE TemplateHaskell #-}
> {-# OPTIONS_GHC -fplugin=Test.Inspection.Plugin #-}
> module ConstantFolding where
>
> import Test.Inspection
>
> ltInt :: Bool
> ltInt = (3::Int) > 2
>
> ltInteger :: Bool
> ltInteger = (3::Integer) > 2
>
> true :: Bool
> true = True
>
>
> inspect $ 'ltInt === 'true
> inspect $ 'ltInteger === 'true
>
> $ ghc -O ConstantFolding.hs
> [1 of 1] Compiling ConstantFolding ( ConstantFolding.hs,
> ConstantFolding.o )
> ConstantFolding.hs:17:1: ltInt === true passed.
> ConstantFolding.hs:18:1: ltInteger === true passed.
> inspection testing successful
> expected successes: 2
>
>
>
> As an alternative with a maybe simpler user interface (and probably
> less power), I wonder if we can create a magic function
>> compileTimeWHNF :: a -> a
> or
>> compileTimeNF :: a -> a
> and a GHC core plugin (or eventually built-in thing) that finds these
> magic functions and evaluates their arguments, using the simplifier.
>
>
> Cheers,
> Joachim
>
> --
> Joachim Breitner
> mail at joachim-breitner.de
> http://www.joachim-breitner.de/
More information about the ghc-devs
mailing list