-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathif-instance.cabal
144 lines (106 loc) · 2.82 KB
/
if-instance.cabal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
cabal-version: 3.0
name: if-instance
version: 0.5.2.0
synopsis: Branch on whether a constraint is satisfied
license: BSD-3-Clause
build-type: Simple
author: Sam Derbyshire
maintainer: Sam Derbyshire
copyright: 2021 Sam Derbyshire
homepage: https://github.com/sheaf/if-instance
category: Type System, Plugin
description:
This library provides a mechanism that can be used to branch on
whether a constraint is satisfied (not limited to typeclass instances).
Usage example:
@
{-# OPTIONS_GHC -fplugin=IfSat.Plugin #-}
module MyModule where
import Data.Constraint.If ( IfSat(ifSat) )
hypot :: forall a. ( Floating a, IfSat (FMA a) ) => a -> a -> a
hypot = ifSat @(FMA a) withFMA withoutFMA
where
withFMA :: FMA a => a -> a -> a
withFMA x y =
let
h = sqrt $ fma x x (y * y)
h² = h * h
x² = x * x
u = fma (-y) y (h² - x²) + fma h h (-h²) - fma x x (-x²)
in
h - u / ( 2 * h )
withoutFMA :: a -> a -> a
withoutFMA x y = sqrt ( x * x + y * y )
@
Here we select between two ways of computing the hypotenuse function
based on whether we have access to the fused multiply-add operation
@ fma :: FMA a => a -> a -> a -> a @
which computes @ \\ x y z -> ( x * y ) + z @ in a single instruction,
providing stronger guarantees about precision of the result.
A call of the form @hypot \@MyNumberType@ will either use the robust @withFMA@
function when an @FMA MyNumberType@ instance is available, or will fallback
to the simple @withoutFMA@ implementation when no such instance can be found.
extra-source-files:
changelog.md
common common
build-depends:
base
>= 4.15.0 && < 4.22,
ghc
>= 9.0 && < 9.14,
default-language:
Haskell2010
ghc-options:
-Wall
-Wcompat
-fwarn-missing-local-signatures
-fwarn-incomplete-uni-patterns
-fwarn-missing-deriving-strategies
-fno-warn-unticked-promoted-constructors
library
import:
common
hs-source-dirs:
src
build-depends:
ghc-tcplugin-api
>= 0.13 && < 0.15,
exposed-modules:
Data.Constraint.If
IfSat.Plugin
other-modules:
IfSat.Plugin.Compat
default-language:
Haskell2010
ghc-options:
-Wall
-Wcompat
-fwarn-missing-local-signatures
-fwarn-incomplete-uni-patterns
-fwarn-missing-deriving-strategies
-fno-warn-unticked-promoted-constructors
library if-instance-example
import:
common
hs-source-dirs:
example
build-depends:
if-instance
exposed-modules:
M2
other-modules:
M1
test-suite if-instance-test
import:
common
type:
exitcode-stdio-1.0
build-depends:
if-instance
hs-source-dirs:
test
main-is:
Main.hs
other-modules:
Framework
Tests