summaryrefslogtreecommitdiff
path: root/dhall/src/expr.rs
blob: e3a2fe5a1ffd808fe10731db989f5cc81be5fe39 (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
use crate::imports::ImportRoot;
use dhall_core::*;
use std::marker::PhantomData;

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

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

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<X, Import>,
    pub(crate) ImportRoot,
    pub(crate) PhantomData<&'a ()>,
);
derive_other_traits_!(Parsed);

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

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

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

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

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Type(pub(crate) TypeInternal);

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

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

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

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

impl Typed {
    pub(crate) fn as_expr(&self) -> &SubExpr<X, Normalized> {
        &self.0
    }
    pub(crate) fn into_expr(self) -> SubExpr<X, Normalized> {
        self.0
    }
}

impl Normalized {
    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 {
        crate::expr::Type(TypeInternal::Expr(Box::new(self)))
    }
}

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