Is it possible to check list length at compile time?

Hello, I have a function that takes a list as its parameter. The list must always have the same length, for example 16. Instead of producing a "Nothing" or bottom out when an invalid list is supplied, are there ways to verify this at compile time? If not, are there alternative data types that has this kind of functionality? Thanks, Kelong

It is possible to go part of the way. See for example, the NonEmpty type
in the semigroups package. It is a list that always has at least one
element by restricting the operations you can do to the type without
turning it into a normal list. You could easily adapt that solution to a
sixteen element list, but it might not be a reasonable amount of trouble to
go through.
On Tue, Mar 17, 2015 at 1:26 PM, Kelong Cong
Hello,
I have a function that takes a list as its parameter. The list must always have the same length, for example 16. Instead of producing a "Nothing" or bottom out when an invalid list is supplied, are there ways to verify this at compile time? If not, are there alternative data types that has this kind of functionality?
Thanks, Kelong
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

On Tue, Mar 17, 2015 at 05:26:39PM +0000, Kelong Cong wrote:
I have a function that takes a list as its parameter. The list must always have the same length, for example 16. Instead of producing a "Nothing" or bottom out when an invalid list is supplied, are there ways to verify this at compile time? If not, are there alternative data types that has this kind of functionality?
Wouldn't a gigantic 16-sized tuple solve the matter (or any datatype with 16 accessors)? What functions are you using on the list?

Hi Kelong!
Check out
https://hackage.haskell.org/package/sized-vector-0.0.2.0/docs/Data-Vector-Si...
for a vector which has its size stored in type itself.
Cheers.
On Tue, Mar 17, 2015 at 7:26 PM, Kelong Cong
Hello,
I have a function that takes a list as its parameter. The list must always have the same length, for example 16. Instead of producing a "Nothing" or bottom out when an invalid list is supplied, are there ways to verify this at compile time? If not, are there alternative data types that has this kind of functionality?
Thanks, Kelong
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Thanks for the suggestion guys, I think the sized vector is just what I
needed!
I know about tuples but I can't use the usual list functions like map,
splitAt etc, also there is no need to have more than one data type.
On 17 March 2015 at 18:35, Konstantine Rybnikov
Hi Kelong!
Check out https://hackage.haskell.org/package/sized-vector-0.0.2.0/docs/Data-Vector-Si... for a vector which has its size stored in type itself.
Cheers.
On Tue, Mar 17, 2015 at 7:26 PM, Kelong Cong
wrote: Hello,
I have a function that takes a list as its parameter. The list must always have the same length, for example 16. Instead of producing a "Nothing" or bottom out when an invalid list is supplied, are there ways to verify this at compile time? If not, are there alternative data types that has this kind of functionality?
Thanks, Kelong
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

I think you're looking for dependend types or refinement types. It's sort of possible in Haskell, but look into Agda, Idris, F#, and this thread: http://www.reddit.com/r/haskell/comments/2z5l9y/question_type_system_and_ndi... Best regards, Marcin Mrotek
participants (5)
-
David McBride
-
Francesco Ariis
-
Kelong Cong
-
Konstantine Rybnikov
-
Marcin Mrotek