summaryrefslogtreecommitdiff
path: root/dhall_syntax/src/context.rs
blob: 55bfff577f96c64759746fb8f3bf198a382fabbf (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::cmp::Eq;
use std::collections::HashMap;
use std::hash::Hash;

/// A `(Context a)` associates `Text` labels with 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.
///
#[derive(Debug, Clone)]
pub struct Context<K: Eq + Hash, T>(HashMap<K, Vec<T>>);

impl<K: Hash + Eq + Clone, T> Context<K, T> {
    /// An empty context with no key-value pairs
    pub fn new() -> Self {
        Context(HashMap::new())
    }

    /// Look up a key by name and index
    ///
    /// ```c
    /// lookup _ _         empty  = Nothing
    /// lookup k 0 (insert k v c) = Just v
    /// lookup k n (insert k v c) = lookup k (n - 1) c  -- 1 <= n
    /// lookup k n (insert j v c) = lookup k  n      c  -- k /= j
    /// ```
    pub fn lookup<'a>(&'a self, k: &K, n: usize) -> Option<&'a T> {
        self.0.get(k).and_then(|v| {
            if n < v.len() {
                v.get(v.len() - 1 - n)
            } else {
                None
            }
        })
    }

    pub fn map<U, F: Fn(&K, &T) -> U>(&self, f: F) -> Context<K, U> {
        Context(
            self.0
                .iter()
                .map(|(k, vs)| {
                    ((*k).clone(), vs.iter().map(|v| f(k, v)).collect())
                })
                .collect(),
        )
    }

    pub fn lookup_all<'a>(&'a self, k: &K) -> impl Iterator<Item = &T> {
        self.0.get(k).into_iter().flat_map(|v| v.iter())
    }

    pub fn iter<'a>(&'a self) -> impl Iterator<Item = (&K, &T)> {
        self.0
            .iter()
            .flat_map(|(k, vs)| vs.iter().map(move |v| (k, v)))
    }

    pub fn iter_keys<'a>(&'a self) -> impl Iterator<Item = (&K, &Vec<T>)> {
        self.0.iter()
    }
}

impl<K: Hash + Eq + Clone, T: Clone> Context<K, T> {
    /// Add a key-value pair to the `Context`
    pub fn insert(&self, k: K, v: T) -> Self {
        let mut ctx = (*self).clone();
        {
            let m = ctx.0.entry(k).or_insert_with(Vec::new);
            m.push(v);
        }
        ctx
    }
}