Пример #1
0
def test_parse_letrec_oddeven():
    even = Expr.as_proc(["x"],
                Expr.as_if(
                    Expr.as_iszero(Expr.as_var("x")),
                    Expr.as_lit(1),
                    Expr.as_call(
                        Expr.as_var("odd"),
                            Expr.as_diff(
                                Expr.as_var("x"),
                                Expr.as_lit(1))))).procexpr
    odd = Expr.as_proc(["x"],
                Expr.as_if(
                    Expr.as_iszero(Expr.as_var("x")),
                    Expr.as_lit(0),
                    Expr.as_call(
                        Expr.as_var("even"),
                            Expr.as_diff(
                                Expr.as_var("x"),
                                Expr.as_lit(1))))).procexpr
    e2 = Expr.as_letrec({"even": even, "odd": odd},
            Expr.as_call(Expr.as_var("odd"), Expr.as_lit(13)))
    runtest("""
        letrec
            even(x) = if (isz x) then 1 else (odd -(x,1))
            odd(x) = if (isz x) then 0 else (even -(x,1))
        in (odd 13)
    """, e2)
Пример #2
0
def test_parse_letrec_double():
    e2 = Expr.as_letrec({
        "double": Expr.as_proc(["x"],
                    Expr.as_if(Expr.as_iszero(Expr.as_var("x")),
                               Expr.as_lit(0),
                               Expr.as_diff(
                                   Expr.as_call(Expr.as_var("double"),
                                                Expr.as_diff(
                                                    Expr.as_var("x"),
                                                    Expr.as_lit(1))),
                                   Expr.as_lit(-2)))).procexpr
        }, Expr.as_call(Expr.as_var("double"), Expr.as_lit(6)))
    runtest("""
        letrec
            double(x) = (if (isz x) then 0 else -((double -(x,1)), -2))
        in (double 6)
    """, e2)