Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the star mean in this haskell code?

Tags:

haskell

I found this code in the unm-hip package. So Pixel is a function ?

class Imageable i where
  type Pixel i :: *
  rows :: i -> Int
  cols :: i -> Int
  ref  :: i -> Int -> Int -> (Pixel i)
  makeImage :: Int -> Int -> PixelOp (Pixel i) -> i
  pixelList :: i -> [Pixel i]
  pixelList i = [ ref i r c | r <- [0..(rows i - 1)], c <- [0..(cols i - 1)]]
like image 651
McBear Holden Avatar asked Apr 02 '14 09:04

McBear Holden


People also ask

What does <$> mean in Haskell?

It's merely an infix synonym for fmap , so you can write e.g. Prelude> (*2) <$> [1.. 3] [2,4,6] Prelude> show <$> Just 11 Just "11" Like most infix functions, it is not built-in syntax, just a function definition. But functors are such a fundamental tool that <$> is found pretty much everywhere.

What does the period mean in Haskell?

In general terms, where f and g are functions, (f . g) x means the same as f (g x). In other words, the period is used to take the result from the function on the right, feed it as a parameter to the function on the left, and return a new function that represents this computation."

What does () mean in Haskell?

() is very often used as the result of something that has no interesting result. For example, an IO action that is supposed to perform some I/O and terminate without producing a result will typically have type IO () .

What does in do in Haskell?

in goes along with let to name one or more local expressions in a pure function.


2 Answers

As an extension to standard Haskell, you can deal with "kinds". Kinds are sort of a very basic type system for types and type constructors. Kind * is a simple type, like Int. Kind * -> * is a type constructor that takes a type and yields a type, like Maybe: pass it a type like Int as an argument, and you get the type Maybe Int.

The other extension used in this code (which I didn't notice at first, since the indentation was lost) is associated types. A typeclass in standard Haskell can specify a number of functions that the type must support. With associated types, it can additionally specify types and type constructors that are associated with the type.

Here, this means that a type i that is an instance of Imageable (i.e. behaves like an image) must have an associated pixel type Pixel i, and this must be a simple type (kind *), not a type constructor.

like image 53
Sebastian Redl Avatar answered Sep 30 '22 14:09

Sebastian Redl


"So Pixel is a function?"

Pixel is a type-level function. It takes a single type (which must be an instance of Imageable) and returns a type of kind '*'. Based on it's usage in the example code, the input type must also be of kind '*'. So, Pixel is very much like Maybe, they are both "type constructors" of kind '* -> *', you provide them a "simple type" and they return a "simple type". They are also valid in the same locations. Just like you can't have a function of type 'Foo -> Maybe', you also won't be able to have a function of type 'Bar -> Pixel'.

like image 33
Boyd Stephen Smith Jr. Avatar answered Sep 30 '22 13:09

Boyd Stephen Smith Jr.