Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type-level list of a single type level-tuple in Haskell

Using DataKinds and TypeOperators, I can make type level-tuples of types, and type-level lists of types, but I cannot nest them:

> :k '['(Int, Int), '(Int, Int)]
error: parse error on input ‘'’`

I can make a list of multiple tuples:

> :k ['(Int,Int),'(Int,Int)]
['(Int,Int),'(Int,Int)] :: [(*, *)]

But this doesn't work with only one tuple gives:

:k ['(Int,Bool)]
<interactive>:1:2: error:
    • Expected a type, but ‘'(Int, Bool)’ has kind ‘(*, *)’

It can be done using KindSignatures, but it is very verbose:

> :k '[('(Int,Bool) :: (*,*))]
'[('(Int,Bool) :: (*,*))] :: [(*, *)]

Is there a less verbose way to do this, or is this the best way?

like image 543
Greg C Avatar asked Feb 18 '20 19:02

Greg C


People also ask

Can lists in Haskell have different types?

One is of type (String,Int) , whereas the other is (Int,String) . This has implications for building up lists of tuples. We could very well have lists like [("a",1),("b",9),("c",9)] , but Haskell cannot have a list like [("a",1),(2,"b"),(9,"c")] .

What is a tuple in Haskell?

A tuple is a fixed-length coupling of values, written in parentheses with the values separated by commas. One way to use this is to pass all parameters into a function as one value, rather than the curried functions we've seen so far.

How do I turn a list into a tuple Haskell?

In a general way, you can't. Each size of tuple is a distinct type, whereas lists of any length are a single type. Thus, there's no good way to write a function that takes a list and returns a tuple of the same length--it wouldn't have a well-defined return type.

How do you get the second element in a tuple Haskell?

2. snd. This tuple function is used to get the second element from the tuple values or group. We can use this function before the tuple and it will return us the second element as the result in Haskell.


1 Answers

You need to add a space:

> :k '['(Int, Int), '(Int, Int)]
<interactive>:1:1: error: parse error on input '
> :k '[ '(Int, Int), '(Int, Int)]
'[ '(Int, Int), '(Int, Int)] :: [(*, *)]

Essentially, the parser is confused by the char literal '[' which happens to be at the beginning.

like image 133
chi Avatar answered Sep 20 '22 13:09

chi