Skip to content

Constraint synonym for any valid back end? #108

@kindaro

Description

@kindaro

So I am learning diagrams for like 2 days. At first it was smooth sailing, but then I decided to
split some diagram definitions out from the Main module. This is when I met type errors like
these:

src/Draw.hs:10:74: error:
    • Couldn't match type ‘V t0’ with ‘V2’
        arising from a use of ‘circle’
      The type variable ‘t0’ is ambiguous
    • In the expression: circle 0.1
      In the expression:
        let r = (sqrt $ fromIntegral $ Graph.size graph :: Double)
        in circle 0.1
      In an equation for ‘node’:
          node
            = let r = (sqrt $ fromIntegral $ Graph.size graph :: Double)
              in circle 0.1
    • Relevant bindings include node :: t0 (bound at src/Draw.hs:10:5)

 

src/Draw.hs:1:1: error:
    solveWanteds: too many iterations (limit = 4)
      Unsolved: WC {wc_simple =
                      [W] $dTrailLike_a27Vn {0}:: TrailLike
                                                    (QDiagram backEnd s0 s1 Any) (CDictCan)
                      [D] _ {2}:: base-4.12.0.0:Data.Typeable.Internal.Typeable
                                    s0 (CDictCan)
                      [D] _ {2}:: RealFloat s0 (CDictCan(psc))
                      [D] _ {1}:: Renderable (Path V2 s0) backEnd (CDictCan(psc))
                      [WD] $dFloating_a27Vo {2}:: Floating s0 (CDictCan)
                      [WD] $dOrd_a27Vp {2}:: Ord s0 (CDictCan)
                      [W] $dMetric_a27Vq {1}:: Metric s0 (CDictCan)
                      [WD] $dFractional_a27Vr {0}:: Fractional s0 (CDictCan)
                      [D] _ {0}:: Num s0 (CDictCan)
                      [D] _ {2}:: Eq s0 (CDictCan)
                      [D] _ {2}:: RealFrac s0 (CDictCan)
                      [D] _ {2}:: Real s0 (CDictCan)
                      [WD] hole{co_a27Vk} {8}:: s0 ~ V2 (CNonCanonical)}
      Set limit with -fconstraint-solver-iterations=n; n=0 for no limit

 

I would probably be struggling with them for a lot longer, have I not found this awesome Stack
Overflow answer
from Daniel @bergey. In short, there is defined a type synonym like this:

type Back b = (V b ~ V2, N b ~ Double,
    Renderable (Path V2 Double) b, Renderable (Text Double) b)

dia2 :: Back b => Diagram b
dia2 = circle 1 # fc blue

I tried it and it actually solves all those type errors!

I understand that this type synonym only covers 2D back ends. But is it not nevertheless awesome?

I propose that type synonyms like this are included in Diagrams.Prelude and written about in the
awesome official manual.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions