Skip to content

Repository files navigation

VectorProvider

An F# library that generates dimension-typed vectors and arithmetic operators for them. This library is an erased type provider - during compilation all Vector<_> types are unwrapped to array<int> types. This gives compile-time checking of vector dimensions with the same runtime performance of non-length checked arrays.

openVectorProvidertypeVec3= Vector<3u>letpt3d= Vec3 (1,2,3)letone= pt3d.Dimension1
pt3d.Dimension2 <-5letfour= pt3d.Dimension4 // Compiler error FS0039: The type 'Vector<3>' does not define the field, constructor or member 'Dimension4'
pt3d.Dimension4 <-7// Compiler error FS0039: The type 'Vector<3>' does not define the field, constructor or member 'Dimension4'typeVec4= Vector<4u>letpt4dError= Vec4 (1,2,3)// Compiler error FS0505: The member or object constructor 'Vector,dimension="4"' does not take 3 argumentsletpt4d= Vec4 (0,0,0,0)
pt4d.Dimension4 <-1letpt1= Vec3 (1,2,3)letpt2= Vec3 (4,5,6)letpt3= pt1 +. pt2 // = Vec3 (5, 7, 9)letpt4= pt2 -. pt1 // = Vec3 (3, 3, 3)letpt5=-.pt1 // = Vec3 (-1, -2, -3)letpt6= pt3d +. pt4d // Compiler error FS0001: This expression was expected to have type 'Vector<3>' but here has type 'Vector<4>'

To do

  • Add more vector arithmetic operators
  • Generalize to types other than int
    • This may require RFC FS-1023 to implement in a generalized way
  • Improve testing
    • FsCheck views a Vector<_> as the type after erasure (array<int>), so property-based testing via FsCheck passes in arrays that are not length-checked. For now FsCheck is used to generate int * int * int values, which can be piped to the Vector<3u> constructor to create test data
    • The current tests only involve Vector<3u> - is it possible to have a bug in Vector<n> but not Vector<m>?

About

An F# erased type provider generating dimension-typed vectors

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages