Skip to content

Commit 86873cf

Browse files
committed
New operators for 'Eq'-like and 'Ord'-like signals.
We already have the '.foo.' pattern for these typeclasses. After this change, you can use '.foo bar' instead when `bar` is a constant.
1 parent 45d09f6 commit 86873cf

File tree

4 files changed

+201
-7
lines changed

4 files changed

+201
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ADDED: Alongside the existing Eq-like and Ord-like signal operators like `.==.` and `.<=.` etc. There are now new functions for comparing with constants: `.==`, `==.`, `./=`, `/=.`, `.<=`, `<=.`, `.>=`, `>=.`, `.>`, `>.`, `.<`, `<.`, `.&&`, `&&.`, `.||`, `||.`. These are useful for comparing signals with constants in a more readable way. For example, `a .==. pure True` can now be replaced with `a .== True`.

clash-prelude/src/Clash/Explicit/Signal.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,9 @@ module Clash.Explicit.Signal
276276
, testFor
277277
-- * Type classes
278278
-- ** 'Eq'-like
279-
, (.==.), (./=.)
279+
, (.==.), (.==), (==.), (./=.), (./=), (/=.)
280280
-- ** 'Ord'-like
281-
, (.<.), (.<=.), (.>=.), (.>.)
281+
, (.<.), (.<), (<.), (.<=.), (.<=), (<=.), (.>=.), (.>=), (>=.), (.>.), (.>), (>.)
282282
-- * Bisignal functions
283283
, veryUnsafeToBiSignalIn
284284
, readFromBiSignal

clash-prelude/src/Clash/Signal.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,9 @@ module Clash.Signal
241241
, testFor
242242
-- * Type classes
243243
-- ** 'Eq'-like
244-
, (.==.), (./=.)
244+
, (.==.), (.==), (==.), (./=.), (./=), (/=.)
245245
-- ** 'Ord'-like
246-
, (.<.), (.<=.), (.>=.), (.>.)
246+
, (.<.), (.<), (<.), (.<=.), (.<=), (<=.), (.>=.), (.>=), (>=.), (.>.), (.>), (>.)
247247
-- * Bisignal functions
248248
, veryUnsafeToBiSignalIn
249249
, readFromBiSignal

clash-prelude/src/Clash/Signal/Internal.hs

+196-3
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ module Clash.Signal.Internal
138138
, resetGen
139139
, resetGenN
140140
-- * Boolean connectives
141-
, (.&&.), (.||.)
141+
, (.&&.), (&&.), (.&&), (.||.), (||.), (.||)
142142
-- * Simulation functions (not synthesizable)
143143
, simulate
144144
-- ** lazy version
@@ -157,9 +157,9 @@ module Clash.Signal.Internal
157157
, testFor
158158
-- * Type classes
159159
-- ** 'Eq'-like
160-
, (.==.), (./=.)
160+
, (.==.), (.==), (==.), (./=.), (./=), (/=.)
161161
-- ** 'Ord'-like
162-
, (.<.), (.<=.), (.>=.), (.>.)
162+
, (.<.), (.<), (<.), (.<=.), (.<=), (<=.), (.>=.), (.>=), (>=.), (.>.), (.>), (>.)
163163
-- ** 'Functor'
164164
, mapSignal#
165165
-- ** 'Applicative'
@@ -1421,6 +1421,30 @@ infixr 2 .||.
14211421
(.||.) :: Applicative f => f Bool -> f Bool -> f Bool
14221422
(.||.) = liftA2 (||)
14231423

1424+
infix 2 .||
1425+
-- | The above type is a generalization for:
1426+
--
1427+
-- @
1428+
-- __(.||)__ :: 'Ord' a => 'Clash.Signal.Signal' Bool -> 'Bool' -> 'Clash.Signal.Signal' 'Bool'
1429+
-- @
1430+
--
1431+
-- It is a version of ('||') that allows comparing a @'Clash.Signal.Signal' Bool@ with a constant
1432+
-- @Bool@ and returns a 'Clash.Signal.Signal' of 'Bool'
1433+
(.||) :: Functor f => f Bool -> Bool -> f Bool
1434+
a .|| b = fmap (|| b) a
1435+
1436+
infixr 2 ||.
1437+
-- | The above type is a generalization for:
1438+
--
1439+
-- @
1440+
-- __(||.)__ :: 'Clash.Signal.Signal' 'Bool' -> 'Clash.Signal.Signal' 'Bool' -> 'Clash.Signal.Signal' 'Bool'
1441+
-- @
1442+
--
1443+
-- It is a version of ('||') that allows comparing a constant @Bool@ with a @'Clash.Signal.Signal' Bool@
1444+
-- and returns a 'Clash.Signal.Signal' of 'Bool'
1445+
(||.) :: Functor f => Bool -> f Bool -> f Bool
1446+
a ||. b = fmap (a ||) b
1447+
14241448
infixr 3 .&&.
14251449
-- | The above type is a generalization for:
14261450
--
@@ -1432,6 +1456,30 @@ infixr 3 .&&.
14321456
(.&&.) :: Applicative f => f Bool -> f Bool -> f Bool
14331457
(.&&.) = liftA2 (&&)
14341458

1459+
infixr 3 .&&
1460+
-- | The above type is a generalization for:
1461+
--
1462+
-- @
1463+
-- __(.&&)__ :: 'Clash.Signal.Signal' 'Bool' -> 'Clash.Signal.Signal' 'Bool' -> 'Clash.Signal.Signal' 'Bool'
1464+
-- @
1465+
--
1466+
-- It is a version of ('&&') that allows comparing a @'Clash.Signal.Signal' Bool@ with a
1467+
-- constant @Bool@ and returns a 'Clash.Signal.Signal' of 'Bool'
1468+
(.&&) :: (Functor f) => f Bool -> Bool -> f Bool
1469+
(.&&) a b = fmap (&& b) a
1470+
1471+
infixr 3 &&.
1472+
-- | The above type is a generalization for:
1473+
--
1474+
-- @
1475+
-- __(&&.)__ :: 'Clash.Signal.Signal' 'Bool' -> 'Clash.Signal.Signal' 'Bool' -> 'Clash.Signal.Signal' 'Bool'
1476+
-- @
1477+
--
1478+
-- It is a version of ('&&') that allows comparing a constant @'Bool@ with a
1479+
-- @'Clash.Signal.Signal' Bool@ and returns a 'Clash.Signal.Signal' of 'Bool'
1480+
(&&.) :: (Functor f) => Bool -> f Bool -> f Bool
1481+
(&&.) a b = fmap (a &&) b
1482+
14351483
-- [Note: register strictness annotations]
14361484
--
14371485
-- In order to produce the first (current) value of the register's output
@@ -1611,6 +1659,30 @@ infix 4 .==.
16111659
(.==.) :: (Eq a, Applicative f) => f a -> f a -> f Bool
16121660
(.==.) = liftA2 (==)
16131661

1662+
infix 4 .==
1663+
-- | The above type is a generalization for:
1664+
--
1665+
-- @
1666+
-- __(.==)__ :: 'Eq' a => 'Clash.Signal.Signal' a -> a -> 'Clash.Signal.Signal' 'Bool'
1667+
-- @
1668+
--
1669+
-- It is a version of ('==') that allows comparing a @'Clash.Signal.Signal' a@ with a
1670+
-- constant @a@ and returns a 'Clash.Signal.Signal' of 'Bool'
1671+
(.==) :: (Eq a, Functor f) => f a -> a -> f Bool
1672+
(.==) a b = fmap (==b) a
1673+
1674+
infix 4 ==.
1675+
-- | The above type is a generalization for:
1676+
--
1677+
-- @
1678+
-- __(==.)__ :: 'Eq' a => a -> 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' 'Bool'
1679+
-- @
1680+
--
1681+
-- It is a version of ('==') that allows comparing a @'Clash.Signal.Signal' a@ with a
1682+
-- constant @a@ and returns a 'Clash.Signal.Signal' of 'Bool'
1683+
(==.) :: (Eq a, Functor f) => a -> f a -> f Bool
1684+
(==.) a b = fmap (a==) b
1685+
16141686
infix 4 ./=.
16151687
-- | The above type is a generalization for:
16161688
--
@@ -1622,6 +1694,31 @@ infix 4 ./=.
16221694
(./=.) :: (Eq a, Applicative f) => f a -> f a -> f Bool
16231695
(./=.) = liftA2 (/=)
16241696

1697+
infix 4 ./=
1698+
-- | The above type is a generalization for:
1699+
--
1700+
-- @
1701+
-- __(./=)__ :: 'Eq' a => 'Clash.Signal.Signal' a -> a -> 'Clash.Signal.Signal' 'Bool'
1702+
-- @
1703+
--
1704+
-- It is a version of ('/=') that allows comparing a @'Clash.Signal.Signal' a@ with a
1705+
-- constant @a@ and returns a 'Clash.Signal.Signal' of 'Bool'
1706+
(./=) :: (Eq a, Functor f) => f a -> a -> f Bool
1707+
(./=) a b = fmap (/=b) a
1708+
1709+
infix 4 /=.
1710+
-- | The above type is a generalization for:
1711+
--
1712+
-- @
1713+
-- __(/=.)__ :: 'Eq' a => a -> 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' 'Bool'
1714+
-- @
1715+
--
1716+
-- It is a version of ('/=') that allows comparing a @'Clash.Signal.Signal' a@ with a
1717+
-- constant @a@ and returns a 'Clash.Signal.Signal' of 'Bool'
1718+
1719+
(/=.) :: (Eq a, Functor f) => a -> f a -> f Bool
1720+
(/=.) a b = fmap (a /=) b
1721+
16251722
infix 4 .<.
16261723
-- | The above type is a generalization for:
16271724
--
@@ -1633,6 +1730,30 @@ infix 4 .<.
16331730
(.<.) :: (Ord a, Applicative f) => f a -> f a -> f Bool
16341731
(.<.) = liftA2 (<)
16351732

1733+
infix 4 <.
1734+
-- | The above type is a generalization for:
1735+
--
1736+
-- @
1737+
-- __(<.)__ :: 'Ord' a => a -> 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' 'Bool'
1738+
-- @
1739+
--
1740+
-- It is a version of ('<') that allows comparing a @'Clash.Signal.Signal' a@ with a constant
1741+
-- @a@ and returns a 'Clash.Signal.Signal' of 'Bool'
1742+
(<.) :: (Ord a, Functor f) => a -> f a -> f Bool
1743+
(<.) a b = fmap (a<) b
1744+
1745+
infix 4 .<
1746+
-- | The above type is a generalization for:
1747+
--
1748+
-- @
1749+
-- __(.<)__ :: 'Ord' a => 'Clash.Signal.Signal' a -> a -> 'Clash.Signal.Signal' 'Bool'
1750+
-- @
1751+
--
1752+
-- It is a version of ('<') that allows comparing a @'Clash.Signal.Signal' a@ with a constant
1753+
-- @a@ and returns a 'Clash.Signal.Signal' of 'Bool'
1754+
(.<) :: (Ord a, Functor f) => f a -> a -> f Bool
1755+
(.<) a b = fmap (<b) a
1756+
16361757
infix 4 .<=.
16371758
-- | The above type is a generalization for:
16381759
--
@@ -1644,6 +1765,30 @@ infix 4 .<=.
16441765
(.<=.) :: (Ord a, Applicative f) => f a -> f a -> f Bool
16451766
(.<=.) = liftA2 (<=)
16461767

1768+
infix 4 .<=
1769+
-- | The above type is a generalization for:
1770+
--
1771+
-- @
1772+
-- __(.<=)__ :: 'Ord' a => 'Clash.Signal.Signal' a -> a -> 'Clash.Signal.Signal' 'Bool'
1773+
-- @
1774+
--
1775+
-- It is a version of ('GHC.TypeNats.<=') that allows comparing a @'Clash.Signal.Signal' a@ with a constant
1776+
-- @a@ and returns a 'Clash.Signal.Signal' of 'Bool'
1777+
(.<=) :: (Ord a, Functor f) => f a -> a -> f Bool
1778+
(.<=) a b = fmap (<=b) a
1779+
1780+
infix 4 <=.
1781+
-- | The above type is a generalization for:
1782+
--
1783+
-- @
1784+
-- __(<=.)__ :: 'Ord' a => a -> 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' 'Bool'
1785+
-- @
1786+
--
1787+
-- It is a version of ('GHC.TypeNats.<=') that allows comparing a @'Clash.Signal.Signal' a@ with a constant
1788+
-- @a@ and returns a 'Clash.Signal.Signal' of 'Bool'
1789+
(<=.) :: (Ord a, Functor f) => a -> f a -> f Bool
1790+
(<=.) a b = fmap (a<=)b
1791+
16471792
infix 4 .>.
16481793
-- | The above type is a generalization for:
16491794
--
@@ -1655,6 +1800,30 @@ infix 4 .>.
16551800
(.>.) :: (Ord a, Applicative f) => f a -> f a -> f Bool
16561801
(.>.) = liftA2 (>)
16571802

1803+
infix 4 .>
1804+
-- | The above type is a generalization for:
1805+
--
1806+
-- @
1807+
-- __(.>)__ :: 'Ord' a => 'Clash.Signal.Signal' a -> a -> 'Clash.Signal.Signal' 'Bool'
1808+
-- @
1809+
--
1810+
-- It is a version of ('>') that allows comparing a @'Clash.Signal.Signal' a@ with a constant
1811+
-- @a@ and returns a 'Clash.Signal.Signal' of 'Bool'
1812+
(.>) :: (Ord a, Functor f) => f a -> a -> f Bool
1813+
(.>) a b = fmap (>b) a
1814+
1815+
infix 4 >.
1816+
-- | The above type is a generalization for:
1817+
--
1818+
-- @
1819+
-- __(>.)__ :: 'Ord' a => a -> 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' 'Bool'
1820+
-- @
1821+
--
1822+
-- It is a version of ('>') that allows comparing a @'Clash.Signal.Signal' a@ with a constant
1823+
-- @a@ and returns a 'Clash.Signal.Signal' of 'Bool'
1824+
(>.) :: (Ord a, Functor f) => a -> f a -> f Bool
1825+
(>.) a b = fmap (a>) b
1826+
16581827
infix 4 .>=.
16591828
-- | The above type is a generalization for:
16601829
--
@@ -1666,6 +1835,30 @@ infix 4 .>=.
16661835
(.>=.) :: (Ord a, Applicative f) => f a -> f a -> f Bool
16671836
(.>=.) = liftA2 (>=)
16681837

1838+
infix 4 .>=
1839+
-- | The above type is a generalization for:
1840+
--
1841+
-- @
1842+
-- __(.>=)__ :: 'Ord' a => 'Clash.Signal.Signal' a -> a -> 'Clash.Signal.Signal' 'Bool'
1843+
-- @
1844+
--
1845+
-- It is a version of ('>=') that allows comparing a @'Clash.Signal.Signal' a@ with a constant
1846+
-- @a@ and returns a 'Clash.Signal.Signal' of 'Bool'
1847+
(.>=) :: (Ord a, Functor f) => f a -> a -> f Bool
1848+
(.>=) a b = fmap (>=b) a
1849+
1850+
infix 4 >=.
1851+
-- | The above type is a generalization for:
1852+
--
1853+
-- @
1854+
-- __(>=.)__ :: 'Ord' a => a -> 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' 'Bool'
1855+
-- @
1856+
--
1857+
-- It is a version of ('>=') that allows comparing a @'Clash.Signal.Signal' a@ with a constant
1858+
-- @a@ and returns a 'Clash.Signal.Signal' of 'Bool'
1859+
(>=.) :: (Ord a, Functor f) => a -> f a -> f Bool
1860+
(>=.) a b = fmap (a>=) b
1861+
16691862
instance Fractional a => Fractional (Signal dom a) where
16701863
(/) = liftA2 (/)
16711864
recip = fmap recip

0 commit comments

Comments
 (0)