summaryrefslogtreecommitdiff
path: root/dhall/src/expr.rs
blob: 5885359d2bf904daaf20ff52122c7a93733740e2 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use crate::imports::ImportRoot;
use dhall_core::*;
use std::marker::PhantomData;

macro_rules! derive_other_traits {
    ($ty:ident) => {
        impl<'a> std::cmp::PartialEq for $ty<'a> {
            fn eq(&self, other: &Self) -> bool {
                self.0 == other.0
            }
        }

        impl<'a> std::fmt::Display for $ty<'a> {
            fn fmt(
                &self,
                f: &mut std::fmt::Formatter,
            ) -> Result<(), std::fmt::Error> {
                self.0.fmt(f)
            }
        }
    };
}

#[derive(Debug, Clone, Eq)]
pub(crate) struct Parsed<'a>(
    pub(crate) SubExpr<Span<'a>, Import>,
    pub(crate) ImportRoot,
);
derive_other_traits!(Parsed);

#[derive(Debug, Clone, Eq)]
pub(crate) struct Resolved<'a>(
    pub(crate) SubExpr<Span<'a>, Normalized<'static>>,
);
derive_other_traits!(Resolved);

#[derive(Debug, Clone, Eq)]
pub(crate) struct Typed<'a>(
    pub(crate) SubExpr<X, Normalized<'static>>,
    pub(crate) Option<Type<'static>>,
    pub(crate) PhantomData<&'a ()>,
);
derive_other_traits!(Typed);

#[derive(Debug, Clone, Eq)]
pub(crate) struct Normalized<'a>(
    pub(crate) SubExpr<X, X>,
    pub(crate) Option<Type<'static>>,
    pub(crate) PhantomData<&'a ()>,
);
derive_other_traits!(Normalized);

/// A Dhall expression representing a simple type.
///
/// This captures what is usually simply called a "type", like
/// `Bool`, `{ x: Integer }` or `Natural -> Text`.
///
/// For a more general notion of "type", see [Type].
#[derive(Debug, Clone, Eq)]
pub struct SimpleType<'a>(
    pub(crate) SubExpr<X, X>,
    pub(crate) PhantomData<&'a ()>,
);
derive_other_traits!(SimpleType);

/// A Dhall expression representing a (possibly higher-kinded) type.
///
/// This includes [SimpleType]s but also higher-kinded expressions like
/// `Type`, `Kind` and `{ x: Type }`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Type<'a>(pub(crate) TypeInternal<'a>);

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum TypeInternal<'a> {
    Expr(Box<Normalized<'a>>),
    Const(dhall_core::Const),
    /// The type of `Sort`
    SuperType,
}

// Exposed for the macros
#[doc(hidden)]
impl<'a> From<SimpleType<'a>> for SubExpr<X, X> {
    fn from(x: SimpleType<'a>) -> SubExpr<X, X> {
        x.0
    }
}

// Exposed for the macros
#[doc(hidden)]
impl<'a> From<SubExpr<X, X>> for SimpleType<'a> {
    fn from(x: SubExpr<X, X>) -> SimpleType<'a> {
        SimpleType(x, PhantomData)
    }
}

// Exposed for the macros
#[doc(hidden)]
impl<'a> From<Normalized<'a>> for Typed<'a> {
    fn from(x: Normalized<'a>) -> Typed<'a> {
        Typed(x.0.embed_absurd(), x.1, x.2)
    }
}

#[doc(hidden)]
impl<'a> Typed<'a> {
    pub(crate) fn as_expr(&self) -> &SubExpr<X, Normalized<'a>> {
        &self.0
    }
    pub(crate) fn into_expr(self) -> SubExpr<X, Normalized<'a>> {
        self.0
    }
}

#[doc(hidden)]
impl<'a> Normalized<'a> {
    pub(crate) fn as_expr(&self) -> &SubExpr<X, X> {
        &self.0
    }
    pub(crate) fn into_expr(self) -> SubExpr<X, X> {
        self.0
    }
    pub(crate) fn unnote<'b>(self) -> Normalized<'b> {
        Normalized(self.0, self.1, PhantomData)
    }
    pub(crate) fn into_type(self) -> Type<'a> {
        Type(match self.0.as_ref() {
            ExprF::Const(c) => TypeInternal::Const(*c),
            _ => TypeInternal::Expr(Box::new(self)),
        })
    }
}

#[doc(hidden)]
impl<'a> Type<'a> {
    pub(crate) fn unnote<'b>(self) -> Type<'b> {
        use TypeInternal::*;
        Type(match self.0 {
            Expr(e) => Expr(Box::new(e.unnote())),
            Const(c) => Const(c),
            SuperType => SuperType,
        })
    }
}

impl<'a> SimpleType<'a> {
    pub(crate) fn into_type(self) -> Type<'a> {
        Normalized(self.0, Some(Type::const_type()), PhantomData).into_type()
    }
}