Skip to content

Instantly share code, notes, and snippets.

@kai-qu
Created August 21, 2018 18:12
Show Gist options
  • Save kai-qu/786fa0fd46b9a2fa387e806f6bee3cbb to your computer and use it in GitHub Desktop.
Save kai-qu/786fa0fd46b9a2fa387e806f6bee3cbb 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
{- Result:
grad.hs:10:12: warning: [-Wtype-defaults]
• Defaulting the following constraints to type ‘Integer’
(Integral b0) arising from a use of ‘^’ at grad.hs:10:12-14
(Num b0) arising from the literal ‘2’ at grad.hs:10:14
• In the first argument of ‘(+)’, namely ‘x ^ 2’
In the first argument of ‘(+)’, namely ‘x ^ 2 + y ^ 2’
In the expression: x ^ 2 + y ^ 2 + 0.1
grad.hs:10:18: warning: [-Wtype-defaults]
• Defaulting the following constraints to type ‘Integer’
(Integral b0) arising from a use of ‘^’ at grad.hs:10:18-20
(Num b0) arising from the literal ‘2’ at grad.hs:10:20
• In the second argument of ‘(+)’, namely ‘y ^ 2’
In the first argument of ‘(+)’, namely ‘x ^ 2 + y ^ 2’
In the expression: x ^ 2 + y ^ 2 + 0.1
grad.hs:21:19: warning: [-Wtype-defaults]
• Defaulting the following constraints to type ‘Double’
(Show a0) arising from a use of ‘show’ at grad.hs:21:19-28
(RealFloat a0) arising from a use of ‘res’ at grad.hs:21:26-28
• In the second argument of ‘($)’, namely ‘show $ res’
In the expression: putStrLn $ show $ res
In an equation for ‘main’: main = putStrLn $ show $ res
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment