{-# LANGUAGE FlexibleContexts #-} module ASTExamples where import AST import ASTUse import Generics.MultiRec.Base import Generics.MultiRec.Rewriting.Rewriting import Generics.MultiRec.Rewriting.Rules import Generics.MultiRec.Rewriting.Strategies instance Rewrite AST -- | A rewrite rule for expressions. inlineRule var expr = Let (var := expr) (EVar var) :~> expr -- | Example expression example1 = Let ("x" := Mul (Const 6) (Const 9)) (EVar "x") -- | Testing generic rewriting testRewriting1 = rewrite Expr (rule Expr inlineRule) example1 -- | Testing strategies example2 = "y" := example1 -- | Does not apply at top level --testRewriting2 = rewrite Decl (rule Expr inlineRule) example2 -- | testStrategy1 = bottomup f Decl example2 where f :: AST ix -> ix -> ix f Expr = id f Decl = id --rewrite Decl (rule Expr inlineRule) f Var = id