simple-reflect 0.1 → 0.2
raw patch · 2 files changed
+40/−39 lines, 2 files
Files
- Debug/SimpleReflect/Expr.hs +18/−17
- simple-reflect.cabal +22/−22
Debug/SimpleReflect/Expr.hs view
@@ -15,7 +15,7 @@ ( -- * Construction Expr , FromExpr(..)- , var, fun+ , var, fun, Associativity(..), op -- * Evaluating , expr, reduce, reduction ) where@@ -57,15 +57,16 @@ lift :: Show a => a -> Expr lift x = emptyExpr { showExpr = \p -> showsPrec p x } -data Fixity = L | R deriving Eq+-- | This data type specifies the associativity of operators: left, right or none. +data Associativity = InfixL | Infix | InfixR deriving Eq --- | A operator as expression-op :: Fixity -> Int -> String -> Expr -> Expr -> Expr+-- | An infix operator with the given associativity, precedence and name+op :: Associativity -> Int -> String -> Expr -> Expr -> Expr op fix prec op a b = emptyExpr { showExpr = showFun } where showFun p = showParen (p > prec)- $ showExpr a (if fix == L then prec else prec + 1)+ $ showExpr a (if fix == InfixL then prec else prec + 1) . showString op- . showExpr b (if fix == R then prec else prec + 1)+ . showExpr b (if fix == InfixR then prec else prec + 1) ------------------------------------------------------------------------------ -- Adding numeric results@@ -100,9 +101,9 @@ fromExpr = id instance (Show a, FromExpr b) => FromExpr (a -> b) where- fromExpr f a = fromExpr $ op L 10 " " f (lift a)+ fromExpr f a = fromExpr $ op InfixL 10 " " f (lift a) --- | A generic function variable+-- | A generic, overloaded, function variable fun :: FromExpr a => String -> a fun = fromExpr . var @@ -141,9 +142,9 @@ max = fun "max" `iOp2` max `dOp2` max instance Num Expr where- (+) = withReduce2 $ op L 6 " + " `iOp2` (+) `dOp2` (+)- (-) = withReduce2 $ op L 6 " - " `iOp2` (-) `dOp2` (-)- (*) = withReduce2 $ op L 7 " * " `iOp2` (*) `dOp2` (*)+ (+) = withReduce2 $ op InfixL 6 " + " `iOp2` (+) `dOp2` (+)+ (-) = withReduce2 $ op InfixL 6 " - " `iOp2` (-) `dOp2` (-)+ (*) = withReduce2 $ op InfixL 7 " * " `iOp2` (*) `dOp2` (*) negate = withReduce $ fun "negate" `iOp` negate `dOp` negate abs = withReduce $ fun "abs" `iOp` abs `dOp` abs signum = withReduce $ fun "signum" `iOp` signum `dOp` signum@@ -160,16 +161,16 @@ instance Integral Expr where quotRem a b = (quot a b, rem a b) divMod a b = (div a b, mod a b)- quot = withReduce2 $ op L 7 " `quot` " `iOp2` quot- rem = withReduce2 $ op L 7 " `rem` " `iOp2` rem- div = withReduce2 $ op L 7 " `div` " `iOp2` div- mod = withReduce2 $ op L 7 " `mod` " `iOp2` mod+ quot = withReduce2 $ op InfixL 7 " `quot` " `iOp2` quot+ rem = withReduce2 $ op InfixL 7 " `rem` " `iOp2` rem+ div = withReduce2 $ op InfixL 7 " `div` " `iOp2` div+ mod = withReduce2 $ op InfixL 7 " `mod` " `iOp2` mod toInteger expr = case intExpr expr of Just i -> i _ -> error "not a number" instance Fractional Expr where- (/) = withReduce2 $ op L 7 " / " `dOp2` (/)+ (/) = withReduce2 $ op InfixL 7 " / " `dOp2` (/) recip = withReduce $ fun "recip" `dOp` recip fromRational r = fromDouble (fromRational r) @@ -180,7 +181,7 @@ exp = withReduce $ fun "exp" `dOp` exp sqrt = withReduce $ fun "sqrt" `dOp` sqrt log = withReduce $ fun "log" `dOp` log- (**) = withReduce2 $ op R 8 "**" `dOp2` (**)+ (**) = withReduce2 $ op InfixR 8 "**" `dOp2` (**) sin = withReduce $ fun "sin" `dOp` sin cos = withReduce $ fun "cos" `dOp` cos sinh = withReduce $ fun "sinh" `dOp` sinh
simple-reflect.cabal view
@@ -1,22 +1,22 @@-name: simple-reflect-version: 0.1-homepage: http://twan.home.fmf.nl/blog/haskell/simple-reflection-of-expressions.details-license: BSD3-license-file: LICENSE-author: Twan van Laarhoven-maintainer: twanvl@gmail.com-category: Debug-cabal-version: >= 1.2-build-type: Simple-synopsis: Simple reflection of expressions containing variables-description:- This package allows simple reflection of expressions containing variables.- Reflection here means that a Haskell expression is turned into a string.- - The primary aim of this package is teaching and understanding;- there are no options for manipulating the reflected expressions beyond showing them.-build-depends: base-exposed-modules:- Debug.SimpleReflect- Debug.SimpleReflect.Expr- Debug.SimpleReflect.Vars+name: simple-reflect +version: 0.2 +homepage: http://twan.home.fmf.nl/blog/haskell/simple-reflection-of-expressions.details +license: BSD3 +license-file: LICENSE +author: Twan van Laarhoven +maintainer: twanvl@gmail.com +category: Debug +cabal-version: >= 1.2 +build-type: Simple +synopsis: Simple reflection of expressions containing variables +description: + This package allows simple reflection of expressions containing variables. + Reflection here means that a Haskell expression is turned into a string. + + The primary aim of this package is teaching and understanding; + there are no options for manipulating the reflected expressions beyond showing them. +build-depends: base +exposed-modules: + Debug.SimpleReflect + Debug.SimpleReflect.Expr + Debug.SimpleReflect.Vars