Haskellの全てのモナドにバックトラッキングを加えるライブラリを設計し実装しました。 論理プログラミングにインスパイアされたこのライブラリは、モナドプラスに要求される操作に加え、コンストラクトをfair disjunctions、fair conjunctions、conditionals、pruning、そして表現に富むトップレベルのインターフェースに提供します。 これらの追加コンストラクトを実装することはストリームベースのバックトラッキングモデルでは簡単ですが、継続ベースのモデルで可能であるかはわかりません。 これら全ての追加コンストラクトはmsplitという一つのプリミティブを使うことで一般的かつモナド的に実現できることを示します。 二つのライブラリの実装を紹介します。ひとつは成功と失敗の継続を使ったもので、もう一方は限定継続の操作を使うものです。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# OPTIONS_GHC -cpp -pgmPgcc -optP-E #-} | |
| #define SHOW_RESULT(a) (showResult #a (a)) | |
| showResult :: Show a => String -> a -> String | |
| showResult expr value = expr ++ " = " ++ show value | |
| main :: IO () | |
| main = putStrLn SHOW_RESULT(1 + 2) |
!SLIDE
!SLIDE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package kits | |
| sealed trait Free[F[_], A, B] { | |
| def map[C](f: B => C)(implicit F: Functor[F]): Free[F, A, C] = | |
| this match { | |
| case Pure(a) => Pure(a) | |
| case Impure(fb) => Impure(F.map(fb)(f)) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.language.higherKinds | |
| trait Functor[F[_]] { | |
| def map[A, B](fa: F[A])(f: A => B): F[B] | |
| } | |
| object Functor { | |
| implicit val value: Eval[Val] = new Eval[Val] { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package datatypesalacarte | |
| import scala.language.higherKinds | |
| trait Functor[F[_]] { | |
| def map[A, B](fa: F[A])(f: A => B): F[B] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javafx.application.Application | |
| import javafx.event.EventHandler | |
| import javafx.scene.input.MouseEvent | |
| import javafx.stage.Stage | |
| import javafx.scene.{Scene, Group} | |
| import javafx.scene.control.Button | |
| import scalaz.concurrent.Future | |
| class GUI extends Application { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import System.IO (stdin, hSetBuffering, BufferMode (NoBuffering), hReady) | |
| loop :: Int -> Bool -> IO Int | |
| loop n True = return n | |
| loop n False = do | |
| putStrLn $ show n | |
| ready <- hReady stdin | |
| loop (n + 1) ready | |
| main :: IO () |