File: DataFamilies.hs

package info (click to toggle)
haskell-ghc-exactprint 1.7.1.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,044 kB
  • sloc: haskell: 32,076; makefile: 7
file content (50 lines) | stat: -rw-r--r-- 1,202 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE MultiParamTypeClasses #-}

-- Based on https://www.haskell.org/haskellwiki/GHC/Type_families#Detailed_definition_of_data_families

module DataFamilies
  (
    GMap
  , GMapKey(type GMapK)
  ) where

-- Type 1, Top level

data family GMap k :: * -> *

data family Array e

data family ArrayK :: * -> *


-- Type 2, associated types

class GMapKey k where
  data GMapK k :: * -> *

class C a b c where { data T1 c a :: * }  -- OK
-- class C a b c where { data T a a :: * }  -- Bad: repeated variable
-- class D a where { data T a x :: * }      -- Bad: x is not a class variable
class D a where { data T2 a :: * -> * }   -- OK

-- Instances

data instance GMap (Either a b) v = GMapEither (GMap a v) (GMap b v)

data family T3 a
data instance T3 Int  = A
data instance T3 Char = B
nonsense :: T3 a -> Int
-- nonsense A = 1             -- WRONG: These two equations together...
-- nonsense B = 2             -- ...will produce a type error.
nonsense = undefined


instance (GMapKey a, GMapKey b) => GMapKey (Either a b) where
  data GMapK (Either a b) v = GMapEitherK (GMap a v) (GMap b v)

-- data GMap () v = GMapUnit (Maybe v)
--               deriving Show