Skip to content

Commit

Permalink
Add a bunch of Typeable n constraints
Browse files Browse the repository at this point in the history
Needed by the change to the `Action` class to require `Semigroup` for
its first parameter.
  • Loading branch information
byorgey committed Nov 4, 2023
1 parent d6347e8 commit f187b49
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/Diagrams/Layout/Wrap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import qualified Data.Foldable as F
import Data.List (find, inits, tails)
import Diagrams.Prelude hiding (start)
import Linear.Epsilon
import Data.Typeable

-- TODO: Take into account the negative bounds, and iteratively refine
-- the list selection.
Expand All @@ -30,7 +31,7 @@ import Linear.Epsilon
-- | @wrapDiagram@ post-processes the results of @wrapOutside@ /
-- @wrapInside@ into a Diagram of the result. This only works when
-- applying them to a list of diagrams.
wrapDiagram :: (Metric v, OrderedField n)
wrapDiagram :: (Metric v, OrderedField n, Typeable n)
=> ([(v n, QDiagram b v n Any)], [QDiagram b v n Any]) -> QDiagram b v n Any
wrapDiagram = F.foldMap (uncurry translate) . fst

Expand Down
4 changes: 2 additions & 2 deletions src/Diagrams/TwoD/Grid.hs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ makeLenses ''GridOpts
makeLenses ''HighlightLineOpts

-- | Name a point by grid co-ordinates.
tick :: (Floating n, Ord n)
tick :: (Floating n, Ord n, Typeable n)
=> (Int, Int) -> QDiagram b V2 n Any
tick (n, m) = pointDiagram origin # named (n, m)

Expand Down Expand Up @@ -212,7 +212,7 @@ gridWithHalves' opts n m =

-- | Place a diagram on a grid (which is itself a diagram) at all the
-- co-ordinates specified.
placeDiagramOnGrid :: (IsName nm, Floating n, Ord n) =>
placeDiagramOnGrid :: (IsName nm, Floating n, Ord n, Typeable n) =>
QDiagram b V2 n Any -> [nm] -> QDiagram b V2 n Any -> QDiagram b V2 n Any
placeDiagramOnGrid d = flip $ foldr (\n -> withName n (atop . place d . location))

Expand Down
12 changes: 8 additions & 4 deletions src/Diagrams/TwoD/Layout/CirclePacking.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module Diagrams.TwoD.Layout.CirclePacking
, circleRadius ) where

import Optimisation.CirclePacking
import Data.Typeable

import Diagrams.Core.Envelope
import Diagrams.Prelude
Expand All @@ -50,7 +51,8 @@ import Diagrams.TwoD.Vector (e)
-- | Combines the passed objects, whose radius is estimated using the given
-- 'RadiusFunction', so that they do not overlap (according to the radius
-- function) and otherwise form, as far as possible, a tight circle.
renderCirclePacking :: (Monoid' m, Floating (N b), Ord (N b)) => RadiusFunction b m -> [QDiagram b V2 (N b) m] -> QDiagram b V2 (N b) m
renderCirclePacking :: (Monoid' m, Floating (N b), Ord (N b), Typeable (N b))
=> RadiusFunction b m -> [QDiagram b V2 (N b) m] -> QDiagram b V2 (N b) m
renderCirclePacking radiusFunc = createCirclePacking radiusFunc id

toFractional :: (Real a, Fractional b) => a -> b
Expand All @@ -59,7 +61,8 @@ toFractional = fromRational . toRational
-- | More general version of 'renderCirclePacking'. You can use this if you
-- have more information available in the values of type @a@ that allows you to
-- calculate the radius better (or even exactly).
createCirclePacking :: (Monoid' m, Ord (N b), Floating (N b)) => (a -> Double) -> (a -> QDiagram b V2 (N b) m) -> [a] -> QDiagram b V2 (N b) m
createCirclePacking :: (Monoid' m, Ord (N b), Floating (N b), Typeable (N b))
=> (a -> Double) -> (a -> QDiagram b V2 (N b) m) -> [a] -> QDiagram b V2 (N b) m
createCirclePacking radiusFunc diagramFunc =
position .
map (\(o,(x,y)) -> (p2 (toFractional x, toFractional y), diagramFunc o)) .
Expand All @@ -73,7 +76,8 @@ type RadiusFunction b m = QDiagram b V2 (N b) m -> Double
-- | A safe approximation. Calculates the outer radius of the smallest
-- axis-aligned polygon with the given number of edges that contains the
-- object. A parameter of 4 up to 8 should be sufficient for most applications.
approxRadius :: (Monoid' m, Floating (N b), Real (N b), Ord (N b)) => Int -> RadiusFunction b m
approxRadius :: (Monoid' m, Floating (N b), Real (N b), Ord (N b), Typeable (N b))
=> Int -> RadiusFunction b m
approxRadius n =
if n < 3
then error "circleRadius: n needs to be at least 3"
Expand All @@ -91,5 +95,5 @@ approxRadius n =
-- fits in the rectangular bounding box of the object, so it may be too small.
-- It is, however, exact for circles, and there is no function that is safe for
-- all diagrams and exact for circles.
circleRadius :: (Monoid' m, Floating (N b), Real (N b)) => RadiusFunction b m
circleRadius :: (Monoid' m, Floating (N b), Real (N b), Typeable (N b)) => RadiusFunction b m
circleRadius o = toFractional $ maximum [ envelopeS (e (alpha @@ turn)) o | alpha <- [0,0.25,0.5,0.75]]
7 changes: 4 additions & 3 deletions src/Diagrams/TwoD/Layout/Constrained.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ import Data.List (sortBy)
import qualified Data.Map as M
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Data.Typeable
import GHC.Generics

import qualified Math.MFSolve as MFS
Expand Down Expand Up @@ -521,7 +522,7 @@ a ==== b = constrain $ MFS.ignore (a MFS.=== b)
-- short, you do not need to know anything about @Located Envelope@s
-- in order to call this function.
constrainWith
:: (Hashable n, RealFrac n, Floating n, Monoid' m)
:: (Hashable n, RealFrac n, Floating n, Typeable n, Monoid' m)
=> -- (forall a. (...) => [a] -> a)
([[Located (Envelope V2 n)]] -> [Located (Envelope V2 n)])
-> [DiaID s]
Expand Down Expand Up @@ -619,15 +620,15 @@ getDiaVars deps d = M.fromList $
-- This is obviously not ideal. A future version may do something
-- more reasonable.
layout
:: (Monoid' m, Hashable n, Floating n, RealFrac n, Show n)
:: (Monoid' m, Hashable n, Floating n, RealFrac n, Show n, Typeable n)
=> (forall s. Constrained s b n m a)
-> QDiagram b V2 n m
layout constr = snd $ runLayout constr

-- | Like 'layout', but also allows the caller to retrieve the result of the
-- 'Constrained' computation.
runLayout
:: (Monoid' m, Hashable n, Floating n, RealFrac n, Show n)
:: (Monoid' m, Hashable n, Floating n, RealFrac n, Show n, Typeable n)
=> (forall s. Constrained s b n m a)
-> (a, QDiagram b V2 n m)
runLayout constr =
Expand Down
5 changes: 3 additions & 2 deletions src/Diagrams/TwoD/Layout/Tree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ import qualified Data.Map as M
import Data.Maybe
import qualified Data.Traversable as T
import Data.Tree
import Data.Typeable

import Control.Lens (makeLenses, view, (+=), (-=), (^.))
import Diagrams
Expand Down Expand Up @@ -618,7 +619,7 @@ decorate' d (Node a ts) = Node (a, info) ts'

-- | Draw a tree annotated with node positions, given functions
-- specifying how to draw nodes and edges.
renderTree :: (Monoid' m, Floating n, Ord n)
renderTree :: (Monoid' m, Floating n, Ord n, Typeable n)
=> (a -> QDiagram b V2 n m) -> (P2 n -> P2 n -> QDiagram b V2 n m)
-> Tree (a, P2 n) -> QDiagram b V2 n m
renderTree n e = renderTree' n (e `on` snd)
Expand All @@ -627,7 +628,7 @@ renderTree n e = renderTree' n (e `on` snd)
-- specifying how to draw nodes and edges. Unlike 'renderTree',
-- this version gives the edge-drawing function access to the actual
-- values stored at the nodes rather than just their positions.
renderTree' :: (Monoid' m, Floating n, Ord n)
renderTree' :: (Monoid' m, Floating n, Ord n, Typeable n)
=> (a -> QDiagram b V2 n m) -> ((a,P2 n) -> (a,P2 n) -> QDiagram b V2 n m)
-> Tree (a, P2 n) -> QDiagram b V2 n m
renderTree' renderNode renderEdge = alignT . centerX . renderTreeR
Expand Down

0 comments on commit f187b49

Please sign in to comment.