From f5e61572b217c5445c3cd593d1cc94697fa7ec48 Mon Sep 17 00:00:00 2001 From: JJ Date: Wed, 19 Jul 2023 12:25:30 -0700 Subject: major cleanups: switch to dynamic Errors, impl Context, rename enums/records to unions/structs --- tests/test_checking.rs | 40 ++++++++++++++++++++-------------------- tests/test_execution.rs | 17 +++++++++-------- tests/test_parser.rs | 44 ++++++++++++++++++++++---------------------- 3 files changed, 51 insertions(+), 50 deletions(-) (limited to 'tests') diff --git a/tests/test_checking.rs b/tests/test_checking.rs index e553a4e..b0f4140 100644 --- a/tests/test_checking.rs +++ b/tests/test_checking.rs @@ -32,15 +32,15 @@ fn test_parsing_succeeds() { #[test] fn test_inference() { let context = Context::new(); - assert_eq!(infer(&context, parse_lambda(sanity_check).unwrap()), Ok(Int)); - assert_eq!(infer(&context, parse_lambda(negate).unwrap()), Ok(Func(Bool, Bool))); - assert_eq!(infer(&context, parse_lambda(basic_abstraction).unwrap()), Ok(Func(Int, Int))); - assert_eq!(infer(&context, parse_lambda(basic_application).unwrap()), Ok(Int)); - assert_eq!(infer(&context, parse_lambda(correct_cond_abs).unwrap()), Ok(Func(Bool, Int))); - assert_eq!(infer(&context, parse_lambda(correct_cond).unwrap()), Ok(Nat)); - assert!(infer(&context, parse_lambda(not_inferrable).unwrap()).is_err()); - assert!(infer(&context, parse_lambda(incorrect_branches).unwrap()).is_err()); - assert!(infer(&context, parse_lambda(incorrect_cond_abs).unwrap()).is_err()); + assert_eq!(context.infer(parse_lambda(sanity_check).unwrap()).unwrap(), Int); + assert_eq!(context.infer(parse_lambda(negate).unwrap()).unwrap(), Func(Bool, Bool)); + assert_eq!(context.infer(parse_lambda(basic_abstraction).unwrap()).unwrap(), Func(Int, Int)); + assert_eq!(context.infer(parse_lambda(basic_application).unwrap()).unwrap(), Int); + assert_eq!(context.infer(parse_lambda(correct_cond_abs).unwrap()).unwrap(), Func(Bool, Int)); + assert_eq!(context.infer(parse_lambda(correct_cond).unwrap()).unwrap(), Nat); + assert!(context.infer(parse_lambda(not_inferrable).unwrap()).is_err()); + assert!(context.infer(parse_lambda(incorrect_branches).unwrap()).is_err()); + assert!(context.infer(parse_lambda(incorrect_cond_abs).unwrap()).is_err()); } #[test] @@ -48,17 +48,17 @@ fn test_checking() { let context = Context::new(); // uninteresting - assert!(check(&context, parse_lambda(sanity_check).unwrap(), &Int).is_ok()); - assert!(check(&context, parse_lambda(negate).unwrap(), &Func(Bool, Bool)).is_ok()); - assert!(check(&context, parse_lambda(basic_abstraction).unwrap(), &Func(Int, Int)).is_ok()); - assert!(check(&context, parse_lambda(basic_application).unwrap(), &Int).is_ok()); - assert!(check(&context, parse_lambda(correct_cond_abs).unwrap(), &Func(Bool, Int)).is_ok()); - assert!(check(&context, parse_lambda(correct_cond).unwrap(), &Nat).is_ok()); - assert!(check(&context, parse_lambda(incorrect_branches).unwrap(), &Unit).is_err()); - assert!(check(&context, parse_lambda(incorrect_cond_abs).unwrap(), &Error).is_err()); + assert!(context.check(parse_lambda(sanity_check).unwrap(), &Int).is_ok()); + assert!(context.check(parse_lambda(negate).unwrap(), &Func(Bool, Bool)).is_ok()); + assert!(context.check(parse_lambda(basic_abstraction).unwrap(), &Func(Int, Int)).is_ok()); + assert!(context.check(parse_lambda(basic_application).unwrap(), &Int).is_ok()); + assert!(context.check(parse_lambda(correct_cond_abs).unwrap(), &Func(Bool, Int)).is_ok()); + assert!(context.check(parse_lambda(correct_cond).unwrap(), &Nat).is_ok()); + assert!(context.check(parse_lambda(incorrect_branches).unwrap(), &Unit).is_err()); + assert!(context.check(parse_lambda(incorrect_cond_abs).unwrap(), &Error).is_err()); // more fun - assert!(check(&context, parse_lambda(not_inferrable).unwrap(), &Func(Bool, Func(Int, Func(Int, Int)))).is_ok()); - assert!(check(&context, parse_lambda(not_inferrable).unwrap(), &Func(Bool, Func(Nat, Func(Nat, Nat)))).is_ok()); - assert!(check(&context, parse_lambda(not_inferrable).unwrap(), &Func(Bool, Func(Unit, Func(Unit, Unit)))).is_ok()); + assert!(context.check(parse_lambda(not_inferrable).unwrap(), &Func(Bool, Func(Int, Func(Int, Int)))).is_ok()); + assert!(context.check(parse_lambda(not_inferrable).unwrap(), &Func(Bool, Func(Nat, Func(Nat, Nat)))).is_ok()); + assert!(context.check(parse_lambda(not_inferrable).unwrap(), &Func(Bool, Func(Unit, Func(Unit, Unit)))).is_ok()); } diff --git a/tests/test_execution.rs b/tests/test_execution.rs index d9b1c5e..44df5ae 100644 --- a/tests/test_execution.rs +++ b/tests/test_execution.rs @@ -5,10 +5,10 @@ use chrysanthemum::util::*; #[test] fn test_simple() { let context = Context::new(); - assert_eq!(execute(&context, Const(Term::Boolean(false))), Ok(Term::Boolean(false))); - assert_eq!(execute(&context, Const(Term::Natural(123))), Ok(Term::Natural(123))); - assert_eq!(execute(&context, Const(Term::Integer(123))), Ok(Term::Integer(123))); - assert!(execute(&context, Var("x")).is_err()); + assert_eq!(context.execute(Const(Term::Boolean(false))).unwrap(), Term::Boolean(false)); + assert_eq!(context.execute(Const(Term::Natural(123))).unwrap(), Term::Natural(123)); + assert_eq!(context.execute(Const(Term::Integer(123))).unwrap(), Term::Integer(123)); + assert!(context.execute(Var("x")).is_err()); } #[test] @@ -16,8 +16,9 @@ fn test_complex() { let mut context = Context::new(); context.insert(String::from("x"), Term::Natural(413)); context.insert(String::from("y"), Term::Boolean(true)); - assert_eq!(execute(&context, Var("x")), Ok(Term::Natural(413))); - assert_eq!(execute(&context, Cond(Var("y"), Const(Term::Integer(612)), Var("x"))), Ok(Term::Integer(612))); - assert_eq!(execute(&context, - App(Abs("z", Cond(Const(Term::Boolean(false)), Var("x"), Var("z"))), Const(Term::Integer(1025)))), Ok(Term::Integer(1025))); + assert_eq!(context.execute(Var("x")).unwrap(), Term::Natural(413)); + assert_eq!(context.execute(Cond(Var("y"), Const(Term::Integer(612)), + Var("x"))).unwrap(), Term::Integer(612)); + assert_eq!(context.execute(App(Abs("z", Cond(Const(Term::Boolean(false)), + Var("x"), Var("z"))), Const(Term::Integer(1025)))).unwrap(), Term::Integer(1025)); } diff --git a/tests/test_parser.rs b/tests/test_parser.rs index 9ac8f32..84f6395 100644 --- a/tests/test_parser.rs +++ b/tests/test_parser.rs @@ -6,9 +6,9 @@ use chrysanthemum::util::*; #[test] fn test_simple_phrases() { - assert_eq!(parse_lambda("-123"), Ok(Const(Term::Integer(-123)))); - assert_eq!(parse_lambda("x12"), Ok(Var("x12"))); - assert_eq!(parse_lambda("x12x2"), Ok(Var("x12x2"))); + assert_eq!(parse_lambda("-123").unwrap(), Const(Term::Integer(-123))); + assert_eq!(parse_lambda("x12").unwrap(), Var("x12")); + assert_eq!(parse_lambda("x12x2").unwrap(), Var("x12x2")); // so i _don't_ want these to be valid identifiers: // but i actually have no idea why my peg is rejecting them lmao assert!(parse_lambda("12x").is_err()); @@ -17,36 +17,36 @@ fn test_simple_phrases() { #[test] fn test_simple_annotations() { - assert_eq!(parse_lambda("t: int"), Ok(Ann(Var("t"), Int))); - assert_eq!(parse_lambda("12: nat"), Ok(Ann(Const(Term::Natural(12)), Nat))); + assert_eq!(parse_lambda("t: int").unwrap(), Ann(Var("t"), Int)); + assert_eq!(parse_lambda("12: nat").unwrap(), Ann(Const(Term::Natural(12)), Nat)); assert!(parse_lambda("t: fake").is_err()); } #[test] fn test_simple_expressions() { - assert_eq!(parse_lambda("λx.y"), Ok(Abs("x", Var("y")))); - assert_eq!(parse_lambda("λ x.y"), Ok(Abs("x", Var("y")))); - assert_eq!(parse_lambda("λx.y"), Ok(Abs("x", Var("y")))); - assert_eq!(parse_lambda("lambda x . y"), Ok(Abs("x", Var("y")))); - assert_eq!(parse_lambda("(λx.y)"), Ok(Abs("x", Var("y")))); - assert_eq!(parse_lambda("(λx.y) x"), Ok(App(Abs("x", Var("y")), Var("x")))); - assert_eq!(parse_lambda("(λx.y) x"), Ok(App(Abs("x", Var("y")), Var("x")))); - assert_eq!(parse_lambda("if x then y else z"), Ok(Cond(Var("x"), Var("y"), Var("z")))); - assert_eq!(parse_lambda("if xeme then yak else zebra"), Ok(Cond(Var("xeme"), Var("yak"), Var("zebra")))); - assert_eq!(parse_lambda("if 413 then 612 else 1025"), Ok(Cond(Const(Term::Natural(413)), Const(Term::Natural(612)), Const(Term::Natural(1025))))); // invalid, but should parse + assert_eq!(parse_lambda("λx.y").unwrap(), Abs("x", Var("y"))); + assert_eq!(parse_lambda("λ x.y").unwrap(), Abs("x", Var("y"))); + assert_eq!(parse_lambda("λx.y").unwrap(), Abs("x", Var("y"))); + assert_eq!(parse_lambda("lambda x . y").unwrap(), Abs("x", Var("y"))); + assert_eq!(parse_lambda("(λx.y)").unwrap(), Abs("x", Var("y"))); + assert_eq!(parse_lambda("(λx.y) x").unwrap(), App(Abs("x", Var("y")), Var("x"))); + assert_eq!(parse_lambda("(λx.y) x").unwrap(), App(Abs("x", Var("y")), Var("x"))); + assert_eq!(parse_lambda("if x then y else z").unwrap(), Cond(Var("x"), Var("y"), Var("z"))); + assert_eq!(parse_lambda("if xeme then yak else zebra").unwrap(), Cond(Var("xeme"), Var("yak"), Var("zebra"))); + assert_eq!(parse_lambda("if 413 then 612 else 1025").unwrap(), Cond(Const(Term::Natural(413)), Const(Term::Natural(612)), Const(Term::Natural(1025)))); // invalid, but should parse } #[test] fn test_complex_expressions() { - assert_eq!(parse_lambda("(λy.if y then false else true) z"), Ok(App(Abs("y", Cond(Var("y"), Const(Term::Boolean(false)), Const(Term::Boolean(true)))), Var("z")))); + assert_eq!(parse_lambda("(λy.if y then false else true) z").unwrap(), App(Abs("y", Cond(Var("y"), Const(Term::Boolean(false)), Const(Term::Boolean(true)))), Var("z"))); } #[test] fn test_complex_annotations() { - assert_eq!(parse_lambda("(lambda x . y) : int"), Ok(Ann(Abs("x", Var("y")), Int))); - assert_eq!(parse_lambda("((lambda x. y): (int -> int)) -413: int"), Ok(App(Ann(Abs("x", Var("y")), Func(Int, Int) ), Ann(Const(Term::Integer(-413)), Int)))); - assert_eq!(parse_lambda("if false: bool then true: bool else 2: int"), Ok(Cond(Ann(Const(Term::Boolean(false)), Bool), Ann(Const(Term::Boolean(true)), Bool), Ann(Const(Term::Natural(2)), Int)))); - assert_eq!(parse_lambda("(lambda x. if x then true: bool else false: bool): (int -> bool)"), Ok(Ann(Abs("x", Cond(Var("x"), Ann(Const(Term::Boolean(true)), Bool), Ann(Const(Term::Boolean(false)), Bool))), Func(Int, Bool)))); - assert_eq!(parse_lambda("(lambda x. if x then 1: int else 0: int): (bool -> int)"), Ok(Ann(Abs("x", Cond(Var("x"), Ann(Const(Term::Natural(1)), Int), Ann(Const(Term::Natural(0)), Int))), Func(Bool, Int)))); - assert_eq!(parse_lambda("(lambda x. if x then false else true): (bool -> bool)"), Ok(Ann(Abs("x", Cond(Var("x"), Const(Term::Boolean(false)), Const(Term::Boolean(true)))), Func(Bool, Bool)))); + assert_eq!(parse_lambda("(lambda x . y) : int").unwrap(), Ann(Abs("x", Var("y")), Int)); + assert_eq!(parse_lambda("((lambda x. y): (int -> int)) -413: int").unwrap(), App(Ann(Abs("x", Var("y")), Func(Int, Int) ), Ann(Const(Term::Integer(-413)), Int))); + assert_eq!(parse_lambda("if false: bool then true: bool else 2: int").unwrap(), Cond(Ann(Const(Term::Boolean(false)), Bool), Ann(Const(Term::Boolean(true)), Bool), Ann(Const(Term::Natural(2)), Int))); + assert_eq!(parse_lambda("(lambda x. if x then true: bool else false: bool): (int -> bool)").unwrap(), Ann(Abs("x", Cond(Var("x"), Ann(Const(Term::Boolean(true)), Bool), Ann(Const(Term::Boolean(false)), Bool))), Func(Int, Bool))); + assert_eq!(parse_lambda("(lambda x. if x then 1: int else 0: int): (bool -> int)").unwrap(), Ann(Abs("x", Cond(Var("x"), Ann(Const(Term::Natural(1)), Int), Ann(Const(Term::Natural(0)), Int))), Func(Bool, Int))); + assert_eq!(parse_lambda("(lambda x. if x then false else true): (bool -> bool)").unwrap(), Ann(Abs("x", Cond(Var("x"), Const(Term::Boolean(false)), Const(Term::Boolean(true)))), Func(Bool, Bool))); } -- cgit v1.2.3-70-g09d2