Skip to content

Commit

Permalink
Doubli, working around ku-fpg/hermit#173
Browse files Browse the repository at this point in the history
  • Loading branch information
conal committed Feb 18, 2016
1 parent 04aa786 commit 3ba151c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
1 change: 1 addition & 0 deletions circat.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Library
Circat.Shift
Circat.Mealy
Circat.Scan
Circat.Doubli
Circat.Complex
Circat.FFT
Circat.Circuit
Expand Down
18 changes: 13 additions & 5 deletions src/Circat/Circuit.hs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ import TypeUnary.Vec hiding (get)

-- TODO: Eliminate most of the following, as I move data types out of circat
import Circat.Misc (Unit,(:*),(<~),Unop,Binop)
import Circat.Doubli
import Circat.Complex
import Circat.Category
import Circat.Classes
Expand Down Expand Up @@ -207,7 +208,7 @@ data Buses :: * -> * where
BoolB :: Source -> Buses Bool
IntB :: Source -> Buses Int
FloatB :: Source -> Buses Float
DoubleB :: Source -> Buses Double
DoubleB :: Source -> Buses Doubli
PairB :: Buses a -> Buses b -> Buses (a :* b)
FunB :: (a :> b) -> Buses (a -> b)
IsoB :: Buses (Rep a) -> Buses a
Expand Down Expand Up @@ -298,7 +299,7 @@ instance GenBuses Float where
delay = primDelay
ty = const FloatT

instance GenBuses Double where
instance GenBuses Doubli where
genBuses' = genBus DoubleB 64
delay = primDelay
ty = const DoubleT
Expand Down Expand Up @@ -334,9 +335,15 @@ isoErr nm = error (nm ++ ": IsoB")
pairB :: Buses a :* Buses b -> Buses (a :* b)
pairB (a,b) = PairB a b

-- Workaround for "spurious non-exhaustive warning with GADT and newtypes"
-- <https://ghc.haskell.org/trac/ghc/ticket/6124>.
#define BogusMatch(name) name _ = error "BogusMatch"
#define BogusAlt _ -> error "BogusMatch"

unUnitB :: Buses Unit -> Unit
unUnitB UnitB = ()
unUnitB (IsoB _) = isoErr "unUnitB"
BogusMatch(unUnitB)

unPairB :: Buses (a :* b) -> Buses a :* Buses b
#if 0
Expand All @@ -346,14 +353,14 @@ unPairB (IsoB _) = isoErr "unPairB"
-- Lazier
unPairB w = (a,b)
where

a = case w of
PairB p _ -> p
IsoB _ -> isoErr "unPairB"
BogusAlt
b = case w of
PairB _ q -> q
IsoB _ -> isoErr "unPairB"

BogusAlt
-- (a,b) = case w of
-- PairB p q -> (p,q)
-- IsoB _ -> isoErr "unPairB"
Expand All @@ -363,6 +370,7 @@ unPairB w = (a,b)
unFunB :: Buses (a -> b) -> (a :> b)
unFunB (FunB circ) = circ
unFunB (IsoB _) = isoErr "unFunB"
BogusMatch(unFunB)

exlB :: Buses (a :* b) -> Buses a
exlB = fst . unPairB
Expand Down Expand Up @@ -801,7 +809,7 @@ class SourceToBuses a where toBuses :: Source -> Buses a
instance SourceToBuses Bool where toBuses = BoolB
instance SourceToBuses Int where toBuses = IntB
instance SourceToBuses Float where toBuses = FloatB
instance SourceToBuses Double where toBuses = DoubleB
instance SourceToBuses Doubli where toBuses = DoubleB

sourceB :: SourceToBuses a => Source -> CircuitM (Maybe (Buses a))
sourceB = justA . toBuses
Expand Down
24 changes: 24 additions & 0 deletions src/Circat/Doubli.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# OPTIONS_GHC -Wall #-}

-- {-# OPTIONS_GHC -fno-warn-unused-imports #-} -- TEMP
-- {-# OPTIONS_GHC -fno-warn-unused-binds #-} -- TEMP

----------------------------------------------------------------------
-- |
-- Module : Circat.Doubli
-- Copyright : (c) 2016 Conal Elliott
-- License : BSD3
--
-- Maintainer : [email protected]
-- Stability : experimental
--
-- newtype wrapper around Double to work around a problem finding instances
-- defined in GHC.Float.
----------------------------------------------------------------------

module Circat.Doubli where

newtype Doubli = Doubli Double
deriving (Enum,Eq,Floating,Fractional,Num,Ord,Read,Real,RealFloat,RealFrac,Show)

5 changes: 3 additions & 2 deletions src/Circat/Prim.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import Circat.Circuit (GenBuses,(:>)
import Circat.Misc
import Circat.ShowUtils (Show'(..))
import Circat.Show (showsApp1)
import Circat.Doubli

{--------------------------------------------------------------------
Literals
Expand All @@ -70,7 +71,7 @@ data Lit :: * -> * where
BoolL :: Bool -> Lit Bool
IntL :: Int -> Lit Int
FloatL :: Float -> Lit Float
DoubleL :: Double -> Lit Double
DoubleL :: Doubli -> Lit Doubli

-- The Unit argument is just for uniformity

Expand All @@ -91,7 +92,7 @@ instance HasLit Unit where toLit = UnitL
instance HasLit Bool where toLit = BoolL
instance HasLit Int where toLit = IntL
instance HasLit Float where toLit = FloatL
instance HasLit Double where toLit = DoubleL
instance HasLit Doubli where toLit = DoubleL

-- TODO: Do I still need this stuff?

Expand Down

0 comments on commit 3ba151c

Please sign in to comment.