summaryrefslogtreecommitdiff
path: root/dhall/src/expr.rs
blob: ab59ce05b2a338699f91588c2186b4c196c647fc (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
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 struct Parsed<'a>(
    pub(crate) SubExpr<Span<'a>, Import>,
    pub(crate) ImportRoot,
);
derive_other_traits!(Parsed);

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

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

/// An expression of type `Type` (like `Bool` or `Natural -> Text`, but not `Type`)
#[derive(Debug, Clone, Eq)]
pub struct SimpleType<'a>(
    pub(crate) SubExpr<X, X>,
    pub(crate) PhantomData<&'a ()>,
);
derive_other_traits!(SimpleType);

#[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>>),
    // 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.absurd(), x.1, x.2)
    }
}

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
    }
}

impl<'a> Normalized<'a> {
    pub(crate) fn as_expr(&self) -> &SubExpr<X, X> {
        &self.0
    }
    pub(crate) fn into_expr<T>(self) -> SubExpr<X, T> {
        self.0.absurd()
    }
    pub(crate) fn into_type(self) -> Type<'a> {
        crate::expr::Type(TypeInternal::Expr(Box::new(self)))
    }
}

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