[Haskell-cafe] vector-simd: some code available, and some questions

Nicolas Trangez nicolas at incubaid.com
Sun Jul 8 17:09:30 CEST 2012


On Sun, 2012-07-08 at 10:27 +0200, Gábor Lehel wrote:
> On Sun, Jul 8, 2012 at 3:05 AM, Nicolas Trangez <nicolas at incubaid.com> wrote:
> > I implemented the inductive alignment calculation over One and Twice
> > (good idea, and easy to do), but I don't get the thing about
> > superclasses. I've been trying several approaches (including definitions
> > based on forall and other trickery I never used before), but didn't get
> > things to work, at least: the compiler always said I'd need
> > UndecidableInstances, and that sounds scary... Care to elaborate?
> 
> All I meant was
> 
> class (Alignment n, Alignment a) => AlignedToAtLeast n a
> 
> but I got a bit ahead of myself, because that rules out the instance
> on tuples.

Maybe that's one of the things I ran into, can't remember OTOH.

>  (I suppose you *could* write some kind of Alignment
> instance for them, taking their minimum or something, but that's
> getting a bit too subversive for me).

Heh, it already feels evil-but-in-a-good-and-powerful-way to me right
now ;-) Thanks for all your input!

>  The alternative, if you want
> both Alignment as a superclass and the ability to constrain multiple
> types at once, is to have the above, remove the instance on tuples,
> and instead something like:
> 
> class (AlignedToAtLeast n a, AlignedToAtLeast n b) => AlignedToAtLeast2 n a b
> instance (AlignedToAtLeast n a, AlignedToAtLeast n b) => AlignedToAtLeast2 n a b
> class (AlignedToAtLeast n a, AlignedToAtLeast n b, AlignedToAtLeast n
> c) => AlignedToAtLeast3 n a b c
> instance (AlignedToAtLeast n a, AlignedToAtLeast n b, AlignedToAtLeast
> n c) => AlignedToAtLeast3 n a b c
> (feel free to think of better names!)
> 
> unsafeXorSSE42 :: (Storable a, SV.AlignedToAtLeast3 SV.A16 o1 o2 o3)
> => SV.Vector o1 a -> SV.Vector o2 a -> SV.Vector o3 a

Implemented in [1]. Code says

unsafeXorSSE42 :: (Storable a,
    SV.AlignedToAtLeast3 SV.A16 o1 o2 o3) =>
    SV.Vector o1 a -> SV.Vector o2 a -> SV.Vector o3 a

ghci says

unsafeXorSSE42
  :: (AlignedToAtLeast (Data.Vector.SIMD.Mutable.Twice A8) o2,
      AlignedToAtLeast (Data.Vector.SIMD.Mutable.Twice A8) o1,
      AlignedToAtLeast (Data.Vector.SIMD.Mutable.Twice A8) o3,
      Foreign.Storable.Storable a) =>
     Vector o1 a -> Vector o2 a -> Vector o3 a

which is the same thing, so should do.

> That will require UndecidableInstances, but all that means is that GHC
> can't prove to itself that instance checking will terminate. So you
> could end up getting the compiler into an infinite loop (or in
> practice, to exceed its recursion limit). But it doesn't allow
> anything unsafe to happen at runtime, and there's plenty of perfectly
> good instances which terminate even if GHC can't prove it.

I see. I had some rather unsafe/undecidable-by-a-developer things in
mind, but I guess I should read up on some of the language extensions
GHC provides.

Thanks,

Nicolas

[1]
https://github.com/NicolasT/vector-simd/commit/8f934891c9630a96ce009fafa7f6ba70df306d4f







More information about the Haskell-Cafe mailing list