summaryrefslogtreecommitdiff
path: root/dhall/src/lib.rs
blob: 7300d885f13e17f5f90bbd4d837490fc266a5cdd (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#![doc(html_root_url = "https://docs.rs/dhall/0.4.0")]
#![allow(
    clippy::module_inception,
    clippy::needless_lifetimes,
    clippy::useless_format
)]

mod tests;

pub mod error;
pub mod semantics;
pub mod simple;
pub mod syntax;

use std::fmt::Display;
use std::path::Path;
use url::Url;

use crate::error::{EncodeError, Error, TypeError};
use crate::semantics::parse;
use crate::semantics::resolve;
use crate::semantics::resolve::ImportLocation;
use crate::semantics::{
    typecheck, typecheck_with, Hir, Nir, NirKind, Tir, Type,
};
use crate::syntax::binary;
use crate::syntax::{Builtin, Expr};

pub(crate) type ParsedExpr = Expr;
pub(crate) type DecodedExpr = Expr;
pub(crate) type ResolvedExpr = Expr;
pub(crate) type NormalizedExpr = Expr;
pub use crate::simple::{STyKind, SValKind, SimpleType, SimpleValue};

#[derive(Debug, Clone)]
pub(crate) struct Parsed(ParsedExpr, ImportLocation);

/// An expression where all imports have been resolved
///
/// Invariant: there must be no `Import` nodes or `ImportAlt` operations left.
#[derive(Debug, Clone)]
pub(crate) struct Resolved(Hir);

/// A typed expression
#[derive(Debug, Clone)]
pub(crate) struct Typed {
    hir: Hir,
    ty: Type,
}

/// A normalized expression.
#[derive(Debug, Clone)]
pub(crate) struct Normalized(Nir);

/// A Dhall value.
#[derive(Debug, Clone)]
pub struct Value {
    /// Invariant: in normal form
    pub(crate) hir: Hir,
    simple_val: Option<SimpleValue>,
    simple_ty: Option<SimpleType>,
}

/// Controls conversion from `Nir` to `Expr`
#[derive(Copy, Clone, Default)]
pub(crate) struct ToExprOptions {
    /// Whether to convert all variables to `_`
    pub(crate) alpha: bool,
}

impl Parsed {
    pub fn parse_file(f: &Path) -> Result<Parsed, Error> {
        parse::parse_file(f)
    }
    pub fn parse_remote(url: Url) -> Result<Parsed, Error> {
        parse::parse_remote(url)
    }
    pub fn parse_str(s: &str) -> Result<Parsed, Error> {
        parse::parse_str(s)
    }
    pub fn parse_binary_file(f: &Path) -> Result<Parsed, Error> {
        parse::parse_binary_file(f)
    }
    #[allow(dead_code)]
    pub fn parse_binary(data: &[u8]) -> Result<Parsed, Error> {
        parse::parse_binary(data)
    }

    pub fn resolve(self) -> Result<Resolved, Error> {
        resolve::resolve(self)
    }

    /// Converts a value back to the corresponding AST expression.
    pub fn to_expr(&self) -> ParsedExpr {
        self.0.clone()
    }
}

impl Resolved {
    pub fn typecheck(&self) -> Result<Typed, TypeError> {
        Ok(Typed::from_tir(typecheck(&self.0)?))
    }
    pub(crate) fn typecheck_with(self, ty: &Hir) -> Result<Typed, TypeError> {
        Ok(Typed::from_tir(typecheck_with(&self.0, ty)?))
    }
    /// Converts a value back to the corresponding AST expression.
    pub fn to_expr(&self) -> ResolvedExpr {
        self.0.to_expr_noopts()
    }
}

impl Typed {
    fn from_tir(tir: Tir<'_>) -> Self {
        Typed {
            hir: tir.as_hir().clone(),
            ty: tir.ty().clone(),
        }
    }
    /// Reduce an expression to its normal form, performing beta reduction
    pub fn normalize(&self) -> Normalized {
        Normalized(self.hir.rec_eval_closed_expr())
    }

    /// Converts a value back to the corresponding AST expression.
    fn to_expr(&self) -> ResolvedExpr {
        self.hir.to_expr(ToExprOptions { alpha: false })
    }

    pub(crate) fn ty(&self) -> &Type {
        &self.ty
    }
    pub(crate) fn get_type(&self) -> Result<Normalized, TypeError> {
        Ok(Normalized(self.ty.clone().into_nir()))
    }
}

impl Normalized {
    pub fn encode(&self) -> Result<Vec<u8>, EncodeError> {
        binary::encode(&self.to_expr())
    }
    pub fn to_value(&self) -> Value {
        Value {
            hir: self.to_hir(),
            simple_val: self.0.to_simple_value(),
            simple_ty: self.0.to_simple_type(),
        }
    }

    /// Converts a value back to the corresponding AST expression.
    pub fn to_expr(&self) -> NormalizedExpr {
        self.0.to_expr(ToExprOptions::default())
    }
    /// Converts a value back to the corresponding Hir expression.
    pub(crate) fn to_hir(&self) -> Hir {
        self.0.to_hir_noenv()
    }
    /// Converts a value back to the corresponding AST expression, alpha-normalizing in the process.
    pub(crate) fn to_expr_alpha(&self) -> NormalizedExpr {
        self.0.to_expr(ToExprOptions { alpha: true })
    }
    pub(crate) fn to_nir(&self) -> Nir {
        self.0.clone()
    }
    pub(crate) fn into_nir(self) -> Nir {
        self.0
    }

    pub(crate) fn from_kind(v: NirKind) -> Self {
        Normalized(Nir::from_kind(v))
    }
    pub(crate) fn from_nir(th: Nir) -> Self {
        Normalized(th)
    }

    pub(crate) fn make_builtin_type(b: Builtin) -> Self {
        Normalized::from_nir(Nir::from_builtin(b))
    }
    pub(crate) fn make_optional_type(t: Normalized) -> Self {
        Normalized::from_nir(
            Nir::from_builtin(Builtin::Optional).app(t.to_nir()),
        )
    }
    pub(crate) fn make_list_type(t: Normalized) -> Self {
        Normalized::from_nir(Nir::from_builtin(Builtin::List).app(t.to_nir()))
    }
    pub(crate) fn make_record_type(
        kts: impl Iterator<Item = (String, Normalized)>,
    ) -> Self {
        Normalized::from_kind(NirKind::RecordType(
            kts.map(|(k, t)| (k.into(), t.into_nir())).collect(),
        ))
    }
    pub(crate) fn make_union_type(
        kts: impl Iterator<Item = (String, Option<Normalized>)>,
    ) -> Self {
        Normalized::from_kind(NirKind::UnionType(
            kts.map(|(k, t)| (k.into(), t.map(|t| t.into_nir())))
                .collect(),
        ))
    }
}

impl Value {
    /// Parse a string into a Value, and optionally ensure that the value matches the provided type
    /// annotation.
    pub fn from_str_with_annot(
        s: &str,
        ty: Option<&Self>,
    ) -> Result<Self, Error> {
        let resolved = Parsed::parse_str(s)?.resolve()?;
        let typed = match ty {
            None => resolved.typecheck()?,
            Some(ty) => resolved.typecheck_with(&ty.hir)?,
        };
        Ok(typed.normalize().to_value())
    }

    /// Converts a Value into a SimpleValue.
    pub fn to_simple_value(&self) -> Option<SimpleValue> {
        self.simple_val.clone()
    }
    /// Converts a Value into a SimpleType.
    pub fn to_simple_type(&self) -> Option<SimpleType> {
        self.simple_ty.clone()
    }

    /// Converts a value back to the corresponding AST expression.
    pub(crate) fn to_expr(&self) -> NormalizedExpr {
        self.hir.to_expr(ToExprOptions::default())
    }
}

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

        impl std::cmp::Eq for $ty {}

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

derive_traits_for_wrapper_struct!(Parsed);

impl std::hash::Hash for Normalized {
    fn hash<H>(&self, state: &mut H)
    where
        H: std::hash::Hasher,
    {
        if let Ok(vec) = self.encode() {
            vec.hash(state)
        }
    }
}

impl From<Parsed> for NormalizedExpr {
    fn from(other: Parsed) -> Self {
        other.to_expr()
    }
}
impl From<Normalized> for NormalizedExpr {
    fn from(other: Normalized) -> Self {
        other.to_expr()
    }
}

impl Display for Resolved {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        self.to_expr().fmt(f)
    }
}

impl Eq for Typed {}
impl PartialEq for Typed {
    fn eq(&self, other: &Self) -> bool {
        self.normalize() == other.normalize()
    }
}
impl Display for Typed {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        self.to_expr().fmt(f)
    }
}

impl Eq for Normalized {}
impl PartialEq for Normalized {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}
impl Display for Normalized {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        self.to_expr().fmt(f)
    }
}

impl Eq for Value {}
impl PartialEq for Value {
    fn eq(&self, other: &Self) -> bool {
        self.hir == other.hir
    }
}
impl Display for Value {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        self.to_expr().fmt(f)
    }
}