我正在尝试编码一个项目列表,这些项目的类型仅限于某些类型类的实例:
{-# LANGUAGE RankNTypes, TypeSynonymInstances, LiberalTypeSynonyms #-}
module Test where
class Someable a where
some :: a -> String
data Some = Some String
type SomeGroup = forall a. Someable a => [a]
instance Someable Some where
some (Some v) = v
instance Someable SomeGroup where
some (x:xs) = (some x) ++ ", " ++ (some xs)
main = do
putStrLn $ show.some [Some "A", [Some "B", Some "C"]]
Run Code Online (Sandbox Code Playgroud)
但编译失败并出现错误:
Test.hs:14:10:
Illegal polymorphic or qualified type: SomeGroup
In the instance …Run Code Online (Sandbox Code Playgroud) 我想实现以下stripPrefixBy功能:
-- psuedo code signature
stripPrefixBy :: forall a. [forall b. a -> Maybe b] -> [a] -> Maybe [a]
stripPrefixBy [] xs = Just xs
stripPrefixBy _ [] = Nothing
stripPrefixBy (p:ps) (x:xs) = case p x of
Just _ -> stripPrefixBy ps xs
Nothing -> Nothing
res :: Maybe String
res = stripPrefixBy [const (Just 0), Just] "abc"
wantThisToBeTrue :: Bool
wantThisToBeTrue = case res of
Just "c" -> True
_ -> False
Run Code Online (Sandbox Code Playgroud)
我已经尝试使用ImpredicativeTypes和 …
在工作中已经有一些关于将其作为禁止使用unsafePerformIO和相关的部门范围政策的讨论.就个人而言,我并不介意,因为我一直认为,如果我发现自己想要使用它,通常意味着我需要重新思考我的方法.
这种限制听起来合理吗?我似乎记得在某个地方读到它主要是为了FFI,但是我不记得我现在读到的地方.
编辑:好的,那是我的错.它不会受到合理需要的限制,即.FFI.政策的重点更多是为了阻止懒惰和代码味道.
我有大量类型的矢量函数
f :: (M.MVector v r, PrimMonad m) =>
v (PrimState m) r -> v (PrimState m) r -> m ()
Run Code Online (Sandbox Code Playgroud)
这些函数大部分都是就地工作,所以将它们的参数作为一个可变向量很方便,这样我就可以编写,迭代等等.但是,在顶层,我只想使用不可变的"Haskell"/纯向量.
以下是问题的示例:
{-# LANGUAGE TypeFamilies,
ScopedTypeVariables,
MultiParamTypeClasses,
FlexibleInstances #-}
import Data.Vector.Generic as V hiding (eq)
import Data.Vector.Generic.Mutable as M
import Control.Monad.ST
import Control.Monad.Primitive
f :: (M.MVector v r, PrimMonad m) =>
v (PrimState m) r -> v (PrimState m) r -> m ()
f vIn vOut = do val <- M.read vIn 0
M.write vOut 0 val …Run Code Online (Sandbox Code Playgroud) 这是代码:
{-# LANGUAGE RankNTypes, FlexibleContexts, ScopedTypeVariables #-}
module Foo where
import Data.Vector.Generic.Mutable as M
import Data.Vector.Generic as V
import Control.Monad.ST
import Control.Monad.Primitive
import Control.Monad
data DimFun v s r =
DimFun {dim::Int, func :: v (PrimState s) r -> s ()}
runFun :: (Vector v r) =>
(forall s . (PrimMonad s) => DimFun (Mutable v) s r) -> v r -> v r
runFun t x = runST $ do
y <- thaw x
evalFun t y
unsafeFreeze y …Run Code Online (Sandbox Code Playgroud)