{-# LANGUAGE DeriveFunctor #-}
module Dhall.Context (
Context
, empty
, insert
, match
, lookup
, toList
) where
import Data.Text (Text)
import Prelude hiding (lookup)
newtype Context a = Context { Context a -> [(Text, a)]
getContext :: [(Text, a)] }
deriving (a -> Context b -> Context a
(a -> b) -> Context a -> Context b
(forall a b. (a -> b) -> Context a -> Context b)
-> (forall a b. a -> Context b -> Context a) -> Functor Context
forall a b. a -> Context b -> Context a
forall a b. (a -> b) -> Context a -> Context b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Context b -> Context a
$c<$ :: forall a b. a -> Context b -> Context a
fmap :: (a -> b) -> Context a -> Context b
$cfmap :: forall a b. (a -> b) -> Context a -> Context b
Functor)
empty :: Context a
empty :: Context a
empty = [(Text, a)] -> Context a
forall a. [(Text, a)] -> Context a
Context []
insert :: Text -> a -> Context a -> Context a
insert :: Text -> a -> Context a -> Context a
insert Text
k a
v (Context [(Text, a)]
kvs) = [(Text, a)] -> Context a
forall a. [(Text, a)] -> Context a
Context ((Text
k, a
v) (Text, a) -> [(Text, a)] -> [(Text, a)]
forall a. a -> [a] -> [a]
: [(Text, a)]
kvs)
{-# INLINABLE insert #-}
match :: Context a -> Maybe (Text, a, Context a)
match :: Context a -> Maybe (Text, a, Context a)
match (Context ((Text
k, a
v) : [(Text, a)]
kvs)) = (Text, a, Context a) -> Maybe (Text, a, Context a)
forall a. a -> Maybe a
Just (Text
k, a
v, [(Text, a)] -> Context a
forall a. [(Text, a)] -> Context a
Context [(Text, a)]
kvs)
match (Context [] ) = Maybe (Text, a, Context a)
forall a. Maybe a
Nothing
{-# INLINABLE match #-}
lookup :: Text -> Int -> Context a -> Maybe a
lookup :: Text -> Int -> Context a -> Maybe a
lookup Text
_ Int
_ (Context [] ) =
Maybe a
forall a. Maybe a
Nothing
lookup Text
x Int
n (Context ((Text
k, a
v):[(Text, a)]
kvs)) =
if Text
x Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
k
then if Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then a -> Maybe a
forall a. a -> Maybe a
Just a
v
else Text -> Int -> Context a -> Maybe a
forall a. Text -> Int -> Context a -> Maybe a
lookup Text
x (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ([(Text, a)] -> Context a
forall a. [(Text, a)] -> Context a
Context [(Text, a)]
kvs)
else Text -> Int -> Context a -> Maybe a
forall a. Text -> Int -> Context a -> Maybe a
lookup Text
x Int
n ([(Text, a)] -> Context a
forall a. [(Text, a)] -> Context a
Context [(Text, a)]
kvs)
{-# INLINABLE lookup #-}
toList :: Context a -> [(Text, a)]
toList :: Context a -> [(Text, a)]
toList = Context a -> [(Text, a)]
forall a. Context a -> [(Text, a)]
getContext
{-# INLINABLE toList #-}