When doing calculations modulo n with large numbers, you will encounter huge performency penalties when doing for example mod (123456789^987654321) n. Instead you have to use your own ^ that internally calculates mod n also for intermedite calculations.
Sure, I can easily implement my own functions, but then I have to explicitly say "mod n" for each operation. Instead one could build an numeric expression tree and defer actual calculations, and in the end state modulo n only once. (see my code below)
I started on this to clearly show what I mean, but I wonder if there already exists implementations of this, it seems quite useful so somebody ought to have implemented it.
module Modulo where
data Expr =
V Integer
| Plus Expr Expr
| Mult Expr Expr
deriving (Eq, Show)
instance Num Expr where
(+) = Plus
(*) = Mult
fromInteger = V
eval :: Integer -> Expr -> Integer
eval m (V i) = i `mod` m
eval m (Plus e1 e2) = (eval m e1 + eval m e2) `mod` m
eval m (Mult e1 e2) = (eval m e1 * eval m e2) `mod` m
fifteen :: Expr
fifteen = 10 + 5
test = eval 13 fifteen
Oleg did something of this kind, where you make an instance for modulo arithmetic, but for a arbitrary modulus. Implicit configurations.
Related
I was writing my own haskell datatype to resolve a sum operation with integers, but i dont know how to make the semantic .
data Expr = Value Int
| Sum Expr Expr
I was trying to do:
sum:: Expr -> Expr -> Int
sum a b = b + a
val:: Int -> Int
val a = a
I want to write like:
Sum (Value 3) (Value 5)
And get 8 as return, any ideas?
Typically, in this sort of situation, you write an "evaluator" or "interpreter" -- a single function that accepts an Expr and evaluates it to a value:
eval :: Expr -> Int
Then, you can write:
> eval (Sum (Value 3) (Value 5))
8
>
Haskell's pattern matching for function definitions makes this very elegant:
eval (Value x) = ...
eval (Sum e1 e2) = ...you'll need to use eval recursively here...
So, instead of writing multiple functions, one for each component of your Expr, you write a single function with one pattern-based definition for each component.
If this was homework, you might want to stop here and try to figure the details out for yourself. If it wasn't, then the following should work:
eval :: Expr -> Int
eval (Value x) = x
eval (Sum e1 e2) = eval e1 + eval e2
You need to define sum appropriately for each combination of data constructors.
sum :: Expr -> Expr -> Int
sum (Value x) (Value y) = x + y
sum (Sum a b) (Sum c d) = sum a b + sum c d
sum (Value x) (Sub a b) = x + sum a b
sum (Sum a b) (Value y) = sum a b + y
This can be simplified; one way is with a helper function that reduces a single Expr to an integer value first.
value :: Expr -> Int
value (Value x) = x
value (Sum x y) = (value x) + (value y)
sum :: Expr -> Expr -> Int
sum x y = value x + value y
I'm new to Haskell and trying to create a function that evaluates an arithmetic expression:
data Expr = Number Rational | Variable String | Plus Expr Expr deriving (Eq, Show , Read)
eval (Plus lhs rhs) = Number (a + b)
where Number a = eval lhs
Number b = eval rhs
eval x = x
main = putStrLn . show $ eval (Plus (Number 1) (Variable "x"))
I want the eval function to evaluate the expression until it hits a Variable, meaning whenever the two expressions within Plus evaluate to a Number you can collapse it to a new Number, otherwise you cannot collapse the Plus but maybe you can collapse only one side of the Plus. eg: eval (Plus (Number a) (Number b)) == Number (a + b) eval (Plus (Var "a") b) == Plus (Var "a") (eval b)
I expected the pattern Number b to fail and to move on to eval x = x
and just return the expression as is, but instead it produces an error:
main: main.hs:5:9-27: Irrefutable pattern failed for pattern Main.Number b
Why is this happening and how can I solve it ?
You will want to use a case expression to match all the possible outcomes of the inner eval calls, not just Numbers:
eval (Plus lhs rhs) = case (eval lhs, eval rhs) of
(Number a, Number b) -> Number (a + b) -- two numbers can be summed
(otherA, otherB) -> Plus otherA otherB -- other values form a Plus with collapsed parts
eval x = x
You can use ViewPatterns to make this relatively pretty:
{-# LANGUAGE ViewPatterns #-}
eval (Plus (eval -> Number a) (eval -> Number b)) = Number (a+b)
eval x = x
Given your comments, I would be remiss if I didn't point out there are some flaws with your proposed algorithm, though; for example:
> eval (Number 3 `Plus` Number 4 `Plus` Variable "a") -- doesn't know about associativity of Plus
Plus (Plus (Number (3 % 1)) (Number (4 % 1))) (Variable "a")
> eval (Variable "a" `Plus` (Number 3 `Plus` Number 4)) -- doesn't simplify recursively when it hits a variable
Plus (Variable "a") (Plus (Number (3 % 1)) (Number (4 % 1)))
Consider normalizing to a less structured type; perhaps something like
data Sum = Sum Rational (MultiSet String)
telling the sum of all the Numbers in the first part and how often each variable appeared in the second part. Then
eval :: Expr -> Sum
eval (Number r) = Sum r empty
eval (Variable v) = Sum 0 (singleton v)
eval (Plus l r) = Sum (r+r') (union vs vs') where
Sum r vs = eval l
Sum r' vs' = eval r
I understand how to create and evaluate a simple data-type Expr. For example like this:
data Expr = Lit Int | Add Expr Expr | Sub Expr Expr | [...]
eval :: Expr -> Int
eval (Lit x) = x
eval (Add x y) = eval x + eval y
eval (Sub x y) = eval x - eval y
So here is my question: How can I add Variables to this Expr type, which should be evaluated for its assigned value? It should look like this:
data Expr = Var Char | Lit Int | Add Expr Expr [...]
type Assignment = Char -> Int
eval :: Expr -> Assignment -> Int
How do I have to do my eval function now for (Var Char) and (Add Expr Expr)? I think I figured out the easiest, how to do it for Lit already.
eval (Lit x) _ = x
For (Var Char) I tried a lot, but I cant get an Int out of an Assignment.. Thought It would work like this:
eval (Var x) (varname number) = number
Well if you model your enviroment as
type Env = Char -> Int
Then all you have is
eval (Var c) env = env c
But this isn't really "correct". For one, what happens with unbound variables? So perhaps a more accurate type is
type Env = Char -> Maybe Int
emptyEnv = const Nothing
And now we can see whether a variable is unbound
eval (Var c) env = maybe handleUnboundCase id (env c)
And now we can use handleUnboundCase to do something like assign a default value, blow up the program, or make monkeys climb out of your ears.
The final question to ask is "how are variables bound?". If you where looking for a "let" statement like we have in Haskell, then we can use a trick known as HOAS (higher order abstract syntax).
data Exp = ... | Let Exp (Exp -> Exp)
The HOAS bit is that (Exp -> Exp). Essentially we use Haskell's name-binding to implement our languages. Now to evaluate a let expression we do
eval (Let val body) = body val
This let's us dodge Var and Assignment by relying on Haskell to resolve the variable name.
An example let statement in this style might be
Let 1 $ \x -> x + x
-- let x = 1 in x + x
The biggest downside here is that modelling mutability is a royal pain, but this was already the case when relying on the Assignment type vs a concrete map.
You need to apply your Assignment function to the variable name to get the Int:
eval (Var x) f = f x
This works because f :: Char -> Int and x:: Char, so you can just do f x to get an Int.
Pleasingly this will work across a collection of variable names.
Example
ass :: Assignment
ass 'a' = 1
ass 'b' = 2
meaning that
eval ((Add (Var 'a') (Var 'b')) ass
= eval (Var 'a') ass + eval (Var 'b') ass
= ass 'a' + ass 'b'
= 1 + 2
= 3
Pass the assignment functions through to other calls of eval
You need to keep passing the assignment function around until you get integers:
eval (Add x y) f = eval x f + eval y f
Different order?
If you're allowed to change the types, it seems more logical to me to put the assignment function first and the data second:
eval :: Assignment -> Expr -> Int
eval f (Var x) = f x
eval f (Add x y) = eval f x + eval f y
...but I guess you can think of it as a constant expression with varying variables (feels imperative)rather than a constant set of values across a range of expressions (feels like referential transparency).
I would recommend using Map from Data.Map instead. You could implement it something like
import Data.Map (Map)
import qualified Data.Map as M -- A lot of conflicts with Prelude
-- Used to map operations through Maybe
import Control.Monad (liftM2)
data Expr
= Var Char
| Lit Int
| Add Expr Expr
| Sub Expr Expr
| Mul Expr Expr
deriving (Eq, Show, Read)
type Assignment = Map Char Int
eval :: Expr -> Assignment -> Maybe Int
eval (Lit x) _ = Just x
eval (Add x y) vars = liftM2 (+) (eval x vars) (eval y vars)
eval (Sub x y) vars = liftM2 (-) (eval x vars) (eval y vars)
eval (Mul x y) vars = liftM2 (*) (eval x vars) (eval y vars)
eval (Var x) vars = M.lookup x vars
But this looks clunky, and we'd have to keep using liftM2 op every time we added an operation. Let's clean it up a bit with some helpers
(|+|), (|-|), (|*|) :: (Monad m, Num a) => m a -> m a -> m a
(|+|) = liftM2 (+)
(|-|) = liftM2 (-)
(|*|) = liftM2 (*)
infixl 6 |+|, |-|
infixl 7 |*|
eval :: Expr -> Assignment -> Maybe Int
eval (Lit x) _ = return x -- Use generic return instead of explicit Just
eval (Add x y) vars = eval x vars |+| eval y vars
eval (Sub x y) vars = eval x vars |-| eval y vars
eval (Mul x y) vars = eval x vars |*| eval y vars
eval (Var x) vars = M.lookup x vars
That's a better, but we still have to pass around the vars everywhere, this is ugly to me. Instead, we can use the ReaderT monad from the mtl package. The ReaderT monad (and the non-transformer Reader) is a very simple monad, it exposes a function ask that returns the value you pass in when it's run, where all you can do is "read" this value, and is usually used for running an application with static configuration. In this case, our "config" is an Assignment.
This is where the liftM2 operators really come in handy
-- This is a long type signature, let's make an alias
type ExprM a = ReaderT Assignment Maybe a
-- Eval still has the same signature
eval :: Expr -> Assignment -> Maybe Int
eval expr vars = runReaderT (evalM expr) vars
-- evalM is essentially our old eval function
evalM :: Expr -> ExprM Int
evalM (Lit x) = return x
evalM (Add x y) = evalM x |+| evalM y
evalM (Sub x y) = evalM x |-| evalM y
evalM (Mul x y) = evalM x |*| evalM y
evalM (Var x) = do
vars <- ask -- Get the static "configuration" that is our list of vars
lift $ M.lookup x vars
-- or just
-- evalM (Var x) = ask >>= lift . M.lookup x
The only thing that we really changed was that we have to do a bit extra whenever we encounter a Var x, and we removed the vars parameter. I think this makes evalM very elegant, since we only access the Assignment when we need it, and we don't even have to worry about failure, it's completely taken care of by the Monad instance for Maybe. There isn't a single line of error handling logic in this entire algorithm, yet it will gracefully return Nothing if a variable name is not present in the Assignment.
Also, consider if later you wanted to switch to Doubles and add division, but you also want to return an error code so you can determine if there was a divide by 0 error or a lookup error. Instead of Maybe Double, you could use Either ErrorCode Double where
data ErrorCode
= VarUndefinedError
| DivideByZeroError
deriving (Eq, Show, Read)
Then you could write this module as
data Expr
= Var Char
| Lit Double
| Add Expr Expr
| Sub Expr Expr
| Mul Expr Expr
| Div Expr Expr
deriving (Eq, Show, Read)
type Assignment = Map Char Double
data ErrorCode
= VarUndefinedError
| DivideByZeroError
deriving (Eq, Show, Read)
type ExprM a = ReaderT Assignment (Either ErrorCode) a
eval :: Expr -> Assignment -> Either ErrorCode Double
eval expr vars = runReaderT (evalM expr) vars
throw :: ErrorCode -> ExprM a
throw = lift . Left
evalM :: Expr -> ExprM Double
evalM (Lit x) = return x
evalM (Add x y) = evalM x |+| evalM y
evalM (Sub x y) = evalM x |-| evalM y
evalM (Mul x y) = evalM x |*| evalM y
evalM (Div x y) = do
x' <- evalM x
y' <- evalM y
if y' == 0
then throw DivideByZeroError
else return $ x' / y'
evalM (Var x) = do
vars <- ask
maybe (throw VarUndefinedError) return $ M.lookup x vars
Now we do have explicit error handling, but it isn't bad, and we've been able to use maybe to avoid explicitly matching on Just and Nothing.
This is a lot more information than you really need to solve this problem, I just wanted to present an alternative solution that uses the monadic properties of Maybe and Either to provide easy error handling and use ReaderT to clean up that noise of passing an Assignment argument around everywhere.
In an exam today I was asked to create an expression evaluation tree in Haskell.
Usually the answer is as simple as:
data Expr = Value Integer
| Add Expr Expr
| Sub Expr Expr
| Mul Expr Expr
And to evaluate it, you just use a function such as:
eval :: Expr -> Integer
eval (Value x) = x
eval (Add l r) = eval l + eval r
eval (Sub l r) = eval l - eval r
eval (Mul l r) = eval l * eval r
However today, we were given a data type:
data Op = Add
| Sub
| Mul
So I assumed to create the expression tree I could just do:
data Expr = Value Integer
| Op Expr Expr
And use the same eval function. However, I have since written that function and loaded it into GHCI, but it does not seem to work.
Could anyone explain why this doesn't work?
You must define a data constructor (providing a name)
data Expr = Value Integer | Compute Op Expr Expr
^^^^^^^
then
eval :: Expr -> Integer
eval (Value x) = x
eval (Compute Add l r) = eval l + eval r
and so on.
:)
I'm working on a function that can count the number of operators used in an expression. My code is as follows:
data Expr = Lit Int |
Expr :+: Expr |
Expr :-: Expr
size :: Expr -> Int
size (Lit n) = 0
size (e1 :+: e2) = 1 + (size e1) + (size e2)
size (e1 :-: e2) = 1 + (size e1) + (size e2)
But when I try to execute this code using Hugs98 i get the following error:
Main> size 2+3
ERROR - Cannot infer instance
*** Instance : Num Expr
*** Expression : size 2 + 3
Can somebody tell me what I'm doing wrong? I'm really out of idea's myself.
2+3 is not a valid expression. With your types, primtive values are created using the Lit data constructor, and the valid operators are :+: and :-:. So what you really need is Lit 2 :+: Lit 3. So try
size (Lit 2 :+: Lit 3)
You can make it a Num instance:
instance Num Expr where
(+) = (:+:)
(-) = (:-:)
negate = (0 -)
fromInteger = Lit . fromInteger
(*) = error "not implemented"
abs = error "not implemented"
signum = error "not implemented"
Once that is in place, size (2+3) will work (note the parenthesis).