Hah! I had misread the signatures in the Core output. I'm getting exactly
the dictionary removal I wanted. Fantastic!

I'm attaching my sample source code and the Core it produces.

Sorry for the misdirection, and kudos for specialis/zation in GHC!

-- Conal

On Thu, Jan 28, 2016 at 4:54 AM, Simon Peyton Jones <[email protected]>
wrote:

> Aggressive inlining is one way, but specialisation ought to get a long
> way, and makes fewer copies of the specialised code.
>
>
>
> It’s hard to help without a concrete example
>
>
>
> Simon
>
>
>
> *From:* ghc-devs [mailto:[email protected]] *On Behalf Of *Conal
> Elliott
> *Sent:* 28 January 2016 00:05
> *To:* [email protected]
> *Subject:* More aggressive dictionary removal?
>
>
>
> I'm looking for pointers on getting GHC to eliminate more overloading &
> polymorphism. I think this sort of thing mainly happens in the Specialise
> module. The default GHC flag settings get me a couple levels of
> monomorphization and dictionary removal, but I want to go further. I've
> tried -fspecialise-aggressively, but it didn't seem to make a difference,
> and I haven't found this flag described in the GHC user's guide. Anyone
> have pointers to more information?
>
> Thanks, - Conal
>

Attachment: SpecT.dump-simpl
Description: Binary data

{-# LANGUAGE KindSignatures, GADTs, FlexibleInstances, FlexibleContexts, DeriveFunctor #-}
{-# OPTIONS_GHC -Wall #-}

{-# OPTIONS_GHC -O2 -ddump-simpl -ddump-to-file #-}

module Foo (foo) where

-- Type-level naturals, old-school
data Z
data S n

-- | Uniform pairs
data Pair a = a :# a deriving Functor

-- Top-down, depth-typed, perfect, binary, leaf trees
data Tree :: * -> * -> * where
  L :: a -> Tree Z a
  B :: Pair (Tree n a) -> Tree (S n) a

instance Functor (Tree Z) where
  fmap f (L a ) = L (f a)
  -- {-# INLINABLE fmap #-}

instance Functor (Tree n) => Functor (Tree (S n)) where
  fmap f (B ts) = B ((fmap.fmap) f ts)
  -- {-# INLINABLE fmap #-}

-- Specialization works (within this module at least) with or without INLINABLE.

type Unop a = a -> a

foo :: Unop (Tree (S (S (S (S (S (S (S (S (S (S (S Z))))))))))) Int)
foo = fmap (+1)

-- With this alternative instance, I get nowhere with specialization:

-- instance Functor (Tree n) where
--   fmap f (L a ) = L (f a)
--   fmap f (B ts) = B ((fmap.fmap) f ts)
--   {-# INLINABLE fmap #-}
_______________________________________________
ghc-devs mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs

Reply via email to