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.
Related
I'm encountering the following hard to understand behavior from GHC-8.2.2
I have some overlapping typeclass instances. No incoherent typeclass instances. There's a certain typeclass instance of the form, roughly,
instance (C f h, C g h) => D1 (D2 f g) h
where C has overlapping instances. When I load my project into stack repl, all is well. The context of this instance is resolved to the instances of C I'm looking for.
If I create a file test.hs which imports a datatype falling under the instance above, all is not well. When I load that into stack repl, and call an operation of D1, then it's clear that the context of the instance of D1 is being resolved to the "wrong" instance of C.
What's especially strange is that if I add test.hs to my project as an exposed module, then reload it into the repl with no other changes, then the context of the instance above is resolved to the "right" instance of C.
I do not see this behavior with GHC-7.10.3, 8.0.1, 8.0.2, or with 8.4.3. Perhaps this is related to this bug?
But I'm not using incoherent instances, which is what that bug seems to involve. I am using a fair number of language extensions in the module where the instance above occurs:
{-#LANGUAGE TypeFamilies, UndecidableInstances, FlexibleInstances, MultiParamTypeClasses, FunctionalDependencies, GADTs, DataKinds, PolyKinds, TypeOperators, PatternSynonyms, RankNTypes, FlexibleContexts, ScopedTypeVariables, DefaultSignatures #-}
I don't yet have a minimal example. A minimal example of the behavior can be found at GHC-Repro. Run test.sh to see the phenomenon. What I would like to know is:
Whether this might conceivably be intended behavior by GHC, and I'm just doing something wrong.
If I am doing something wrong, what I might do to select the "right" instance when importing stuff from my project into other projects.
This issue is now being tracked at: https://ghc.haskell.org/trac/ghc/ticket/15599
In Yesod, I want to define a new data type:
data Status = Read | Reviewed | Learned
I'm using the Scaffold example. So in the best practice where should I declare the above data? In the Foundation.hs or Application.hs or elsewhere?
I will then create a database table with one of the column as this Status type.
How is this mapped to my Postgresql backend? Which sql data type should correspond to this Status type?
So in the best practice where should I declare the above data? In the Foundation.hs or Application.hs or elsewhere?
I define it neither of the places. I usually create a new module for it and define the type there. But ultimately it boils down to personal taste. I don't recommend it doing it in Foundation.hs because that's a module where your master application type and it's instances for various Yesod related typeclasses reside on. Similary I would not add it in Application.hs because that's a module where your application's setting and the Wai Application related functions reside on. But that's just my taste. :-)
I will then create a database table with one of the column as this Status type. How is this mapped to my Postgresql backend? Which sql data type should correspond to this Status type?
You can use Status algebric type to be define as it is. An example:
#!/usr/bin/env stack
{- stack
--resolver lts-6.19
--install-ghc
runghc
--package persistent
--package aeson
--package persistent-postgresql
--package text
--package persistent-template
--package time
--package mtl
-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleInstances#-}
{-# LANGUAGE TypeFamilies #-}
import Database.Persist
import Database.Persist.Postgresql
import Database.Persist.TH
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Logger (runStderrLoggingT)
import Data.Time
import Data.Text
import Data.Aeson
import ModelSum
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
User
name Text
age Int
status Status
deriving Show
|]
connStr = "host=localhost dbname=test user=postgres password=postgres port=5432"
main :: IO ()
main = mockMigration migrateAll
And the ModelSum file:
{-# LANGUAGE TemplateHaskell #-}
module ModelSum where
import Database.Persist.TH
data Status
= Read
| Reviewed
| Learned
deriving (Show, Eq, Read)
derivePersistField "Status"
On executing it, you get:
$ ./script.hs
CREATe TABLE "user"("id" SERIAL8 PRIMARY KEY UNIQUE,"name" VARCHAR NOT NULL,"age" INT8 NOT NULL,"status" VARCHAR NOT NULL)
You can see that the status column is created as
varchar. Internally it performs the conversion using the Show and
Read instances.
I saw a Haskell source code, and at the beginning of the source file, it included several things like:
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DeriveFoldable #-}
I know that comments in Haskell stat with {- and end with -}, but this is clearly something else. What's the purpose of this? It seems pretty similar like include statements or macros in C.
The purpose is to enable language extensions. It's a compiler pragma. The GHC compiler supports a lot of language extensions. The GHC manual provides explanation of each extension and examples.
It seems that data types promoted by DataKinds do not automatically get typeable instances. It is quite easy solve this by using StandaloneDeriving but this creates orphan instances. Is there anyway to avoid this?
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
module Orphan where
import Data.Typeable
deriving instance Typeable '[]
deriving instance Typeable '(:)
On compilation..
[1 of 1] Compiling Orphan ( orphan.hs, interpreted )
orphan.hs:10:1: Warning: Orphan instance: instance Typeable '[]
orphan.hs:11:1: Warning: Orphan instance: instance Typeable (':)
Ok, modules loaded: Orphan.
*Orphan>
I'm getting an error that Constraint is not in scope, when I try to write a simple example,
{-# LANGUAGE UndecidableInstances,
MultiParamTypeClasses,
KindSignatures,
Rank2Types,
ConstraintKinds,
FlexibleInstances,
OverlappingInstances,
TypeFamilies #-}
type family A a :: Constraint
The error is,
> ghci test.hs
[1 of 1] Compiling Main ( test.hs, interpreted )
test.hs:10:20: Not in scope: type constructor or class `Constraint'
Failed, modules loaded: none.
Constraint synonyms seem to work as expected,
type ShowOrd a = (Ord a, Show a)
Thanks in advance.
Your error appears to be caused by Constraint being, unsurprisingly, not in scope. You'll want to import it from GHC.Prim or from GHC.Exts.
It seems a bit odd that enabling ConstraintKinds doesn't bring it into scope automatically, as it does in a 7.3.2 snapshot build I had lying around, but there you go.