ad- Automatic Differentiation

PortabilityGHC only
Safe HaskellNone



This module provides reverse-mode Automatic Differentiation implementation using linear time topological sorting after the fact.

For this form of reverse-mode AD we use StableName to recover sharing information from the tape to avoid combinatorial explosion, and thus run asymptotically faster than it could without such sharing information, but the use of side-effects contained herein is benign.



newtype Kahn a s Source

Kahn is a Mode using reverse-mode automatic differentiation that provides fast diffFU, diff2FU, grad, grad2 and a fast jacobian when you have a significantly smaller number of outputs than inputs.


Kahn (Tape a (Kahn a s)) 


Typeable2 Kahn 
(Num a, Bounded a) => Bounded (Kahn a s) 
(Num a, Enum a) => Enum (Kahn a s) 
(Num a, Eq a) => Eq (Kahn a s) 
Floating a => Floating (Kahn a s) 
Fractional a => Fractional (Kahn a s) 
Num a => Num (Kahn a s) 
(Num a, Ord a) => Ord (Kahn a s) 
Real a => Real (Kahn a s) 
RealFloat a => RealFloat (Kahn a s) 
RealFrac a => RealFrac (Kahn a s) 
Show a => Show (Kahn a s) 
MuRef (Kahn a s) 
Erf a => Erf (Kahn a s) 
InvErf a => InvErf (Kahn a s) 
Num a => Mode (Kahn a s) 
Num a => Jacobian (Kahn a s) 
Num a => Grad (Kahn a ()) [a] (a, [a]) a 
Grad i o o' a => Grad (Kahn a () -> i) (a -> o) (a -> o') a 

data Tape a t Source

A Tape records the information needed back propagate from the output to each input during reverse Mode AD.


Lift !a 
Var !a !Int 
Binary !a a a t t 
Unary !a a t 


Typeable2 Tape 
(Data a, Data t) => Data (Tape a t) 
(Show a, Show t) => Show (Tape a t) 

partials :: forall s a. Num a => Kahn a s -> [(Int, a)]Source

This returns a list of contributions to the partials. The variable ids returned in the list are likely not unique!

partialArray :: Num a => (Int, Int) -> Kahn a s -> Array Int aSource

Return an Array of partials given bounds for the variable IDs.

partialMap :: Num a => Kahn a s -> IntMap aSource

Return an IntMap of sparse partials

derivative :: Num a => Kahn a s -> aSource

derivative' :: Num a => Kahn a s -> (a, a)Source

vgrad :: Grad i o o' a => i -> oSource

vgrad' :: Grad i o o' a => i -> o'Source

class Num a => Grad i o o' a | i -> a o o', o -> a i o', o' -> a i o whereSource


pack :: i -> [Kahn a ()] -> Kahn a ()Source

unpack :: ([a] -> [a]) -> oSource

unpack' :: ([a] -> (a, [a])) -> o'Source


Num a => Grad (Kahn a ()) [a] (a, [a]) a 
Grad i o o' a => Grad (Kahn a () -> i) (a -> o) (a -> o') a 

bind :: Traversable f => f a -> (f (Kahn a s), (Int, Int))Source

unbind :: Functor f => f (Kahn a s) -> Array Int a -> f aSource

unbindMap :: (Functor f, Num a) => f (Kahn a s) -> IntMap a -> f aSource

unbindWith :: (Functor f, Num a) => (a -> b -> c) -> f (Kahn a s) -> Array Int b -> f cSource

unbindMapWithDefault :: (Functor f, Num a) => b -> (a -> b -> c) -> f (Kahn a s) -> IntMap b -> f cSource

primal :: Num a => Kahn a s -> aSource

var :: a -> Int -> Kahn a sSource

varId :: Kahn a s -> IntSource