-
Notifications
You must be signed in to change notification settings - Fork 271
/
Plates.hs
25 lines (21 loc) · 884 Bytes
/
Plates.hs
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
{-# LANGUAGE MultiParamTypeClasses, DeriveGeneric, DeriveDataTypeable #-}
import Control.Applicative
import Control.Lens
import GHC.Generics
import Data.Data
import Data.Data.Lens
data Expr = Var Int | Pos Expr String | Neg Expr | Add Expr Expr deriving (Eq,Ord,Show,Read,Generic,Data,Typeable)
data Stmt = Seq [Stmt] | Sel [Expr] | Let String Expr deriving (Eq,Ord,Show,Read,Generic,Data,Typeable)
instance Plated Expr where
plate f (Var x ) = pure (Var x)
plate f (Pos x y) = Pos <$> f x <*> pure y
plate f (Neg x ) = Neg <$> f x
plate f (Add x y) = Add <$> f x <*> f y
instance Plated Stmt where
plate f (Seq xs) = Seq <$> traverse f xs
plate f (Sel xs) = pure (Sel xs)
plate f (Let x y) = pure (Let x y)
exprs :: Traversal' Stmt Expr
exprs f (Seq xs) = Seq <$> traverse (exprs f) xs
exprs f (Sel xs) = Sel <$> traverse f xs
exprs f (Let x y) = Let x <$> f y