Dhall.Context

dhall-1.42.2: A configuration language guaranteed to terminate

Safe Haskell | Safe-Inferred
Language | Haskell2010

Dhall.Context

Contents

- Context

Description

This is a utility module that consolidates all `Context`-related operations

Synopsis

- data Context a
- empty :: Context a
- insert :: Text -> a -> Context a -> Context a
- match :: Context a -> Maybe (Text, a, Context a)
- lookup :: Text -> Int -> Context a -> Maybe a
- toList :: Context a -> [(Text, a)]

# Context

data Context a #

A `(Context a)` associates `Text` labels with values of type `a`. Each `Text` label can correspond to multiple values of type `a`

The `Context` is used for type-checking when `(a = Expr X)`

- You create a `Context` using `empty` and `insert`
- You transform a `Context` using `fmap`
- You consume a `Context` using `lookup` and `toList`

The difference between a `Context` and a `Map` is that a `Context` lets you have multiple ordered occurrences of the same key and you can query for the `n`th occurrence of a given key.

#### Instances

Instances details

Functor Context # |
Instance details Defined in Dhall.Context Methods fmap :: (a -> b) -> Context a -> Context b # (<$) :: a -> Context b -> Context a #

empty :: Context a #

An empty context with no key-value pairs

insert :: Text -> a -> Context a -> Context a #

Add a key-value pair to the `Context`

match :: Context a -> Maybe (Text, a, Context a) #

"Pattern match" on a `Context`

    match (insert k v ctx) = Just (k, v, ctx)
    match  empty           = Nothing

lookup :: Text -> Int -> Context a -> Maybe a #

Look up a key by name and index

    lookup _ _         empty  = Nothing
    lookup k 0 (insert k v c) = Just v
    lookup k n (insert k v c) = lookup k (n - 1) c
    lookup k n (insert j v c) = lookup k  n      c  -- k /= j

toList :: Context a -> [(Text, a)] #

Return all key-value associations as a list

    toList           empty  = []
    toList (insert k v ctx) = (k, v) : toList ctx