Skip to content

Instantly share code, notes, and snippets.

@kai-qu
Created August 21, 2018 18:11
Show Gist options
  • Save kai-qu/5b95f5e7cba7c9186e6e4f5660020dc5 to your computer and use it in GitHub Desktop.
Save kai-qu/5b95f5e7cba7c9186e6e4f5660020dc5 to your computer and use it in GitHub Desktop.
Command: `stack ghc grad.hs -- -Wtype-defaults`
{-# LANGUAGE AllowAmbiguousTypes, RankNTypes, NoMonomorphismRestriction, ConstraintKinds #-}
-- Minimal AD example with polymorphism.
import Numeric.AD
type Autofloat a = (RealFloat a, Floating a, Real a, Show a, Ord a)
f :: (Autofloat a) => [a] -> a
f [x, y] = x^2 + y^2 + 0.1
appGrad :: (Autofloat a) => (forall b . (Autofloat b) => [b] -> b) -> [a] -> [a]
appGrad f l = grad f l
state :: (Autofloat a) => [a]
state = [1.10, -1.2]
res = appGrad f state
main :: IO ()
main = putStrLn $ show $ res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment