Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- arrange expression for a _associative_ operation type
- -- (therefore ot can be either + or *); examples:
- -- (a+(b+c)+d) -> (((a+b)+c)+d), where a, b, c, d are not Add operations
- -- (a*(b*c)*d) -> (((a*b)*c)*d), where a, b, c, d are not Mul operations
- -- o1 o2
- -- / \ / \
- -- a o2 => o1 b2
- -- / \ / \
- -- b1 b2 a b1
- arrange :: Expr -> Expr
- -- arr a | trace ("arr " ++ show a) False = undefined
- arrange n@Nm{} = n
- arrange ex@(Op _o _a@Nm{} _b@Nm{}) = ex
- arrange (Op o a@Op{} b@Nm{}) = Op o (arrange a) b
- arrange (Op o1 a b@(Op o2 b1 b2)) = case (o1, o2) of
- (Add, Add) -> arrange regroup
- (Add, Sub) -> arrange regroup
- (Mul, Mul) -> arrange regroup
- (Mul, Div) -> arrange regroup
- _ -> Op o1 (arrange a) (arrange b)
- where
- regroup = Op o2 (Op o1 a b1) b2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement