Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

haskell - 如何编写Continuation Monad的Functor实例?(How to write Functor instance of Continuation Monad?)

newtype Cont k a = Cont { runCont :: (a -> k) -> k }

instance Functor (Cont k) where
  -- fmap :: (a -> b) -> (Cont k a) -> (Cont k b)
  fmap f (Cont akTok) = Cont $ ???

My doubts:

(我的疑问:)

  1. We can only write Functor instance to any data type that can produce a type out (ex: [a], Maybe a, (y -> a)), but not for the data types that consumes a type.

    (我们只能将Functor实例写入任何可以产生类型的数据类型(例如:[a],也许是a,(y-> a)),但不能写入消耗类型的数据类型。)

    Now in the above data type it consumes a function that consumes a then how does this indirect consumption's can be considered as producing a type a .

    (现在,在上述数据键入它消耗了功能消耗那么请问这个间接消耗的可视为生产类型 。)

    That means we cannot write Functor instance for (k -> a) -> k ?

    (这意味着我们不能为(k-> a)-> k编写Functor实例?)

  2. How can I read the Cont data type.

    (如何读取Cont数据类型。)

    Cont produces k when it have a ?

    (产生k当它有一个 ?)

    (Just like Javascript XHR callback produces JSON when it have response from fetching data from server?)

    ((就像Javascript XHR回调在从服务器获取数据做出响应时会生成JSON吗?))

  3. How to write QuickCheck test cases for Cont data type

    (如何为Cont数据类型编写QuickCheck测试用例)

import Test.QuickCheck
import Test.QuickCheck.Checkers
import Test.QuickCheck.Classes

newtype Cont k a = Cont { runCont :: (a -> k) -> k }

instance Functor (Cont k) where
   ...

instance Applicative (Cont k) where
   ...

instance Monad (Cont k) where
   ...

instance (Arbitrary a, Arbitrary b) => Arbitrary (Cont k a) where
    arbitrary = do
        -- akTok <- arbitrary -- How to generate Arbitrary functions like this
        return $ Cont akTok

instance (Eq k, Eq a) => EqProp (Cont k a) where
    (=-=) = eq -- How can I test this equality

main :: IO ()
main = do
    let trigger :: Cont ???
        trigger = undefined
    quickBatch $ functor trigger
    quickBatch $ applicative trigger
    quickBatch $ monad trigger
  ask by Pawan Kumar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...