Skip to content

Commit

Permalink
add the commutations function
Browse files Browse the repository at this point in the history
  • Loading branch information
rudymatela committed Feb 9, 2024
1 parent 9736c30 commit cbceab1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Test/Speculate/Expr/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ module Test.Speculate.Expr.Core
, unification
, isCanonInstanceOf
, hasCanonInstanceOf

-- * Commuting
, commutations
)
where

Expand Down Expand Up @@ -136,3 +139,17 @@ e1 `hasCanonInstanceOf` e2 | e1 `isCanonInstanceOf` e2 = True
(e1f :$ e1x) `hasCanonInstanceOf` e2 | e1f `hasCanonInstanceOf` e2 ||
e1x `hasCanonInstanceOf` e2 = True
_ `hasCanonInstanceOf` _ = False

commutations :: [Expr] -> Expr -> [Expr]
commutations cos = cmms
where
cmms (eo :$ ex :$ ey) | isValue eo && eo `elem` cos
= concat [ [eo :$ ex' :$ ey', eo :$ ey' :$ ex']
| ex' <- cmms ex
, ey' <- cmms ey
]
cmms (ef :$ ex) = [ ef' :$ ex'
| ef' <- cmms ef
, ex' <- cmms ex
]
cmms e = [e]
19 changes: 19 additions & 0 deletions test/expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ tests n =
, (yy,ff2 xx xx)
]

, commutations [plus] (xx -+- yy) == [xx -+- yy, yy -+- xx]
, commutations [times] (xx -*- yy) == [xx -*- yy, yy -*- xx]
, commutations [times] (xx -+- yy) == [xx -+- yy]
, commutations [plus] (xx -*- yy) == [xx -*- yy]

, commutations [plus] (xx -+- (yy -+- zz))
== [ xx -+- (yy -+- zz)
, (yy -+- zz) -+- xx
, xx -+- (zz -+- yy)
, (zz -+- yy) -+- xx
]

, commutations [plus,times] (xx -+- (yy -*- zz))
== [ xx -+- (yy -*- zz)
, (yy -*- zz) -+- xx
, xx -+- (zz -*- yy)
, (zz -*- yy) -+- xx
]

, constifications xx == map constify [xx]

, constifications (xx -+- yy)
Expand Down

0 comments on commit cbceab1

Please sign in to comment.