JacquesCarette / Drasil

Generate all the things (focusing on research software)
https://jacquescarette.github.io/Drasil
BSD 2-Clause "Simplified" License
136 stars 25 forks source link

Merged AddRe, MulRe, AddI and MulI into Add and Mul #3794

Closed NoahCardoso closed 2 weeks ago

NoahCardoso commented 3 weeks ago

It makes sense to merge these to reduce the amount of redundant code within Drasil. Closes #3153

-- | Add two expressions. add (C.Lit (Int 0)) r = r add l (C.Lit (Int 0)) = l add (C.Lit (Dbl 0)) r = r add l (C.Lit (Dbl 0)) = l add l (C.Lit (ExactDbl 0)) = l add (C.Lit (ExactDbl 0)) r = r add (C.Lit (Int i1)) (C.Lit (Int i2)) = C.Lit (Int (i1 + i2)) add (C.Lit (Dbl r1)) (C.Lit (Dbl r2)) = C.Lit (Dbl (r1 + r2)) add (C.Lit (Int i)) (C.Lit (Dbl r)) = C.Lit (Dbl (fromIntegral i + r)) add (C.Lit (Dbl r)) (C.Lit (Int i)) = C.Lit (Dbl (r + fromIntegral i)) add l r = C.AssocA C.Add [l, r]

-- | Multiply two expressions. mul (C.Lit (Int 1)) r = r mul l (C.Lit (Int 1)) = l mul (C.Lit (Dbl 1.0)) r = r mul l (C.Lit (Dbl 1.0)) = l mul l (C.Lit (ExactDbl 1)) = l mul (C.Lit (ExactDbl 1)) r = r mul (C.Lit (Int i1)) (C.Lit (Int i2)) = C.Lit (Int (i1 i2)) mul (C.Lit (Dbl r1)) (C.Lit (Dbl r2)) = C.Lit (Dbl (r1 r2)) mul (C.Lit (Int i)) (C.Lit (Dbl r)) = C.Lit (Dbl (fromIntegral i r)) mul (C.Lit (Dbl r)) (C.Lit (Int i)) = C.Lit (Dbl (r fromIntegral i)) mul (C.AssocA C.Mul l) (C.AssocA C.Mul r) = C.AssocA C.Mul (l ++ r) mul (C.AssocA C.Mul l) (C.AssocA C.Mul r) = C.AssocA C.Mul (l ++ r) mul l r = C.AssocA C.Mul [l,r]

Also added Natural and Rational numbers to Add and Mul.

assocArithOperToTy :: AssocArithOper -> Space assocArithOperToTy Add = S.Integer assocArithOperToTy Add = S.Real assocArithOperToTy Add = S.Natural assocArithOperToTy Add = S.Rational assocArithOperToTy Mul = S.Integer assocArithOperToTy Mul = S.Real assocArithOperToTy Mul = S.Natural assocArithOperToTy Mul = S.Rational

JacquesCarette commented 2 weeks ago

While this is under active discussion, I won't review. Please ask me again to review when this is ready.

balacij commented 2 weeks ago

If you can, please address the notes in separate commits, and then write down "Fixed in X", where X is the commit hash, if/when you address the comments. That just removes the need for us to re-read the entire PR.