Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type Families extension does not work as described

On the Haskell wiki page for Type Families, there is the following list of examples:

type family F a :: *
type instance F [Int]              = Int         -- OK!
type instance F String             = Char        -- OK!
type instance F (F a)              = a           -- WRONG: type parameter mentions a type family
type instance F (forall a. (a, b)) = b           -- WRONG: a forall type appears in a type parameter
type instance F Float              = forall a.a  -- WRONG: right-hand side may not be a forall type
type instance where                              -- OK!
  F (Maybe Int)  = Int
  F (Maybe Bool) = Bool
  F (Maybe a)    = String
type instance where            -- WRONG: conflicts with earlier instances (see below)
  F Int = Float
  F a   = [a]

type family G a b :: * -> *
type instance G Int            = (,)     -- WRONG: must be two type parameters
type instance G Int Char Float = Double  -- WRONG: must be two type parameters

This demonstrates that type instance where is valid syntax under this extension. However the following code does not compile for me with GHC 7.4.2:

{-# LANGUAGE TypeFamilies #-}

type family F a :: *
type instance where
  F (Maybe Int)  = Int
  F (Maybe Bool) = Bool
  F (Maybe a)    = String

The error message is:

test.hs:4:15: parse error on input `where'

Since this is a parsing error, it looks like that syntax is not supported, so am I missing a requisite extension, or is something else amiss?

like image 944
Dylan Avatar asked May 02 '13 12:05

Dylan


People also ask

What is a closed type family?

A closed type family has all of its equations defined in one place and cannot be extended, whereas an open family can have instances spread across modules. The advantage of a closed family is that its equations are tried in order, similar to a term-level function definition.

What are type families in Haskell?

Indexed type families, or type families for short, are a Haskell extension supporting ad-hoc overloading of data types. Type families are parametric types that can be assigned specialized representations based on the type parameters they are instantiated with.

What is instance Haskell?

An instance of a class is an individual object which belongs to that class. In Haskell, the class system is (roughly speaking) a way to group similar types. (This is the reason we call them "type classes"). An instance of a class is an individual type which belongs to that class.


1 Answers

This appears to be a case of premature documentation. According to this blog post, this syntax is part of a feature recently added to GHC HEAD, but it's not yet valid in any released version of GHC.

like image 185
hammar Avatar answered Oct 11 '22 05:10

hammar