newtype

Does F# have 'newtype' of Haskell?

拥有回忆 提交于 2019-12-20 14:45:11
问题 New Library: XParsec This question has lead to a stream-type-independent parsec implementation in F# 3.0 - inspired by FParsec, freed from CharStreams and simplified: http://corsis.github.com/XParsec/ In an FParsec-inspired stream-type-independent simple parsec implementation, I wonder how I could distinguish the following at the type level: parsers that consume a piece of stream parsers that work on the current position without moving ahead in the stream Specifically, how can I restrict in F

What do user-defined value classes look like from Java?

江枫思渺然 提交于 2019-12-19 16:52:09
问题 I think I understand the new "value class" feature of Scala 2.10, by comparison with Haskell's newtype : trait BoundedValue[+This] extends Any { this: This => def upperBound: This def lowerBound: This } class Probability @throws(classOf[IllegalArgumentException]) (v: Double) extends AnyVal with BoundedValue[Probability] { val value: Double = if ((v >= 0.0) && (v <= 1.0)) v else throw new IllegalArgumentException((v.toString) + "is not within the range [0.0, 1.0]") override val upperBound:

What do user-defined value classes look like from Java?

依然范特西╮ 提交于 2019-12-19 16:52:09
问题 I think I understand the new "value class" feature of Scala 2.10, by comparison with Haskell's newtype : trait BoundedValue[+This] extends Any { this: This => def upperBound: This def lowerBound: This } class Probability @throws(classOf[IllegalArgumentException]) (v: Double) extends AnyVal with BoundedValue[Probability] { val value: Double = if ((v >= 0.0) && (v <= 1.0)) v else throw new IllegalArgumentException((v.toString) + "is not within the range [0.0, 1.0]") override val upperBound:

Can a typeclass constraint be used in a newtype definition?

本小妞迷上赌 提交于 2019-12-12 13:56:07
问题 Suppose we have the following newtype definition: newtype A = A { _run :: Monad m => A -> [Int] -> m Int } This does not compile with GHC 8.0.2: error: Not in scope: type variable ‘m’ Replacing m with a concrete typeclass like IO or [] does compile, as I would expect. Given that this is ok, why does GHC not allow the signature above? What is wrong with adding a typeclass constraint inside of this newtype ? 回答1: This is possible: {-# LANGUAGE RankNTypes #-} newtype A = A { _run :: forall m.

Laziness/strictness between data and newtype

瘦欲@ 提交于 2019-12-12 07:32:02
问题 I'm struggling to understand why these two snippets produce different results under the so-called "poor man's strictness analysis". The first example uses data (assuming a correct Applicative instance): data Parser t a = Parser { getParser :: [t] -> Maybe ([t], a) } > getParser (pure (,) <*> literal ';' <*> undefined ) "abc" *** Exception: Prelude.undefined The second uses newtype . There is no other difference: newtype Parser t a = Parser { getParser :: [t] -> Maybe ([t], a) } > getParser

data family instances: newtype, data

巧了我就是萌 提交于 2019-12-11 16:59:12
问题 With Haskell 98 decls, the whole datatype must be either newtype or data . But data families can have a mix of newtype instance , data instance . Does that mean that being newtype is a property of the data constructor rather than the datatype? Could there be a Haskell with something like: data Foo a = MkFoo1 Int a | MkFoo2 Bool a | newtype MkFoo3 a I know I can't write the following, but why/what goes wrong?: data family Bar a newtype instance Bar (Maybe Int) = MkBar1 (Maybe Int) -- MkBar1 ::

Why isn’t this newtype being given the right Read instance?

不打扰是莪最后的温柔 提交于 2019-12-08 01:04:10
问题 I created a newtype alias of the IP type from Data.IP : {-# LANGUAGE GeneralizedNewtypeDeriving #-} module IPAddress (IPAddress) where import Data.IP (IP) import Database.PostgreSQL.Simple.ToField newtype IPAddress = IPAddress IP deriving (Read, Show) instance ToField IPAddress where toField ip = toField $ show ip (I wanted to make it an instance of ToField without creating an orphan instance.) The new type doesn’t seem to support Read in the way it should, though. In this GHCi transcript,

GeneralizedNewtypeDeriving fails for PersistFieldSql

女生的网名这么多〃 提交于 2019-12-07 04:05:53
问题 I'm trying to define a Markdown newtype, and using GeneralizedNewtypeDeriving to automatically define new instances: import Text.Markdown import Yesod.Text.Markdown import Database.Persist.Sql newtype MarkdownNewT = MarkdownNewT { getMarkdown :: Markdown } deriving (Eq, IsString, Monoid, PersistField, PersistFieldSql) This fails for the PersistFieldSql with the following message: Could not coerce from ‘m Markdown’ to ‘m MarkdownNewT’ because ‘m Markdown’ and ‘m MarkdownNewT’ are different

Why isn’t this newtype being given the right Read instance?

末鹿安然 提交于 2019-12-06 09:23:46
I created a newtype alias of the IP type from Data.IP : {-# LANGUAGE GeneralizedNewtypeDeriving #-} module IPAddress (IPAddress) where import Data.IP (IP) import Database.PostgreSQL.Simple.ToField newtype IPAddress = IPAddress IP deriving (Read, Show) instance ToField IPAddress where toField ip = toField $ show ip (I wanted to make it an instance of ToField without creating an orphan instance.) The new type doesn’t seem to support Read in the way it should, though. In this GHCi transcript, you can see that the given string can be interpreted as an IP but not as an IPAddress : *Main IPAddress>

Even more generalized newtype deriving

纵饮孤独 提交于 2019-12-05 20:12:39
问题 Newtypes are often used to change the behavior of certain types when used in certain class contexts. For example, one would use the Data.Monoid.All wrapper to change the behavior of Bool when used as a Monoid . I'm currently writing such a newtype wrapper that would apply to a large range of different types. The wrapper is supposed to change the behavior of one specific class instance. It might look like this: newtype Wrapper a = Wrapper a instance Special a => Special (Wrapper a) where -- ..