Higher Order Functions#

%3 outer_cluster_2 cluster_2 outer_cluster_9 cluster_9 outer_cluster_12 cluster_12 outer_cluster_11 cluster_11 outer_cluster_0 cluster_0 outer_cluster_8 cluster_8 outer_cluster_5 cluster_5 outer_cluster_3 cluster_3 outer_cluster_UnstableFn_Math_Math-0 cluster_UnstableFn_Math_Math-0 outer_cluster_i64-5871781006564002453 cluster_i64-5871781006564002453 outer_cluster_i64-17615343019692007359 cluster_i64-17615343019692007359 outer_cluster_i64-11743562013128004906 cluster_i64-11743562013128004906 Math___init__-5871781006564002453:s->i64-5871781006564002453 Math___init__-17615343019692007359:s->i64-17615343019692007359 lambda x: x + Math(1)-15121139857639374588:s->Math___add__-13656135949156587278 Math___add__-13656135949156587278:s->Math___init__-5871781006564002453 Math___add__-13656135949156587278:s->Math___init__-5871781006564002453 Math___add__-1186789197313807224:s->Math___init__-5871781006564002453 Math___add__-1186789197313807224:s->lambda x: x + Math(1)-11743562013128004906 lambda x: x + Math(1)-11743562013128004906:s->Math___init__-5871781006564002453 Math___init__-11743562013128004906:s->i64-11743562013128004906 MathList_map-11974064242427520944:s->MathList_map-11974064242427520944 MathList_map-11974064242427520944:s->UnstableFn_Math_Math-0 MathList_append-10871952703302121208:s->lambda x: x + Math(1)-15121139857639374588 MathList_append-10871952703302121208:s->MathList_map-2868860904042873558 MathList_map-2868860904042873558:s->UnstableFn_Math_Math-0 MathList_map-2868860904042873558:s->MathList_append-5270882181845974234 MathList_map-1351883367118893594:s->UnstableFn_Math_Math-0 MathList_map-1351883367118893594:s->MathList_append-6194465120113050586 MathList_append-6194465120113050586:s->Math___add__-13656135949156587278 MathList_append-6194465120113050586:s->MathList_append-5270882181845974234 incr_list-10912160959110460649:s->MathList_append-6194465120113050586 MathList_append-5270882181845974234:s->Math___init__-5871781006564002453 MathList_append-5270882181845974234:s->MathList___init__-0 MathList_append-8648460026357343916:s->lambda x: x + Math(1)-11743562013128004906 MathList_append-8648460026357343916:s->MathList_map-11974064242427520944 Math___init__-5871781006564002453 Math i64-5871781006564002453 1 Math___init__-17615343019692007359 Math i64-17615343019692007359 3 lambda x: x + Math(1)-15121139857639374588 lambda x: x + Math(1) Math___add__-13656135949156587278 · + · Math___add__-1186789197313807224 · + · lambda x: x + Math(1)-11743562013128004906 lambda x: x + Math(1) Math___init__-11743562013128004906 Math i64-11743562013128004906 2 MathList_map-11974064242427520944 ·.map UnstableFn_Math_Math-0 lambda x: x + Math(1) MathList___init__-0 MathList MathList_append-10871952703302121208 ·.append MathList_map-2868860904042873558 ·.map MathList_map-1351883367118893594 ·.map MathList_append-6194465120113050586 ·.append incr_list-10912160959110460649 incr_list MathList_append-5270882181845974234 ·.append MathList_append-8648460026357343916 ·.append


from __future__ import annotations

from typing import TYPE_CHECKING

from egglog import *

if TYPE_CHECKING:
    from collections.abc import Callable


class Math(Expr):
    def __init__(self, i: i64Like) -> None: ...
    def __add__(self, other: Math) -> Math: ...


class MathList(Expr):
    def __init__(self) -> None: ...
    def append(self, i: Math) -> MathList: ...
    def map(self, f: Callable[[Math], Math]) -> MathList: ...


@ruleset
def math_ruleset(i: i64, j: i64, xs: MathList, x: Math, f: Callable[[Math], Math]):  # noqa: ANN201
    yield rewrite(Math(i) + Math(j)).to(Math(i + j))
    yield rewrite(xs.append(x).map(f)).to(xs.map(f).append(f(x)))
    yield rewrite(MathList().map(f)).to(MathList())


@function(ruleset=math_ruleset)
def incr_list(xs: MathList) -> MathList:
    return xs.map(lambda x: x + Math(1))


egraph = EGraph()
y = egraph.let("y", incr_list(MathList().append(Math(1)).append(Math(2))))
egraph.run(math_ruleset.saturate())
egraph.check(eq(y).to(MathList().append(Math(2)).append(Math(3))))

egraph

Total running time of the script: (0 minutes 0.040 seconds)

Gallery generated by Sphinx-Gallery