Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is the type of [ ( True , [ ] ) , ( False , [ ['a'] ] ) ] , "[ (Bool , [ [char] ] )]"?

When i type :type [(True,[]),(False, [['a']])] , "[(Bool,[[char]])]" in ghci it outputs [ (Bool , [ [char] ] )]. how is it possible that Haskell's type inference is treating an empty list and [[char]] as the same.

like image 218
Rahat Avatar asked Oct 28 '25 16:10

Rahat


1 Answers

Haskell knows that True and False are Bools, and furthermore we know that all the elements of a list have the same type.

We are thus looking for a type [(True,[]),(False, [['a']])]. We see with the element that it is a 2-tuple with as first element a Bool, and as second item there is an empty list, so that has type [] :: [a] with a, at that time, unknown.

Now if we look at the second item, and see (False, [['a']]). This is thus a 2-tuple with False as first item, and [['a']] as second item. The type of 'a' is Char, hence ['a'] has type [Char], and [['a']] has type [[Char]]. Since the elements of a list should have the same type, we thus derive that the a type parameter of the first item, is a ~ [Char], and thus the outer list has type [(True,[]),(False, [['a']])] :: [(Bool, [[Char]])].

like image 60
Willem Van Onsem Avatar answered Oct 30 '25 11:10

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!