1

I need to find the length of a trail in Haskell Diagrams.

I've found the function stdArcLength which seems to give me the length. I have no idea how I would convert this into an Int or a Double.

round and floor throws ambigoues errors. How should I go about converting this (N p) (which I don't what is) to a Double?

EDIT: Example and error.

I have a function called mkPathLength :: Int -> Int -> AnimationAttribute which I first try to call like so mkPathLength (stdArcLength l) 0. This gives the following error

Couldn't match expected type ‘Int’ with actual type ‘N p0’ The type variable ‘p0’ is ambiguous

Understandably, I can't pass it directly as an Int, so I try to round it. That gives me this error

Ambiguous type variable ‘p0’ arising from a use of ‘round’ prevents the constraint ‘(RealFrac (N p0))’ from being solved. Probable fix: use a type annotation to specify what ‘p0’ should be.

Providing a type annotation doesn't seem to work either, but I'm not entirely sure which parts it want me to annonate.

My file has the following extensions:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NegativeLiterals #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}

EDIT v2 Removing "NoMonomorphismRestriction" has solved the issue. I don't even know what it does.

Chris Wohlert
  • 610
  • 3
  • 12
  • 1
    `N p` _is_ typically `Double`! But you may need to fix the type of the trail in order for the compiler to see it. (Diagrams are by default polymorphic with respect to backend etc., but to compute concrete values you need to select a particular one.) – leftaroundabout Feb 20 '21 at 23:23
  • @leftaroundabout I tried simply saying `round $ stdArcLength dia`, but that gives me the ambiguous error. `round $ (stdArcLength dia) :: Double` doesn't work either. – Chris Wohlert Feb 20 '21 at 23:30
  • 2
    `round :: (RealFrac a, Integral b) => a -> b` returns integer values, so it can't return a `Double`, and you probably don't need it. Beyond that, if you're still getting errors we'd need to see the errors themselves, a minimal example for reproducing them, as well as which (if any) GHC extensions are turned on in your file. – duplode Feb 20 '21 at 23:39
  • @duplode I have provided some more context, though my issue is now fixed. Thank you for making me look at my extensions / surrounding context. – Chris Wohlert Feb 21 '21 at 10:06
  • It feels surprising that turning [the monomorphism restriction](https://stackoverflow.com/q/32496864/) *on* would eliminate an ambiguity error. I tried to approximate your issue in [this gist](https://gist.github.com/duplode/c6906714a1f535c803e0d756cefc9f98); however, everything works fine there even with the restriction off if I give the `TrailLike` a sufficiently specialised type signature, and if I leave the signature out I get different errors. If you still want to investigate this, please provide a minimal runnable example of the error to make sure we are looking at the same things. – duplode Feb 21 '21 at 13:57

0 Answers0