3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

リストモナドとLogicモナド

Posted at

http://hackage.haskell.org/packages/archive/logict/latest/doc/html/Control-Monad-Logic.html
リストモナドの一般化としてLogicTというものがあり、バックトラックを表現できるらしい。

いくつかの自明な例を作ってみた。

import Control.Monad
import Control.Monad.Logic

fromList :: [a] -> Logic a
fromList xs = logic $ \f x -> foldr f x xs

toList :: Logic a -> [a]
toList = observeAll

main = do
    print $ toList $ return 42 -- [42]
    print $ toList $ fromList [0,1,2] >>= \x -> [x + 1, x + 2, x + 3] -- [1,2,3,2,3,4,3,4,5]
    print $ toList $ mzero -- []
    print $ toList $ fromList [0,1,2] `mplus` fromList [7,8,9] -- [0,1,2,7,8,9]
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?