summaryrefslogtreecommitdiff
path: root/elixir/ast_test.exs
blob: b10c9597f3fc8e43d3312c6c23dee470b8322b2d (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
defmodule AstTest do
  use ExUnit.Case

  test "pprint/1" do
    assert Ast.pprint(Ast.var("x")) == "x"
    assert Ast.pprint(Ast.abs("x", Ast.var("x"))) == "(λ x . x)"
    assert Ast.pprint(Ast.app(Ast.var("x"), Ast.var("y"))) == "x y"
  end

  test "pprint with if expression lambda calculus" do
    assert Ast.pprint(Ast.abs("f", Ast.abs("y", Ast.app(Ast.var("f"), Ast.var("x"))))) ==
             "(λ f . (λ y . f x))"
  end

  test "free_vars-1" do
    program = Ast.var("x")
    expected = MapSet.new(["x"])
    repr_expected = "x"
    repr_subs_expected = "y"

    assert MapSet.equal?(Ast.free_vars(program), expected)
    assert Ast.pprint(program) == repr_expected

    assert Ast.pprint(Ast.subst(program, "x", Ast.var("y"))) == repr_subs_expected
  end

  test "free_vars-2" do
    program = Ast.abs("x", Ast.var("x"))
    expected = MapSet.new([])
    repr_expected = "(λ x . x)"
    repr_subs_expected = "(λ x . x)"

    assert MapSet.equal?(Ast.free_vars(program), expected)
    assert Ast.pprint(program) == repr_expected

    assert Ast.pprint(Ast.subst(program, "x", Ast.var("y"))) == repr_subs_expected
  end

  test "free_vars-3" do
    program = Ast.app(Ast.var("f"), Ast.var("x"))
    expected = MapSet.new(["f", "x"])
    repr_expected = "f x"

    assert MapSet.equal?(Ast.free_vars(program), expected)
    assert Ast.pprint(program) == repr_expected
  end

  test "free_vars-4" do
    program =
      Ast.abs(
        "f",
        Ast.app(Ast.var("x"), Ast.abs("x", Ast.app(Ast.var("f"), Ast.var("x"))))
      )

    expected = MapSet.new(["x"])
    repr_expected = "(λ f . x (λ x . f x))"

    assert MapSet.equal?(Ast.free_vars(program), expected)
    assert Ast.pprint(program) == repr_expected
  end

  test "free_vars-5" do
    program = Ast.abs("f", Ast.abs("y", Ast.app(Ast.var("f"), Ast.var("x"))))
    expected = MapSet.new(["x"])
    repr_expected = "(λ f . (λ y . f x))"

    assert MapSet.equal?(Ast.free_vars(program), expected)
    assert Ast.pprint(program) == repr_expected
  end
end