mikeizbicki / subhask

Type safe interface for working in subcategories of Hask
BSD 3-Clause "New" or "Revised" License
418 stars 43 forks source link

[WIP] UVector algebra instances #26

Closed tonyday567 closed 8 years ago

tonyday567 commented 8 years ago

I had a go at adding some algebra instances for UVector. My motivation is to be able to say:

λ> let v1 = unsafeToModule [1..7] :: UVector "x" Double
λ> let sphere v = (v.+(-1)) <> (v.+(-1))
λ> sphere v1
91.0

The Action instance was easy but the <> proved to be tougher. Hilbert needed a TensorAlgebra instance of UVector (does it have to?) and this had a knock on effect so I ended up having to create some of the matrix heirarchy.

I saw along the way that the TensorAlgebra class needs to be replaced by the Tensor product in the Monoidal category Vect. It all looked like a big refactor well above my pay grade.

Once I created the Tensor_UVector family, I had to also create a ValidUVector type and then use it in a lot of places, also having to export it for use in Algebra. Is this usually how it goes when developing the algebra instances?

It looked like the ToFromVector constraint in unsafeMkSMatrix and the MatrixField instance of SVector was for convenience. I created a ToFromVector instance for UVector but left it undefined. I wasn't sure whether to go with a ToFromSVector, ToFromUVector, or to combine them and also guessed they would be moving elsewhere eventually.

I also just copied unsafeMkUMatrix from the SVector version and didnt think about an efficient implementation of an Unboxed Matrix (but this would be an interesting mini-project).

mikeizbicki commented 8 years ago

Thanks for this work!

As you've noticed, there's a lot of warts in the algebra hierarchy that need to be smoothed out still. The reason for these warts is the limitations in the current type system. With GHC 8.0, some of these limitations will be fixed, so I'm hoping for a much better hierarchy then.

I've only taken a cursory glance through your code, but it all looks good. There's lots of potential edge cases though when dealing with manual memory management, and I haven't tested these. One of the things I'm working on right now is improved support for the automated test cases that GHC 8.0 will allow.

Anyways, thanks again! I really appreciate your addition, and especially hearing your thought process about what you decided to add. That's great feedback to help me streamline things for the future.

tonyday567 commented 8 years ago

You're very welcome! The heirarchy is pretty good right now for getting stuff done, but certainly looking forward to 8.0 mods.