diff --git a/src/Data/Syntax/Assignment/Table/Array.hs b/src/Data/Syntax/Assignment/Table/Array.hs index eeaf0433f..95b0d0b63 100644 --- a/src/Data/Syntax/Assignment/Table/Array.hs +++ b/src/Data/Syntax/Assignment/Table/Array.hs @@ -3,6 +3,7 @@ module Data.Syntax.Assignment.Table.Array , singleton , fromListWith , toList +, lookup ) where import Control.Arrow ((&&&)) @@ -11,6 +12,7 @@ import Data.Functor.Classes import Data.List.NonEmpty (NonEmpty(..)) import Data.Semigroup (Max(..), Min(..), sconcat) import GHC.Stack +import Prelude hiding (lookup) data Table i a = Table { tableAddresses :: [i], tableBranches :: Array i (Maybe a) } deriving (Foldable, Functor, Traversable) @@ -29,5 +31,11 @@ toList :: Ix i => Table i a -> [(i, a)] toList Table{..} = tableAddresses >>= \ addr -> maybe [] (pure . (,) addr) (tableBranches ! addr) +lookup :: Ix i => i -> Table i a -> Maybe a +lookup i Table{..} + | bounds tableBranches `inRange` i = tableBranches ! i + | otherwise = Nothing + + instance (Ix i, Show i) => Show1 (Table i) where liftShowsPrec spA slA d t = showsBinaryWith showsPrec (const (liftShowList spA slA)) "Table" d (tableAddresses t) (toList t)