I've been trying to learn Haskell by building short programs. I'm somewhat new to the functional programming world but have already done a good amount of reading.
I have a relatively short recursive function in Haskell for using Newton's method to find roots of a function up to the precision allowed by floating point numbers:
newtonsMethod :: (Ord a, Num a, Fractional a) => (a -> a) -> (a -> a) -> a -> a
newtonsMethod f f' x
| f x < epsilon = x
| otherwise =
newtonsMethod f f' (x - (f x / f' x))
where
epsilon = last . map (subtract 1) . takeWhile (/= 1)
. map (+ 1) . iterate (/2) $ 1
When I interpret in GHCi and plug in newtonsMethod (\ x -> cos x + 0.2) (\ x -> -1 * sin x) (-1), I get -1.8797716370899549, which is the first iteration of Newton's method for the values called.
My first question is straightforward: why does it only recurse once? Please also let me know if you see any potential improvements to the way this code is structured or flagrant mistakes.
My second question, a little more involved, is this: is there some clean way to test parent calls of this function, see if it's failing to converge, and bail out accordingly?
Thanks in advance for any answer you can give!
It runs only once because -1.8... is less than epsilon, a strictly positive quantity. You want to check to see if the absolute value of the difference is within tolerance.
One way to get convergence diagnostics for this kind of code is to generate your results as a lazy list, not unlike how you found epsilon using iterate. That means that you can get your final result by traversing the list, but you can also see it in the context of the results that lead up to it.
I couldn't help re-writing it co-recursively and to use automatic differentiation. Of course one should really use the AD package: http://hackage.haskell.org/package/ad. Then you don't have to calculate the derivative yourself and you can see the method converge.
data Dual = Dual Double Double
deriving (Eq, Ord, Show)
constD :: Double -> Dual
constD x = Dual x 0
idD :: Double -> Dual
idD x = Dual x 1.0
instance Num Dual where
fromInteger n = constD $ fromInteger n
(Dual x x') + (Dual y y') = Dual (x + y) (x' + y')
(Dual x x') * (Dual y y') = Dual (x * y) (x * y' + y * x')
negate (Dual x x') = Dual (negate x) (negate x')
signum _ = undefined
abs _ = undefined
instance Fractional Dual where
fromRational p = constD $ fromRational p
recip (Dual x x') = Dual (1.0 / x) (-x' / (x * x))
instance Floating Dual where
pi = constD pi
exp (Dual x x') = Dual (exp x) (x' * exp x)
log (Dual x x') = Dual (log x) (x' / x)
sqrt (Dual x x') = Dual (sqrt x) (x' / (2 * sqrt x))
sin (Dual x x') = Dual (sin x) (x' * cos x)
cos (Dual x x') = Dual (cos x) (x' * (- sin x))
sinh (Dual x x') = Dual (sinh x) (x' * cosh x)
cosh (Dual x x') = Dual (cosh x) (x' * sinh x)
asin (Dual x x') = Dual (asin x) (x' / sqrt (1 - x*x))
acos (Dual x x') = Dual (acos x) (x' / (-sqrt (1 - x*x)))
atan (Dual x x') = Dual (atan x) (x' / (1 + x*x))
asinh (Dual x x') = Dual (asinh x) (x' / sqrt (1 + x*x))
acosh (Dual x x') = Dual (acosh x) (x' / (sqrt (x*x - 1)))
atanh (Dual x x') = Dual (atanh x) (x' / (1 - x*x))
newtonsMethod' :: (Dual -> Dual) -> Double -> [Double]
newtonsMethod' f x = zs
where
zs = x : map g zs
g y = y - a / b
where
Dual a b = f $ idD y
epsilon :: (Eq a, Fractional a) => a
epsilon = last . map (subtract 1) . takeWhile (/= 1)
. map (+ 1) . iterate (/2) $ 1
This gives the following
*Main> take 10 $ newtonsMethod' (\x -> cos x + 0.2) (-1)
[-1.0,
-1.8797716370899549,
-1.770515242616871,
-1.7721539749707398,
-1.7721542475852199,
-1.7721542475852274,
-1.7721542475852274,
-1.7721542475852274,
-1.7721542475852274,
-1.7721542475852274]
Related
I want to realize power function for my custom data type. I mean power (^) which has following signature:
(^) :: (Num a, Integral b) => a -> b -> a
And I mean that my data type MyData should be instance of Num, so I could write
x :: MyData
...
y = x ^ b
where b is some Integral. It's very easy when we need function of one class like
(+), (-), (*) :: (Num a) => a -> a -> a
We just write
instance Num MyData where
(*) x y = someFunc x y
But I have no idea how to define it taking into account that there is also Integral b. That syntax should be like
instance (Integral b) => Num MyData b where
(^) x y = someFunc x y
But I've tried a hundred of such variations and nothing works. Hours of googling also didn't help.
You don't have to do anything to define (^) for your data type; if your type has a Num instance, you get x ^ b for free, because (^) is defined for any type with a Num instance. (It basically just calls * a lot.)
Note that (^) is not a member of Num or Integral; it's just a standalone function whose type is constrained by both classes.
From https://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Real.html#%5E
(^) :: (Num a, Integral b) => a -> b -> a
x0 ^ y0 | y0 < 0 = errorWithoutStackTrace "Negative exponent"
| y0 == 0 = 1
| otherwise = f x0 y0
where -- f : x0 ^ y0 = x ^ y
f x y | even y = f (x * x) (y `quot` 2)
| y == 1 = x
| otherwise = g (x * x) (y `quot` 2) x -- See Note [Half of y - 1]
-- g : x0 ^ y0 = (x ^ y) * z
g x y z | even y = g (x * x) (y `quot` 2) z
| y == 1 = x * z
| otherwise = g (x * x) (y `quot` 2) (x * z) -- See Note [Half of y - 1]
x0 is your MyData value; the only thing (^) ever does with x0 (by virtue of it being passed as the x argument to f or g) is to multiply it by itself, so technically (^) will work as long as you have defined (*) in your Num instance.
I'm really new to Haskell, and it's giving me quite a time. I was trying to write a basic function similar to 'linspace' in Matlab but the compiler seems to reject the idea that 'floor' produces an Integral type. My code:
linspace :: Double -> Double -> Double -> [Double]
linspace x dx y
| y' == y = [x + i * dx | i <- nums]
| otherwise = ([x + i * dx | i <- nums] ++ [y])
where
n = floor ((y - x) / dx)
nums = [0..n]
y' = (x + (fromIntegral n) * dx)
Which produces an error in ghci:
maths.hs:8:21: error:
* No instance for (Integral Double) arising from a use of `floor'
* In the expression: floor ((y - x) / dx)
In an equation for `n': n = floor ((y - x) / dx)
In an equation for `linspace':
linspace x dx y
| y' == y = [x + i * dx | i <- nums]
| otherwise = ([x + i * dx | i <- nums] ++ [y])
where
n = floor ((y - x) / dx)
nums = [0 .. n]
y' = (x + (fromIntegral n) * dx)
maths.hs:10:28: error:
* No instance for (Integral Double)
arising from a use of `fromIntegral'
* In the first argument of `(*)', namely `(fromIntegral n)'
In the second argument of `(+)', namely `(fromIntegral n) * dx'
In the expression: (x + (fromIntegral n) * dx)
Failed, modules loaded: none.
You have bound the result of linspace to be a [Double], due to the type annotation. Therefore, [x + i * dx | i <- nums] must produce such a list of Doubles. x and dx are bound to be Double as they are parameters passed to the function, which are all declared as Double. But what about i? It stems from nums; to have i be a Double, nums must be a [Double].
Nums is defined as
nums = [0..n]
Okay, nums is a list alright. But a list of what? That depends on n; let's take a look!
n = floor ((y - x) / dx)
floor takes, in a nutshell, your Double and produces an Integral. Therefore, nums is a list of integrals. This is the error you're getting: there is no instance of Integral for Double; there is no way for the types to work out nicely.
To fix those errors, you have to make n a Double:
n = fromIntegral $ floor ((y - x) / dx)
As such, your definition of y' must be changed as well:
y' = x + n * dx
In a nutshell
linspace :: Double -> Double -> Double -> [Double]
linspace x dx y
| y' == y = [x + i * dx | i <- nums]
| otherwise = ([x + i * dx | i <- nums] ++ [y])
where
n = fromIntegral $ floor ((y - x) / dx)
nums = [0.. n]
y' = (x + n * dx)
This version compiles:
linspace :: Double -> Double -> Double -> [Double]
linspace x dx y
| y' == y = [x + (fromIntegral i) * dx | i <- nums]
| otherwise = ([x + (fromIntegral i) * dx | i <- nums] ++ [y])
where
n = toInteger $ floor ((y - x) / dx)
nums = [0..n]
y' = (x + (fromIntegral n) * dx)
What are the changes?
As #Alec commented, while floor is logically an integer, it's type is not. You need to use toInteger.
Following that, you need to use fromIntegral when multiplying i and `dex.
I'm a bit at a loss as to why the last pattern in prodV in the following doesn't work:
{-# LANGUAGE GADTs #-}
data V a where
V0 :: Float -> V Float
Vn :: [Float] -> V [Float]
prodV :: (Num a) => V a -> V a -> a
prodV (V0 x) (V0 y) = x * y
-- prodV (Vn x) (Vn y) = zipWith (*) x y -- this one works
prodV (Vn xs) (Vn ys) = [ sum $ zipWith (*) xs ys ]
prodV (V0 n) (Vn ys) = map (* n) ys
GHCi 7.8.3 complains:
Couldn't match type ‘Float’ with ‘[Float]’
Inaccessible code in
a pattern with constructor
Vn :: [Float] -> V [Float],
in an equation for ‘prodV’
In the pattern: Vn ys`
Any pointers? thanks in advance
The signature
prodV :: (Num a) => V a -> V a -> a
mandates that both arguments have the same type parameter, but if you match on V0 and Vn the arguments would have to have the types V Float and V [Float] in which the type parameter does not match.
I'm not quite sure what you want the semantics to be but I'm guessing that you want to define the GADT as something like
data V n where
V0 :: n -> V n
Vn :: [n] -> V n
and your function as either
prodV :: (Num a) => V a -> V a -> V a
prodV (V0 x) (V0 y) = V0 $ x * y
prodV (Vn xs) (Vn ys) = Vn $ zipWith (*) xs ys
prodV (V0 n) (Vn ys) = Vn $ map (* n) ys
or possibly
prodV' :: (Num a) => V a -> V a -> a
prodV' (V0 x) (V0 y) = x * y
prodV' (Vn xs) (Vn ys) = sum $ zipWith (*) xs ys
prodV' (V0 n) (Vn ys) = sum $ map (* n) ys
Can you describe what you would like the function prodV to actually do? The types in your original code don't really make sense to me as they are.
Well, GHC tells you exactly what the problem is. From your definition, V0 something always has the type V Float (and something should be of type Float — but that's not relevant here), while Vn anything always has the type V [Float]. But the type signature for prodV specifies that both have the same type V a. So, in this case a should be Float and [Float] at the same time, which is impossible.
I was just trying to understand how to overload functions in order to mimic algebraic shorthand, but I guess this isn't the right avenue.
What would the idiomatic Haskell way to do this? E.g. from Numeric.Matrix:
adaptScalar f1 f2 f3 x y
| dim x == 1 = f1 (x#>0) y
| dim y == 1 = f3 x (y#>0)
| otherwise = f2 x y
instance Num (Vector Float) where
(+) = adaptScalar addConstant add (flip addConstant)
negate = scale (-1)
(*) = adaptScalar scale mul (flip scale)
signum = vectorMapF Sign
abs = vectorMapF Abs
fromInteger = fromList . return . fromInteger
picks the right implementation of the operator (e.g. + or *) according to the dimension of the operands.
I'm trying out the attached code for linear regression with automatic differentiation.
It specifies a datatype [Dual][2] made of two Floats, and declares it to be instance of Num, Fractional and Floating.
As in all fitting/regression tasks, there's a scalar cost function, parametrized by the fitting parameters c and m, and an optimizer which improves on estimates of these two parameters by gradient descent.
Question
I'm using GHC 7.8.3, and the authors explicitly mention that this is H98 code (I mentioned it in the title because it's the only substantial difference I can think of between my setup and the Author's, however plz correct if wrong).
Why does it choke within the definition of the cost function?
My understanding is: the functions idD and constD map Floats to Duals, g is polymorphic (it can perform algebraic operations on Dual inputs because Dual inherits from Num, Fractional and Floating), and deriv maps Duals to Doubles.
The type signature for g (the eta-reduced cost function wrt the data) was inferred. I tried omitting it, and making it more general by substituting a Floating type constraint to the Fractional one.
Moreover, I tried converting the numeric types of c and m inline with (fromIntegral c :: Double), to no avail.
Specifically this code gives this error:
No instance for (Integral Dual) arising from a use of ‘g’
In the first argument of ‘flip’, namely ‘g’
In the expression: flip g (constD c)
In the second argument of ‘($)’, namely ‘flip g (constD c) $ idD m’
Any hints, please? I'm sure it's a very noob question, but I just don't get it.
The complete code is the following:
{-# LANGUAGE NoMonomorphismRestriction #-}
module ADfw (Dual(..), f, idD, cost) where
data Dual = Dual Double Double deriving (Eq, Show)
constD :: Double -> Dual
constD x = Dual x 0
idD :: Double -> Dual
idD x = Dual x 1.0
instance Num Dual where
fromInteger n = constD $ fromInteger n
(Dual x x') + (Dual y y') = Dual (x+y) (x' + y')
(Dual x x') * (Dual y y') = Dual (x*y) (x*y' + y*x')
negate (Dual x x') = Dual (negate x) (negate x')
signum _ = undefined
abs _ = undefined
instance Fractional Dual where
fromRational p = constD $ fromRational p
recip (Dual x x') = Dual (1.0 / x) (- x' / (x*x))
instance Floating Dual where
pi = constD pi
exp (Dual x x') = Dual (exp x) (x' * exp x)
log (Dual x x') = Dual (log x) (x' / x)
sqrt (Dual x x') = Dual (sqrt x) (x' / (2 * sqrt x))
sin (Dual x x') = Dual (sin x) (x' * cos x)
cos (Dual x x') = Dual (cos x) (x' * (- sin x))
sinh (Dual x x') = Dual (sinh x) (x' * cosh x)
cosh (Dual x x') = Dual (cosh x) (x' * sinh x)
asin (Dual x x') = Dual (asin x) (x' / sqrt (1 - x*x))
acos (Dual x x') = Dual (acos x) (x' / (-sqrt (1 - x*x)))
atan (Dual x x') = Dual (atan x) (x' / (1 + x*x))
asinh (Dual x x') = Dual (asinh x) (x' / sqrt (1 + x*x))
acosh (Dual x x') = Dual (acosh x) (x' / (sqrt (x*x - 1)))
atanh (Dual x x') = Dual (atanh x) (x' / (1 - x*x))
-- example
-- f = sqrt . (* 3) . sin
-- f' x = 3 * cos x / (2 * sqrt (3 * sin x))
-- linear fit sum-of-squares cost
-- cost :: Fractional s => s -> s -> [s] -> [s] -> s
cost m c x y = (/ (2 * (fromIntegral $ length x))) $
sum $ zipWith errSq x y
where
errSq xi yi = zi * zi
where
zi = yi - (m * xi + c)
-- test data
x_ = [1..10]
y_ = [a | a <- [1..20], a `mod` 2 /= 0]
-- learning rate
gamma = 0.04
g :: (Integral s, Fractional s) => s -> s -> s
g m c = cost m c x_ y_
deriv (Dual _ x') = x'
z_ = (0.1, 0.1) : map h z_
h (c, m) = (c - gamma * cd, m - gamma * md) where
cd = deriv $ g (constD m) $ idD c
md = deriv $ flip g (constD c) $ idD m
-- check for convergence
main = do
take 2 $ drop 1000 $ map (\(c, m) -> cost m c x_ y_) z_
take 2 $ drop 1000 $ z_
where the test data x_ and y_ are arrays and the learning rate gamma a scalar.
[2]: The two fields of a Dual object are in fact adjoint one with the other, if we see the derivative as an operator
First, (Integral s, Fractional s) makes no sense; Integral is for Euclidean domains (ones with div and mod), while Fractional is for fields (ones with /). If you have true division all your remainders are going to be zero... .
I think the problem is y_'s attempt to filter to odd numbers. Haskell 98 defines a 'stepped' range form for numbers, so you could write y_ as [1,3..19]. That should allow y_ to be used at the type [Dual], which should allow g to use it without needing the Integral constraint.
Edit: Ørjan Johansen points out that you need an Enum instance for Dual as well, which is actually fairly easy to implement (this is pretty standard for numeric types; I basically copied GHC's instance for Double (which is identical to its instance for Float, for example)):
instance Enum Dual where
succ x = x + 1
pred x = x - 1
toEnum = fromIntegral
fromEnum (Dual x _) = fromEnum x
enumFrom = numericEnumFrom
enumFromTo = numericEnumFromTo
enumFromThen = numericEnumFromThen
enumFromThenTo = numericEnumFromThenTo
In the original code, I don't see a type signature for g. In your code, you have specifically written
g :: (Integral s, Fractional s) => s -> s -> s
The error message says there's no Integral instance for Dual. The code manually defines instances for Num and Fractional, but not Integral.
I'm not actually sure why g needs to be Integral. If you remove that constraint, the code may even work...
EDIT: It seems the Integral instance is necessary because of your use of mod to generate test data. I'm not really sure what this huge block of code does, but I suspect if you apply fromIntegral to convert everything to (say) Double, then it may work.
(I suspect making Dual an instance of Integral is probably not what the original authors intended. Then again, I don't really understand the code, so...)
This is something I have been confused about for a while and I am not sure how I can learn more about it. Let's say I have the following program:
main :: IO ()
main = do
x <- liftM read getLine
y <- liftM read getLine
print (x % y)
If I run this with the input 6 and 2, it will print 3 % 1.
At what point does the simplification happen (namely the division by the gcd)? Is it implemented in show? If so, then is the underlying representation of the rational still 6 % 2? If not, then does (%) do the simplification? I was under the impression that (%) is a data constructor, so how would a data constructor do anything more than "construct"? More importantly, how would I actually go about doing similar things with my own data constructors?
I appreciate any help on the topic.
Ratio is actually implemented in GHC.Real (on GHC, obviously), and is defined as
data Ratio a = !a :% !a deriving (Eq)
The bangs are just there for strictness. As you can see, the function % is not a data constructor, but :% is. Since you aren't supposed to construct a Ratio directly, you use the % function, which calls reduce.
reduce :: (Integral a) => a -> a -> Ratio a
{-# SPECIALISE reduce :: Integer -> Integer -> Rational #-}
reduce _ 0 = ratioZeroDenominatorError
reduce x y = (x `quot` d) :% (y `quot` d)
where d = gcd x y
(%) :: (Integral a) => a -> a -> Ratio a
x % y = reduce (x * signum y) (abs y)
The rule is that if an operator starts with a colon :, then it is a constructor, otherwise it is just a normal operator. In fact, this is part of the Haskell standard, all type operators must have a colon as their first character.
You can just look at the source to see for yourself:
instance (Integral a) => Num (Ratio a) where
(x:%y) + (x':%y') = reduce (x*y' + x'*y) (y*y')
(x:%y) - (x':%y') = reduce (x*y' - x'*y) (y*y')
(x:%y) * (x':%y') = reduce (x * x') (y * y')
negate (x:%y) = (-x) :% y
abs (x:%y) = abs x :% y
signum (x:%_) = signum x :% 1
fromInteger x = fromInteger x :% 1
reduce :: (Integral a) => a -> a -> Ratio a
reduce _ 0 = ratioZeroDenominatorError
reduce x y = (x `quot` d) :% (y `quot` d)
where d = gcd x y