def test_parse_application(): assert parse("a (f b)") == C("a", [C("f", [T("b")])]) assert parse("A (B b) (C c)") == C( "A", [C("B", [T("b")]), C("C", [T("c")])]) with pytest.raises(lark.exceptions.UnexpectedToken): assert parse("(f b) a") == parse("f b a")
def test_scompose_property(): # subtype (scompose f g) = (subtype f) . (subtype g) f = delta("a", T("id")) g = delta("b", T("bb")) fog1 = partial(subtype, scompose(f, g)) fog2 = compose(partial(subtype, f), partial(subtype, g)) assert fog1(I) == fog2(I), f"{fog1(I)} != {fog2(I)}" assert fog1(K) == fog2(K), f"{fog1(K)} != {fog2(K)}" assert fog1(S) == fog2(S), f"{fog1(S)} != {fog2(S)}"
def test_valid_constraints(): assert parse("Eq a => a -> a -> Bool") == ConstrainedType( (), parse("a -> a -> Bool"), [TypeConstraint("Eq", T("a"))]) assert parse("Eq a, Ord a => a -> a -> Bool") == ConstrainedType( (), parse("a -> a -> Bool"), [TypeConstraint("Eq", T("a")), TypeConstraint("Ord", T("a"))], )
def test_unify_basic_vars(): t1 = T("a") t2 = T("b") unification = unify(t1, t2) assert subtype(unification, t1) == subtype(unification, t2) assert unification(t1.name) == t2 assert unification(t2.name) != t1 unification = unify(t2, t1) assert unification(t1.name) == unification(t2.name)
def test_unify_vars_with_cons(): t1 = T("a") t2 = parse("x -> y") unification = unify(t1, t2) assert subtype(unification, t1) == subtype(unification, t2) unification = unify(t2, t1) assert subtype(unification, t1) == subtype(unification, t2)
def test_parse_of_listtypes(): P = parse assert P("[a]") == ListTypeCons(parse("a")) assert P("(a -> b) -> [a] -> [b]") == F( parse("(a -> b)"), F(ListTypeCons(T("a")), ListTypeCons(T("b")))) assert P("[a -> b]") == ListTypeCons(F(T("a"), T("b")))
def test_tvarsupply(): assert list(tvarsupply(".a", limit=2, exclude=".a0")) == [ T(".a1", check=False), T(".a2", check=False), ]
def test_s_combinator(): assert S == F(F(T("a"), parse("b -> c")), F(F(T("a"), T("b")), parse("a -> c")))
def test_k_combinator(): assert K == F(T("a"), F(T("b"), T("a"))) assert K == parse("a -> (b -> a)")
def test_i_combinator(): assert I == F(T("a"), T("a"))