質問

instance Monad (Either a) where
     return = Left
     fail = Right
     Left x >>= f = f x
     Right x >>= _ = Right x

「baby.hs」のこのコードフラグは、恐ろしい編集エラーを引き起こしました。

Prelude> :l baby
[1 of 1] Compiling Main             ( baby.hs, interpreted )

baby.hs:2:18:
Couldn't match expected type `a1' against inferred type `a'
  `a1' is a rigid type variable bound by
       the type signature for `return' at <no location info>
  `a' is a rigid type variable bound by
      the instance declaration at baby.hs:1:23
In the expression: Left
In the definition of `return': return = Left
In the instance declaration for `Monad (Either a)'

baby.hs:3:16:
Couldn't match expected type `[Char]' against inferred type `a1'
  `a1' is a rigid type variable bound by
       the type signature for `fail' at <no location info>
  Expected type: String
  Inferred type: a1
In the expression: Right
In the definition of `fail': fail = Right

baby.hs:4:26:
Couldn't match expected type `a1' against inferred type `a'
  `a1' is a rigid type variable bound by
       the type signature for `>>=' at <no location info>
  `a' is a rigid type variable bound by
      the instance declaration at baby.hs:1:23
In the first argument of `f', namely `x'
In the expression: f x
In the definition of `>>=': Left x >>= f = f x

baby.hs:5:31:
Couldn't match expected type `b' against inferred type `a'
  `b' is a rigid type variable bound by
      the type signature for `>>=' at <no location info>
  `a' is a rigid type variable bound by
      the instance declaration at baby.hs:1:23
In the first argument of `Right', namely `x'
In the expression: Right x
In the definition of `>>=': Right x >>= _ = Right x
Failed, modules loaded: none.

なぜこれが起こるのですか?そして、このコードをコンパイルするにはどうすればよいですか?助けてくれてありがとう〜

分かりました。コードを調整してコンパイルを確認しました。

instance Monad (Either a) where
     return = Right
     Left a >>= f = Left a
     Right x >>= f = f x

それは正常にコンパイルされます!しかし...さらに多くの質問について:

instance Monad (Either a)

「a 'a' a 'a' a a 'and i sull' return = right '...どうすれば「return = left」を取得できますか?私はこれを試しましたが、失敗しました:

instance Monad (`Either` a) where
     return = Left
     Right a >>= f = Right a
     Left x >>= f = f x

または:インスタンスモナド( x->のxa)

まったくコンパイルしません!

役に立ちましたか?

解決

  1. 返品にはタイプが必要です forall b. b -> Either a b, しかし、左にはタイプがあります forall c. a -> Either a c. 。あなたはおそらくここに欲しいです。
  2. fail タイプが必要です forall b. String -> Either a b, しかし、右にはタイプがあります forall b. b -> Either a b, 、だから b=String それは作ります String -> Either a String 適合しません。
  3. >>= タイプが必要です Either a b -> (b -> Either a c) -> Either a c でも Right x >>= _ = Right x 常にタイプの値を返します Either a b, 、 いいえ Either a c.
  4. Left x >>= f = f x xにはタイプがあるため、機能しません a, 、 しかし f タイプがあります b -> c.

他のヒント

混乱のほとんどは、左右の事実に起因しています。リターンのタイプのみを考慮すると、Monad Typeclassからのタイプは次のとおりです。

return :: (Monad m) => b -> m b

インスタンスを定義しようとしています m = Either a, 、したがって、returnはタイプを持っている必要があります:

return :: b -> Either a b

あなたはそれを左に定義しています。タイプがあります。

Left :: a -> Either a b

の左側がどのようにあるかに注意してください -> 異なる。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top