{-# LANGUAGE GADTs           #-}
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_HADDOCK hide #-}
-- |
-- Module      : Data.Array.Accelerate.AST.Idx
-- Copyright   : [2008..2020] The Accelerate Team
-- License     : BSD3
--
-- Maintainer  : Trevor L. McDonell <trevor.mcdonell@gmail.com>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)
--
-- Typed de Bruijn indices
--

module Data.Array.Accelerate.AST.Idx
  where

import Language.Haskell.TH

-- De Bruijn variable index projecting a specific type from a type
-- environment.  Type environments are nested pairs (..((), t1), t2, ..., tn).
--
data Idx env t where
  ZeroIdx ::              Idx (env, t) t
  SuccIdx :: Idx env t -> Idx (env, s) t

data PairIdx p a where
  PairIdxLeft  :: PairIdx (a, b) a
  PairIdxRight :: PairIdx (a, b) b


idxToInt :: Idx env t -> Int
idxToInt :: Idx env t -> Int
idxToInt Idx env t
ZeroIdx       = Int
0
idxToInt (SuccIdx Idx env t
idx) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Idx env t -> Int
forall env t. Idx env t -> Int
idxToInt Idx env t
idx

rnfIdx :: Idx env t -> ()
rnfIdx :: Idx env t -> ()
rnfIdx Idx env t
ZeroIdx      = ()
rnfIdx (SuccIdx Idx env t
ix) = Idx env t -> ()
forall env t. Idx env t -> ()
rnfIdx Idx env t
ix

liftIdx :: Idx env t -> Q (TExp (Idx env t))
liftIdx :: Idx env t -> Q (TExp (Idx env t))
liftIdx Idx env t
ZeroIdx      = [|| ZeroIdx ||]
liftIdx (SuccIdx Idx env t
ix) = [|| SuccIdx $$(liftIdx ix) ||]