小编Nio*_*ium的帖子

IO monad可以防止嵌入式mapM的短路?

以下代码有点神秘.在非玩具版本的问题中,我试图在monad Result中进行monadic计算,其值只能在IO中构造.似乎IO背后的魔力使这样的计算严格,但我无法弄清楚究竟是怎么发生的.

代码:

data Result a = Result a | Failure deriving (Show)

instance Functor Result where
  fmap f (Result a) = Result (f a)
  fmap f Failure = Failure

instance Applicative Result where
  pure = return
  (<*>) = ap

instance Monad Result where
  return = Result
  Result a >>= f = f a
  Failure >>= _ = Failure

compute :: Int -> Result Int
compute 3 = Failure
compute x = traceShow x $ Result x

compute2 :: Monad …
Run Code Online (Sandbox Code Playgroud)

haskell traversal lazy-evaluation strictness io-monad

11
推荐指数
1
解决办法
194
查看次数

惯用布尔等式用法(单例)

我想创建一个数据结构来存储使用Symbol标记在类型级别的项目.这个:

data Store e (ss :: [Symbol]) where
  Nil :: Store e '[]
  Cons :: e s -> Store e ss -> Store e (s ': ss)

data HasElem (a :: k) (as :: [k]) where
  AtHead :: HasElem a (a ': as)
  InTail :: HasElem a as -> HasElem a (b ': as)

class HasElemC (a :: k) (as :: [k]) where hasElem :: HasElem a as
instance HasElemC {OVERLAPPING} a (a ': as) where hasElem = AtHead
instance …
Run Code Online (Sandbox Code Playgroud)

haskell dependent-type singleton-type

9
推荐指数
1
解决办法
264
查看次数

剥离newtype构造函数

我经常编写正在剥离新类型的唯一构造函数的函数,例如在以下函数中返回第一个不是Nothing的参数:

process (Pick xs) = (\(First x) -> x) . mconcat . map (First . process) $ xs
Run Code Online (Sandbox Code Playgroud)

我认为lambda是不必要的冗长.我想写这样的东西:

process (Pick xs) = -First . mconcat . map (First . process) $ xs
Run Code Online (Sandbox Code Playgroud)

Haskell的元编程工具是否允许类似的东西?以更简洁的方式解决这个问题的任何其他解决方案也是受欢迎的.

UPD.已经要求整个代码:

data Node where
  Join :: [Node] -> Node
  Pick :: [Node] -> Node
  Given :: Maybe String -> Node
  Name :: String -> Node

process :: Node -> Maybe String
process (Join xs) = liftM os_path_join (mapM process xs)
process (Pick xs) = getFirst . …
Run Code Online (Sandbox Code Playgroud)

constructor haskell strip syntactic-sugar newtype

7
推荐指数
2
解决办法
424
查看次数

在类型级别验证

假设我想在没有外部工具(如LiquidHaskell)的帮助下构建满足某些不变量的子类型(理想情况下,即使没有类型类,我也希望这样做).最优雅的方式是什么?到目前为止我尝试了以下内容:

class Validated a where
  type Underlying a
  validate :: Underlying a -> Bool
  construct :: Underlying a -> a
  use :: a -> Underlying a

makeValidated :: Validated a => Underlying a -> Maybe a
makeValidated u = if validate u 
                    then Just (construct u)
                    else Nothing


newtype Name = Name String
instance Validated Name where
  type Underlying Name = String
  validate str = and  [ isUppercase (str !! 0 )
                      , all isLetter str ]
  construct = Name …
Run Code Online (Sandbox Code Playgroud)

validation haskell type-families

6
推荐指数
1
解决办法
126
查看次数

强制和存在主义

data T t where
  A :: Show (t a) => t a -> T t
  B :: Coercible Int (t a) => t a -> T t

f :: T t -> String
f (A t) = show t

g :: T t -> Int
g (B t) = coerce t
Run Code Online (Sandbox Code Playgroud)

为什么f编译但g生成如下错误?我正在使用GHC 8.4.

• Couldn't match representation of type ‘Int’ with that of ‘t a’
  Inaccessible code in
    a pattern with constructor:
      B :: forall k …
Run Code Online (Sandbox Code Playgroud)

haskell coercion existential-type gadt

6
推荐指数
1
解决办法
137
查看次数

在 C++03 中作为参数传递的结构的匿名 (?) 初始化

说,我有

struct Foo
{
    char a;
    char b;
};

void bar(Foo foo);
Run Code Online (Sandbox Code Playgroud)

初始化结构并将其传递给函数的最简洁方法是什么?理想情况下,我想写一些类似的东西

bar(Foo = {'a','b'});
Run Code Online (Sandbox Code Playgroud)

如果 Foo 是一个工会呢?

UPD:我真诚地道歉,这个问题应该只与 C++03 相关。此外,在这种特殊情况下,应避免远离 POD(该代码适用于嵌入式系统,因此需要更短的字节码)。vonbrand,感谢 C++11 的回答。

c++ struct anonymous initialization

4
推荐指数
1
解决办法
2403
查看次数