From f5d2151d35942b957230c3081a928af3619d9400 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 31 Mar 2019 18:47:34 +0200 Subject: Make SubExpr a newtype --- dhall/src/binary.rs | 3 +- dhall/src/main.rs | 3 +- dhall/src/normalize.rs | 39 ++++++++++--------- dhall/src/typecheck.rs | 103 +++++++++++++++++++++++-------------------------- 4 files changed, 70 insertions(+), 78 deletions(-) (limited to 'dhall/src') diff --git a/dhall/src/binary.rs b/dhall/src/binary.rs index 235a55b..988891b 100644 --- a/dhall/src/binary.rs +++ b/dhall/src/binary.rs @@ -1,9 +1,8 @@ use dhall_core::*; use itertools::*; use serde_cbor::value::value as cbor; -use std::rc::Rc; -type ParsedExpr = Rc>; +type ParsedExpr = SubExpr; #[derive(Debug)] pub enum DecodeError { diff --git a/dhall/src/main.rs b/dhall/src/main.rs index 1a2e309..eb2206b 100644 --- a/dhall/src/main.rs +++ b/dhall/src/main.rs @@ -1,6 +1,5 @@ use std::error::Error; use std::io::{self, Read}; -use std::rc::Rc; use term_painter::ToStyle; use dhall::*; @@ -66,7 +65,7 @@ fn main() { } }; - let expr: Rc> = rc(imports::panic_imports(&expr)); + let expr: SubExpr<_, _> = rc(imports::panic_imports(&expr)); let type_expr = match typecheck::type_of(expr.clone()) { Err(e) => { diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index a3a2318..5405e88 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -2,7 +2,6 @@ use dhall_core::*; use dhall_generator::dhall_expr; use std::fmt; -use std::rc::Rc; fn apply_builtin( b: Builtin, @@ -44,13 +43,13 @@ where } // Normalize the important argument if let Some(i) = arg_to_eval { - args[i] = Rc::clone(&normalize_whnf(&args[i])); + args[i] = SubExpr::clone(&normalize_whnf(&args[i])); } let evaled_arg = arg_to_eval.map(|i| args[i].as_ref()); let ret = match (b, evaled_arg, args.as_slice()) { - (OptionalSome, _, [x, ..]) => rc(NEOptionalLit(Rc::clone(x))), - (OptionalNone, _, [t, ..]) => rc(EmptyOptionalLit(Rc::clone(t))), + (OptionalSome, _, [x, ..]) => rc(NEOptionalLit(SubExpr::clone(x))), + (OptionalNone, _, [t, ..]) => rc(EmptyOptionalLit(SubExpr::clone(t))), (NaturalIsZero, Some(NaturalLit(n)), _) => rc(BoolLit(*n == 0)), (NaturalEven, Some(NaturalLit(n)), _) => rc(BoolLit(*n % 2 == 0)), (NaturalOdd, Some(NaturalLit(n)), _) => rc(BoolLit(*n % 2 != 0)), @@ -76,7 +75,7 @@ where rc(NEListLit(ys)) } (ListIndexed, Some(EmptyListLit(t)), _) => { - let t = Rc::clone(t); + let t = SubExpr::clone(t); dhall_expr!([] : List ({ index : Natural, value : t })) } (ListIndexed, Some(NEListLit(xs)), _) => { @@ -101,7 +100,7 @@ where break rc(App(x.clone(), rest.to_vec())); } }; - let a0 = Rc::clone(a0); + let a0 = SubExpr::clone(a0); let a1 = shift(1, &V("a".into(), 0), &a0); // TODO: use Embed to avoid reevaluating g break dhall_expr!( @@ -122,7 +121,7 @@ where break rc(App(x.clone(), rest.to_vec())); } }; - let a0 = Rc::clone(a0); + let a0 = SubExpr::clone(a0); // TODO: use Embed to avoid reevaluating g break dhall_expr!( g @@ -133,13 +132,13 @@ where } } (ListFold, Some(EmptyListLit(_)), [_, _, _, _, nil, ..]) => { - Rc::clone(nil) + SubExpr::clone(nil) } (ListFold, Some(NEListLit(xs)), [_, _, _, cons, nil, ..]) => { - xs.iter().rev().fold(Rc::clone(nil), |acc, x| { + xs.iter().rev().fold(SubExpr::clone(nil), |acc, x| { let x = x.clone(); let acc = acc.clone(); - let cons = Rc::clone(cons); + let cons = SubExpr::clone(cons); dhall_expr!(cons x acc) }) } @@ -149,14 +148,14 @@ where // } (OptionalFold, Some(NEOptionalLit(x)), [_, _, _, just, _, ..]) => { let x = x.clone(); - let just = Rc::clone(just); + let just = SubExpr::clone(just); dhall_expr!(just x) } ( OptionalFold, Some(EmptyOptionalLit(_)), [_, _, _, _, nothing, ..], - ) => Rc::clone(nothing), + ) => SubExpr::clone(nothing), // // fold/build fusion // (OptionalFold, [_, App(box Builtin(OptionalBuild), [_, x, rest..]), rest..]) => { // normalize_whnf(&App(bx(x.clone()), rest.to_vec())) @@ -175,13 +174,15 @@ where break dhall_expr!(g Natural (λ(x : Natural) -> x + 1) 0); } } - (NaturalFold, Some(NaturalLit(0)), [_, _, _, zero]) => Rc::clone(zero), + (NaturalFold, Some(NaturalLit(0)), [_, _, _, zero]) => { + SubExpr::clone(zero) + } (NaturalFold, Some(NaturalLit(n)), [_, t, succ, zero]) => { let fold = rc(Builtin(NaturalFold)); let n = rc(NaturalLit(n - 1)); - let t = Rc::clone(t); - let succ = Rc::clone(succ); - let zero = Rc::clone(zero); + let t = SubExpr::clone(t); + let succ = SubExpr::clone(succ); + let zero = SubExpr::clone(zero); dhall_expr!(succ (fold n t succ zero)) } // (NaturalFold, Some(App(f2, args2)), _) => { @@ -273,7 +274,7 @@ where } (TextAppend, TextLit(x), TextLit(y)) => TextLit(x + y), (ListAppend, EmptyListLit(t), EmptyListLit(_)) => { - EmptyListLit(Rc::clone(t)) + EmptyListLit(SubExpr::clone(t)) } (ListAppend, EmptyListLit(_), _) => return y, (ListAppend, _, EmptyListLit(_)) => return x, @@ -326,7 +327,7 @@ where _ => rc(Projection(e, ls.clone())), } } - _ => Rc::clone(e), + _ => SubExpr::clone(e), } } @@ -344,5 +345,5 @@ where S: fmt::Debug, A: fmt::Debug, { - map_subexpr_rc(&normalize_whnf(&e), |x| normalize(Rc::clone(x))) + map_subexpr_rc(&normalize_whnf(&e), |x| normalize(SubExpr::clone(x))) } diff --git a/dhall/src/typecheck.rs b/dhall/src/typecheck.rs index f21721d..e769c7f 100644 --- a/dhall/src/typecheck.rs +++ b/dhall/src/typecheck.rs @@ -1,7 +1,6 @@ #![allow(non_snake_case)] use std::collections::HashSet; use std::fmt; -use std::rc::Rc; use crate::normalize::normalize; use dhall_core; @@ -113,7 +112,7 @@ where go::(&mut ctx, eL0, eR0) } -fn type_of_builtin(b: Builtin) -> Rc> { +fn type_of_builtin(b: Builtin) -> SubExpr { use dhall_core::Builtin::*; match b { Bool | Natural | Integer | Double | Text => dhall_expr!(Type), @@ -184,16 +183,15 @@ fn type_of_builtin(b: Builtin) -> Rc> { /// is not necessary for just type-checking. If you actually care about the /// returned type then you may want to `normalize` it afterwards. pub fn type_with( - ctx: &Context>>, - e: Rc>, -) -> Result>, TypeError> + ctx: &Context>, + e: SubExpr, +) -> Result, TypeError> where S: ::std::fmt::Debug, { use dhall_core::BinOp::*; use dhall_core::Builtin::*; use dhall_core::Const::*; - use dhall_core::Expr; use dhall_core::Expr::*; let mkerr = |msg: TypeMessage<_>| TypeError::new(ctx, e.clone(), msg); let ensure_const = |x: &SubExpr<_, _>, msg: TypeMessage<_>| match x.as_ref() @@ -266,7 +264,7 @@ where return Err(mkerr(NotAFunction(f.clone(), tf))); } }; - let tA = normalize(Rc::clone(tA)); + let tA = normalize(SubExpr::clone(tA)); let tA2 = normalized_type_with(ctx, a.clone())?; if prop_equal(tA.as_ref(), tA2.as_ref()) { let vx0 = &V(x.clone(), 0); @@ -280,9 +278,9 @@ where } Let(f, mt, r, b) => { let r = if let Some(t) = mt { - rc(Annot(Rc::clone(r), Rc::clone(t))) + rc(Annot(SubExpr::clone(r), SubExpr::clone(t))) } else { - Rc::clone(r) + SubExpr::clone(r) }; let tR = type_with(ctx, r)?; @@ -351,7 +349,7 @@ where EmptyListLit(t) => { let s = normalized_type_with(ctx, t.clone())?; ensure_is_type(s, InvalidListType(t.clone()))?; - let t = normalize(Rc::clone(t)); + let t = normalize(SubExpr::clone(t)); return Ok(dhall_expr!(List t)); } NEListLit(xs) => { @@ -377,7 +375,7 @@ where return Ok(dhall_expr!(Optional t)); } NEOptionalLit(x) => { - let t: Rc> = type_with(ctx, x.clone())?; + let t: SubExpr<_, _> = type_with(ctx, x.clone())?; let s = normalized_type_with(ctx, t.clone())?; ensure_is_type(s, InvalidOptionalType(t.clone()))?; let t = normalize(t); @@ -451,9 +449,9 @@ where } pub fn normalized_type_with( - ctx: &Context>>, - e: Rc>, -) -> Result>, TypeError> + ctx: &Context>, + e: SubExpr, +) -> Result, TypeError> where S: ::std::fmt::Debug, { @@ -464,8 +462,8 @@ where /// expression must be closed (i.e. no free variables), otherwise type-checking /// will fail. pub fn type_of( - e: Rc>, -) -> Result>, TypeError> { + e: SubExpr, +) -> Result, TypeError> { let ctx = Context::new(); type_with(&ctx, e) //.map(|e| e.into_owned()) } @@ -474,63 +472,58 @@ pub fn type_of( #[derive(Debug)] pub enum TypeMessage { UnboundVariable, - InvalidInputType(Rc>), - InvalidOutputType(Rc>), - NotAFunction(Rc>, Rc>), - TypeMismatch( - Rc>, - Rc>, - Rc>, - Rc>, - ), - AnnotMismatch(Rc>, Rc>, Rc>), + InvalidInputType(SubExpr), + InvalidOutputType(SubExpr), + NotAFunction(SubExpr, SubExpr), + TypeMismatch(SubExpr, SubExpr, SubExpr, SubExpr), + AnnotMismatch(SubExpr, SubExpr, SubExpr), Untyped, - InvalidListElement(usize, Rc>, Rc>, Rc>), - InvalidListType(Rc>), - InvalidOptionalElement(Rc>, Rc>, Rc>), + InvalidListElement(usize, SubExpr, SubExpr, SubExpr), + InvalidListType(SubExpr), + InvalidOptionalElement(SubExpr, SubExpr, SubExpr), InvalidOptionalLiteral(usize), - InvalidOptionalType(Rc>), - InvalidPredicate(Rc>, Rc>), + InvalidOptionalType(SubExpr), + InvalidPredicate(SubExpr, SubExpr), IfBranchMismatch( - Rc>, - Rc>, - Rc>, - Rc>, + SubExpr, + SubExpr, + SubExpr, + SubExpr, ), - IfBranchMustBeTerm(bool, Rc>, Rc>, Rc>), - InvalidField(Label, Rc>), - InvalidFieldType(Label, Rc>), - InvalidAlternative(Label, Rc>), - InvalidAlternativeType(Label, Rc>), + IfBranchMustBeTerm(bool, SubExpr, SubExpr, SubExpr), + InvalidField(Label, SubExpr), + InvalidFieldType(Label, SubExpr), + InvalidAlternative(Label, SubExpr), + InvalidAlternativeType(Label, SubExpr), DuplicateAlternative(Label), - MustCombineARecord(Rc>, Rc>), + MustCombineARecord(SubExpr, SubExpr), FieldCollision(Label), - MustMergeARecord(Rc>, Rc>), - MustMergeUnion(Rc>, Rc>), + MustMergeARecord(SubExpr, SubExpr), + MustMergeUnion(SubExpr, SubExpr), UnusedHandler(HashSet