Skip to content

Commit

Permalink
add a bunch of Typeable n constraints
Browse files Browse the repository at this point in the history
These extra constraints are necessitated by a change to the `Action`
class in `monoid-extras`, which now requires a `Semigroup` instance
for its first type argument.
  • Loading branch information
byorgey committed Nov 4, 2023
1 parent 0f2eace commit 2ec1667
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/Diagrams/Align.hs
Expand Up @@ -44,6 +44,7 @@ import Diagrams.Util (applyAll)
import Data.Maybe (fromMaybe)
import Data.Ord (comparing)
import Data.Traversable
import Data.Typeable
import Prelude

import qualified Data.Foldable as F
Expand Down Expand Up @@ -115,7 +116,7 @@ instance (V b ~ v, N b ~ n, Metric v, OrderedField n, Alignable b)
=> Alignable (M.Map k b) where
defaultBoundary = combineBoundaries defaultBoundary

instance (Metric v, OrderedField n, Monoid' m)
instance (Metric v, OrderedField n, Typeable n, Monoid' m)
=> Alignable (QDiagram b v n m) where
defaultBoundary = envelopeBoundary

Expand Down
9 changes: 5 additions & 4 deletions src/Diagrams/Animation.hs
Expand Up @@ -32,6 +32,7 @@ module Diagrams.Animation

import Data.Active
import Data.Semigroup
import Data.Typeable

import Diagrams.Core

Expand Down Expand Up @@ -89,15 +90,15 @@ type Animation b v n = QAnimation b v n Any
--
-- See also 'animRect' for help constructing a background to go
-- behind an animation.
animEnvelope :: (OrderedField n, Metric v, Monoid' m)
animEnvelope :: (OrderedField n, Typeable n, Metric v, Monoid' m)
=> QAnimation b v n m -> QAnimation b v n m
animEnvelope = animEnvelope' 30

-- | Like 'animEnvelope', but with an adjustible sample rate. The first
-- parameter is the number of samples per time unit to use. Lower
-- rates will be faster but less accurate; higher rates are more
-- accurate but slower.
animEnvelope' :: (OrderedField n, Metric v, Monoid' m)
animEnvelope' :: (OrderedField n, Typeable n, Metric v, Monoid' m)
=> Rational -> QAnimation b v n m -> QAnimation b v n m
animEnvelope' r a = withEnvelope (simulate r a) <$> a

Expand All @@ -108,15 +109,15 @@ animEnvelope' r a = withEnvelope (simulate r a) <$> a
--
-- Uses 30 samples per time unit by default; to adjust this number
-- see 'animRect''.
animRect :: (InSpace V2 n t, Monoid' m, TrailLike t, Enveloped t, Transformable t, Monoid t)
animRect :: (InSpace V2 n t, Typeable n, Monoid' m, TrailLike t, Enveloped t, Transformable t, Monoid t)
=> QAnimation b V2 n m -> t
animRect = animRect' 30

-- | Like 'animRect', but with an adjustible sample rate. The first
-- parameter is the number of samples per time unit to use. Lower
-- rates will be faster but less accurate; higher rates are more
-- accurate but slower.
animRect' :: (InSpace V2 n t, Monoid' m, TrailLike t, Enveloped t, Transformable t, Monoid t)
animRect' :: (InSpace V2 n t, Typeable n, Monoid' m, TrailLike t, Enveloped t, Transformable t, Monoid t)
=> Rational -> QAnimation b V2 n m -> t
animRect' r anim
| null results = rect 1 1
Expand Down
19 changes: 10 additions & 9 deletions src/Diagrams/Combinators.hs
Expand Up @@ -48,6 +48,7 @@ import Data.Monoid.MList (inj)
import Data.Proxy
import Data.Semigroup
import qualified Data.Tree.DUAL as D
import Data.Typeable

import Diagrams.Core
import Diagrams.Core.Types (QDiagram (QD))
Expand Down Expand Up @@ -77,13 +78,13 @@ import Linear.Vector
-- > )
-- > c = circle 0.8
-- > withEnvelopeEx = sqNewEnv # centerXY # pad 1.5
withEnvelope :: (InSpace v n a, Monoid' m, Enveloped a)
withEnvelope :: (InSpace v n a, Typeable n, Monoid' m, Enveloped a)
=> a -> QDiagram b v n m -> QDiagram b v n m
withEnvelope = setEnvelope . getEnvelope

-- | Use the trace from some object as the trace for a diagram, in
-- place of the diagram's default trace.
withTrace :: (InSpace v n a, Metric v, OrderedField n, Monoid' m, Traced a)
withTrace :: (InSpace v n a, Metric v, OrderedField n, Typeable n, Monoid' m, Traced a)
=> a -> QDiagram b v n m -> QDiagram b v n m
withTrace = setTrace . getTrace

Expand All @@ -98,15 +99,15 @@ phantom a = QD $ D.leafU ((inj . toDeletable . getEnvelope $ a) <> (inj . toDele
-- origin, so if the origin is not centered the padding may appear
-- \"uneven\". If this is not desired, the origin can be centered
-- (using, e.g., 'centerXY' for 2D diagrams) before applying @pad@.
pad :: (Metric v, OrderedField n, Monoid' m)
pad :: (Metric v, OrderedField n, Typeable n, Monoid' m)
=> n -> QDiagram b v n m -> QDiagram b v n m
pad s d = withEnvelope (d # scale s) d

-- | @frame s@ increases the envelope of a diagram by and absolute amount @s@,
-- s is in the local units of the diagram. This function is similar to @pad@,
-- only it takes an absolute quantity and pre-centering should not be
-- necessary.
frame :: (Metric v, OrderedField n, Monoid' m)
frame :: (Metric v, OrderedField n, Typeable n, Monoid' m)
=> n -> QDiagram b v n m -> QDiagram b v n m
frame s = over envelope (onEnvelope $ \f x -> f x + s)

Expand Down Expand Up @@ -143,7 +144,7 @@ strut v = QD $ D.leafU (inj . toDeletable $ env)
-- the cosine of the difference in angle, and leaving it unchanged
-- when this factor is negative.
extrudeEnvelope
:: (Metric v, OrderedField n, Monoid' m)
:: (Metric v, OrderedField n, Typeable n, Monoid' m)
=> v n -> QDiagram b v n m -> QDiagram b v n m
extrudeEnvelope = deformEnvelope 1

Expand All @@ -155,13 +156,13 @@ extrudeEnvelope = deformEnvelope 1
-- Note that this could create strange inverted envelopes, where
-- @ diameter v d < 0 @.
intrudeEnvelope
:: (Metric v, OrderedField n, Monoid' m)
:: (Metric v, OrderedField n, Typeable n, Monoid' m)
=> v n -> QDiagram b v n m -> QDiagram b v n m
intrudeEnvelope = deformEnvelope (-1)

-- Utility for extrudeEnvelope / intrudeEnvelope
deformEnvelope
:: (Metric v, OrderedField n, Monoid' m)
:: (Metric v, OrderedField n, Typeable n, Monoid' m)
=> n -> v n -> QDiagram b v n m -> QDiagram b v n m
deformEnvelope s v = over (envelope . _Wrapping Envelope) deformE
where
Expand All @@ -179,7 +180,7 @@ deformEnvelope s v = over (envelope . _Wrapping Envelope) deformE
-- | @beneath@ is just a convenient synonym for @'flip' 'atop'@; that is,
-- @d1 \`beneath\` d2@ is the diagram with @d2@ superimposed on top of
-- @d1@.
beneath :: (Metric v, OrderedField n, Monoid' m)
beneath :: (Metric v, OrderedField n, Typeable n, Monoid' m)
=> QDiagram b v n m -> QDiagram b v n m -> QDiagram b v n m
beneath = flip atop

Expand Down Expand Up @@ -382,7 +383,7 @@ cat' v (CatOpts { _catMethod = Distrib, _sep = s }) =
-- > # showOrigin
-- > # frame 0.1
composeAligned
:: (Monoid' m, Floating n, Ord n, Metric v)
:: (Monoid' m, Floating n, Ord n, Typeable n, Metric v)
=> (QDiagram b v n m -> QDiagram b v n m) -- ^ Alignment function
-> ([QDiagram b v n m] -> QDiagram b v n m) -- ^ Composition function
-> ([QDiagram b v n m] -> QDiagram b v n m)
Expand Down
7 changes: 3 additions & 4 deletions src/Diagrams/Names.hs
Expand Up @@ -45,22 +45,21 @@ module Diagrams.Names
) where

import Data.Semigroup

import Data.Typeable
import Diagrams.Core (OrderedField, Point)
import Diagrams.Core.Names
import Diagrams.Core.Types

import Linear.Metric

-- | Attach an atomic name to a diagram.
named :: (IsName nm, Metric v, OrderedField n, Semigroup m)
named :: (IsName nm, Metric v, OrderedField n, Typeable n, Semigroup m)
=> nm -> QDiagram b v n m -> QDiagram b v n m
named = nameSub mkSubdiagram

-- | Attach an atomic name to a certain point (which may be computed
-- from the given diagram), treated as a subdiagram with no content
-- and a point envelope.
namePoint :: (IsName nm , Metric v, OrderedField n, Semigroup m)
namePoint :: (IsName nm , Metric v, OrderedField n, Typeable n, Semigroup m)
=> (QDiagram b v n m -> Point v n) -> nm -> QDiagram b v n m -> QDiagram b v n m
namePoint p = nameSub (subPoint . p)

7 changes: 4 additions & 3 deletions src/Diagrams/Trace.hs
Expand Up @@ -36,10 +36,11 @@ module Diagrams.Trace
import Diagrams.Core (OrderedField, Point, Subdiagram, location,
origin, setTrace, trace)
import Diagrams.Core.Trace
import Diagrams.Combinators (withTrace)

import Data.Maybe
import Data.Semigroup
import Diagrams.Combinators (withTrace)
import Data.Typeable

import Linear.Metric
import Linear.Vector
Expand All @@ -48,15 +49,15 @@ import Linear.Vector
-- beginning from the location (local origin) of the subdiagram and
-- moving in the direction of the given vector. If there is no such
-- point, the origin is returned; see also 'boundaryFromMay'.
boundaryFrom :: (OrderedField n, Metric v, Semigroup m)
boundaryFrom :: (OrderedField n, Typeable n, Metric v, Semigroup m)
=> Subdiagram b v n m -> v n -> Point v n
boundaryFrom s v = fromMaybe origin $ boundaryFromMay s v

-- | Compute the furthest point on the boundary of a subdiagram,
-- beginning from the location (local origin) of the subdiagram and
-- moving in the direction of the given vector, or @Nothing@ if
-- there is no such point.
boundaryFromMay :: (Metric v, OrderedField n, Semigroup m)
boundaryFromMay :: (Metric v, OrderedField n, Typeable n, Semigroup m)
=> Subdiagram b v n m -> v n -> Maybe (Point v n)
boundaryFromMay s v = traceP (location s) (negated v) s

17 changes: 9 additions & 8 deletions src/Diagrams/TwoD/Combinators.hs
Expand Up @@ -46,6 +46,7 @@ import Control.Lens ((&), (.~))
import Data.Colour
import Data.Default.Class
import Data.Semigroup
import Data.Typeable

import Diagrams.Core

Expand Down Expand Up @@ -186,7 +187,7 @@ strutY d = strut (zero & _y .~ d)
-- centered horizontally the padding may appear \"uneven\". If this
-- is not desired, the origin can be centered (using 'centerX')
-- before applying @padX@.
padX :: (Metric v, R2 v, OrderedField n, Monoid' m)
padX :: (Metric v, R2 v, OrderedField n, Typeable n, Monoid' m)
=> n -> QDiagram b v n m -> QDiagram b v n m
padX s d = withEnvelope (d # scaleX s) d

Expand All @@ -197,7 +198,7 @@ padX s d = withEnvelope (d # scaleX s) d
-- so if the origin is not centered vertically the padding may appear
-- \"uneven\". If this is not desired, the origin can be centered
-- (using 'centerY') before applying @padY@.
padY :: (Metric v, R2 v, Monoid' m, OrderedField n)
padY :: (Metric v, R2 v, Monoid' m, OrderedField n, Typeable n)
=> n -> QDiagram b v n m -> QDiagram b v n m
padY s d = withEnvelope (d # scaleY s) d

Expand All @@ -206,7 +207,7 @@ padY s d = withEnvelope (d # scaleY s) d
-- the envelope is inset instead.
--
-- See the documentation for 'extrudeEnvelope' for more information.
extrudeLeft :: (OrderedField n, Monoid' m) => n -> QDiagram b V2 n m -> QDiagram b V2 n m
extrudeLeft :: (OrderedField n, Typeable n, Monoid' m) => n -> QDiagram b V2 n m -> QDiagram b V2 n m
extrudeLeft s
| s >= 0 = extrudeEnvelope $ unitX ^* negate s
| otherwise = intrudeEnvelope $ unitX ^* negate s
Expand All @@ -216,7 +217,7 @@ extrudeLeft s
-- the envelope is inset instead.
--
-- See the documentation for 'extrudeEnvelope' for more information.
extrudeRight :: (OrderedField n, Monoid' m) => n -> QDiagram b V2 n m -> QDiagram b V2 n m
extrudeRight :: (OrderedField n, Typeable n, Monoid' m) => n -> QDiagram b V2 n m -> QDiagram b V2 n m
extrudeRight s
| s >= 0 = extrudeEnvelope $ unitX ^* s
| otherwise = intrudeEnvelope $ unitX ^* s
Expand All @@ -226,7 +227,7 @@ extrudeRight s
-- the envelope is inset instead.
--
-- See the documentation for 'extrudeEnvelope' for more information.
extrudeBottom :: (OrderedField n, Monoid' m) => n -> QDiagram b V2 n m -> QDiagram b V2 n m
extrudeBottom :: (OrderedField n, Typeable n, Monoid' m) => n -> QDiagram b V2 n m -> QDiagram b V2 n m
extrudeBottom s
| s >= 0 = extrudeEnvelope $ unitY ^* negate s
| otherwise = intrudeEnvelope $ unitY ^* negate s
Expand All @@ -236,7 +237,7 @@ extrudeBottom s
-- the envelope is inset instead.
--
-- See the documentation for 'extrudeEnvelope' for more information.
extrudeTop :: (OrderedField n, Monoid' m) => n -> QDiagram b V2 n m -> QDiagram b V2 n m
extrudeTop :: (OrderedField n, Typeable n, Monoid' m) => n -> QDiagram b V2 n m -> QDiagram b V2 n m
extrudeTop s
| s >= 0 = extrudeEnvelope $ unitY ^* s
| otherwise = intrudeEnvelope $ unitY ^* s
Expand All @@ -246,12 +247,12 @@ extrudeTop s
-- .+^ v@. Useful for selecting the rectangular portion of a
-- diagram which should actually be \"viewed\" in the final render,
-- if you don't want to see the entire diagram.
rectEnvelope :: forall b n m. (OrderedField n, Monoid' m)
rectEnvelope :: forall b n m. (OrderedField n, Typeable n, Monoid' m)
=> Point V2 n -> V2 n -> QDiagram b V2 n m -> QDiagram b V2 n m
rectEnvelope p (V2 w h) = withEnvelope (rect w h # alignBL # moveTo p :: Path V2 n)

-- | A synonym for 'rectEnvelope'.
crop :: forall b n m. (OrderedField n, Monoid' m)
crop :: forall b n m. (OrderedField n, Typeable n, Monoid' m)
=> Point V2 n -> V2 n -> QDiagram b V2 n m -> QDiagram b V2 n m
crop = rectEnvelope

Expand Down

0 comments on commit 2ec1667

Please sign in to comment.