summaryrefslogtreecommitdiff
path: root/src/Interpreter/Rep.hs
blob: 7add442eb7b611a0209fb790c05a5a0464795661 (plain)
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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
module Interpreter.Rep where

import Data.IORef
import qualified Data.Vector.Mutable as MV
import GHC.TypeError

import Array
import AST


type family Rep t where
  Rep TNil = ()
  Rep (TPair a b) = (Rep a, Rep b)
  Rep (TEither a b) = Either (Rep a) (Rep b)
  Rep (TMaybe t) = Maybe (Rep t)
  Rep (TArr n t) = Array n (Rep t)
  Rep (TScal sty) = ScalRep sty
  Rep (TAccum t) = IORef (RepAc t)

type family RepAc t where
  RepAc TNil = ()
  RepAc (TPair a b) = (RepAc a, RepAc b)
  -- This is annoying when working with values of type 'RepAc t', because
  -- failing a pattern match does not generate negative type information.
  -- However, it works, saves us from having to defining a LEither type
  -- first-class in the type system with
  --   Rep (LEither a b) = Maybe (Either a b)
  -- and it's not even incorrect, in a way.
  RepAc (TMaybe (TEither a b)) = IORef (Maybe (Either (RepAc a) (RepAc b)))
  RepAc (TMaybe t) = IORef (Maybe (RepAc t))
  RepAc (TArr n t) = (Shape n, MV.IOVector (RepAc t))
  RepAc (TScal sty) = IORef (ScalRep sty)
  RepAc (TAccum t) = TypeError (Text "Nested accumulators")