Vectors, vector spaces and type-level Haskell

Greetings Haskellers, I'd like to model in Haskell two-dimensional vectors that "belong to" or "have an ambient space of" a two dimensional vector space. I'm also ignoring for now the issue of which field the vector space is over. I realize this is not strictly necessary to just start using 2D vectors, but I would like to bring the vector space in as a first class concept. Looking at for example this approach: https://hackage.haskell.org/package/linear-1.21.7/docs/Linear-V2.html, I get the impression this is likely to need some kind of type-level construct? e.g. type families or type-level literals? I haven't started learning yet about Haskell type-level programming so I thought I'd ask for advice first to see how this could be done idiomatically in Haskell either with or without type-level concepts. TIA, Stu

2d vector space is generated by two base vectors, if, which are orthogonal, that is normalised. They generate any other vector in that space. Greetings, Branimir.
On 04.10.2021., at 05:35, Stuart Hungerford
wrote: Greetings Haskellers,
I'd like to model in Haskell two-dimensional vectors that "belong to" or "have an ambient space of" a two dimensional vector space. I'm also ignoring for now the issue of which field the vector space is over. I realize this is not strictly necessary to just start using 2D vectors, but I would like to bring the vector space in as a first class concept.
Looking at for example this approach: https://hackage.haskell.org/package/linear-1.21.7/docs/Linear-V2.html, I get the impression this is likely to need some kind of type-level construct? e.g. type families or type-level literals?
I haven't started learning yet about Haskell type-level programming so I thought I'd ask for advice first to see how this could be done idiomatically in Haskell either with or without type-level concepts.
TIA,
Stu _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

On Mon, 4 Oct 2021 at 4:25 pm, Branimir Maksimovic < branimir.maksimovic@gmail.com> wrote:
2d vector space is generated by two base vectors, if, which are orthogonal, that is normalised. They generate any other vector in that space.
Yes, so I would be looking to somehow tie the 2 basis vectors back to a 2D vector space. Or indeed n basis vectors to an n-vector space. Thanks, Stu

On 04.10.2021., at 09:38, Stuart Hungerford
wrote: On Mon, 4 Oct 2021 at 4:25 pm, Branimir Maksimovic
mailto:branimir.maksimovic@gmail.com> wrote: 2d vector space is generated by two base vectors, if, which are orthogonal, that is normalised. They generate any other vector in that space. Yes, so I would be looking to somehow tie the 2 basis vectors back to a 2D vector space. Or indeed n basis vectors to an n-vector space.
Space is generated by base vectors, think in that way… With vector addition and scalar multiplication you get third vector. so dimension is number of different directions of base vectors. So, easy...
Thanks,
Stu

On Mon, Oct 4, 2021 at 6:45 PM Branimir Maksimovic
On 04.10.2021., at 09:38, Stuart Hungerford
wrote: On Mon, 4 Oct 2021 at 4:25 pm, Branimir Maksimovic
wrote: 2d vector space is generated by two base vectors, if, which are orthogonal, that is normalised. They generate any other vector in that space.
Yes, so I would be looking to somehow tie the 2 basis vectors back to a 2D vector space. Or indeed n basis vectors to an n-vector space.
Space is generated by base vectors, think in that way… With vector addition and scalar multiplication you get third vector. so dimension is number of different directions of base vectors. So, easy...
Thanks Branimir I appreciate you taking the time to reply to what could be a silly question. Perhaps I haven't explained what I'm looking for very well. I know about the vector and scalar operations the vector space inherits from the underlying abelian group and field of scalars. Including the basis of linearly independent vectors that generates all vectors in the space. What I'd like to do is use the Haskell type system to encode those operations so I can't--for example--use a two dimensional and three dimensional vector in the same operation, or "ask" a vector what its "ambient" vector space is and have those operations checked at compile time. I've avoided learning about type-level programming in Haskell so far, but it may be time to delve deeper... Thanks again, Stu

On 04.10.2021., at 09:55, Stuart Hungerford
wrote: On Mon, Oct 4, 2021 at 6:45 PM Branimir Maksimovic
mailto:branimir.maksimovic@gmail.com> wrote: On 04.10.2021., at 09:38, Stuart Hungerford
wrote: On Mon, 4 Oct 2021 at 4:25 pm, Branimir Maksimovic
wrote: 2d vector space is generated by two base vectors, if, which are orthogonal, that is normalised. They generate any other vector in that space.
Yes, so I would be looking to somehow tie the 2 basis vectors back to a 2D vector space. Or indeed n basis vectors to an n-vector space.
Space is generated by base vectors, think in that way… With vector addition and scalar multiplication you get third vector. so dimension is number of different directions of base vectors. So, easy...
Thanks Branimir I appreciate you taking the time to reply to what could be a silly question. Perhaps I haven't explained what I'm looking for very well.
I know about the vector and scalar operations the vector space inherits from the underlying abelian group and field of scalars. Including the basis of linearly independent vectors that generates all vectors in the space.
What I'd like to do is use the Haskell type system to encode those operations so I can't--for example--use a two dimensional and three dimensional vector in the same operation, or "ask" a vector what its "ambient" vector space is and have those operations checked at compile time.
I've avoided learning about type-level programming in Haskell so far, but it may be time to delve deeper...
Thanks again,
Stu Well vector is tuple 3d space 3 tuples of 3 elements each representing 3 directions in 3d space eg. Types can be anything, but same, so you could represent with a 3 lists also. i dunno what you mean by type checking? You have type checking already.
Greets, Branimir (and thanks)

Dear Stuart, The V2 and V3 etc. types provided by the linear package (which you already found) model vectors in 2- and 3-dimensional real vector spaces, over the field given by their 'a' type parameter. Wanting to add a vector from a 2-dimensional vector space over 'a' and a vector from a 3-dimensional vector space over 'b' entails adding a 'V2 a' and 'V3 b'; those are distinct types. The vector space of 'V2 a' is morally 'a^2'. It seems this satisfies what you want. Are there other guarantees you want enforced at compile time that this cannot give you? - Tom On 04/10/2021 09:55, Stuart Hungerford wrote:
On Mon, Oct 4, 2021 at 6:45 PM Branimir Maksimovic
wrote: On 04.10.2021., at 09:38, Stuart Hungerford
wrote: On Mon, 4 Oct 2021 at 4:25 pm, Branimir Maksimovic
wrote: 2d vector space is generated by two base vectors, if, which are orthogonal, that is normalised. They generate any other vector in that space.
Yes, so I would be looking to somehow tie the 2 basis vectors back to a 2D vector space. Or indeed n basis vectors to an n-vector space.
Space is generated by base vectors, think in that way… With vector addition and scalar multiplication you get third vector. so dimension is number of different directions of base vectors. So, easy...
Thanks Branimir I appreciate you taking the time to reply to what could be a silly question. Perhaps I haven't explained what I'm looking for very well.
I know about the vector and scalar operations the vector space inherits from the underlying abelian group and field of scalars. Including the basis of linearly independent vectors that generates all vectors in the space.
What I'd like to do is use the Haskell type system to encode those operations so I can't--for example--use a two dimensional and three dimensional vector in the same operation, or "ask" a vector what its "ambient" vector space is and have those operations checked at compile time.
I've avoided learning about type-level programming in Haskell so far, but it may be time to delve deeper...
Thanks again,
Stu _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

On Tue, Oct 5, 2021 at 3:05 AM Henning Thielemann
I'd like to model in Haskell two-dimensional vectors that "belong to" or "have an ambient space of" a two dimensional vector space.
If you only need two dimensional vectors, you might be happy with Data.Complex.
I hadn't thought of that -- thanks. Stu

On Mon, Oct 4, 2021 at 2:35 PM Stuart Hungerford
I'd like to model in Haskell two-dimensional vectors that "belong to" or "have an ambient space of" a two dimensional vector space. I'm also ignoring for now the issue of which field the vector space is over. I realize this is not strictly necessary to just start using 2D vectors, but I would like to bring the vector space in as a first class concept.
After much googling and searching Hackage, I found these references useful (note that "vectors" in these references sometimes refer to arrays of fixed length and sometimes to elements of a vector space): https://mmhaskell.com/machine-learning/dependent-types https://serokell.io/blog/dimensions-and-haskell-introduction https://hackage.haskell.org/package/linear-1.20.4/docs/Linear-V2.html http://mstksg.github.io/hmatrix/Numeric-LinearAlgebra-Static.html https://hackage.haskell.org/package/vector-space-0.16/docs/Data-VectorSpace.... https://hackage.haskell.org/package/fixed-vector-1.2.0.0/docs/Data-Vector-Fi... https://diagrams.github.io/doc/vector.html Stu

On Tue, 5 Oct 2021, Stuart Hungerford wrote:
On Mon, Oct 4, 2021 at 2:35 PM Stuart Hungerford
wrote: I'd like to model in Haskell two-dimensional vectors that "belong to" or "have an ambient space of" a two dimensional vector space. I'm also ignoring for now the issue of which field the vector space is over. I realize this is not strictly necessary to just start using 2D vectors, but I would like to bring the vector space in as a first class concept.
After much googling and searching Hackage, I found these references useful (note that "vectors" in these references sometimes refer to arrays of fixed length and sometimes to elements of a vector space):
https://mmhaskell.com/machine-learning/dependent-types
https://serokell.io/blog/dimensions-and-haskell-introduction
https://hackage.haskell.org/package/linear-1.20.4/docs/Linear-V2.html
http://mstksg.github.io/hmatrix/Numeric-LinearAlgebra-Static.html
You may also use https://hackage.haskell.org/package/comfort-array with https://hackage.haskell.org/package/comfort-array-0.5.1/docs/Data-Array-Comf... or https://hackage.haskell.org/package/comfort-array-shape-0.0/docs/Data-Array-... and https://hackage.haskell.org/package/lapack But it will be overkill for two-dimensional vectors.

On Tue, 5 Oct 2021 at 9:14 am, Henning Thielemann < lemming@henning-thielemann.de> wrote:
[…] You may also use https://hackage.haskell.org/package/comfort-array
with
https://hackage.haskell.org/package/comfort-array-0.5.1/docs/Data-Array-Comf... or
https://hackage.haskell.org/package/comfort-array-shape-0.0/docs/Data-Array-...
and https://hackage.haskell.org/package/lapack
But it will be overkill for two-dimensional vectors.
Thanks—will check those out. Stu
participants (4)
-
Branimir Maksimovic
-
Henning Thielemann
-
Stuart Hungerford
-
Tom Smeding