Skip to content

Instantly share code, notes, and snippets.

@tokiwoousaka
tokiwoousaka / gist:944425424107f0f578a3
Created October 1, 2014 05:44
Eff.StateをMonadStateのインスタンスにすればLensが使える
{-# LANGUAGE FlexibleContexts, ScopedTypeVariables #-}
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, UndecidableInstances #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Data.Typeable
import Control.Eff
import Control.Eff.State.Lazy as EST
import Control.Monad.State.Class (MonadState(..))
@tokiwoousaka
tokiwoousaka / gist:1a8b72044f6e3a7b5b93
Created October 1, 2014 06:51
Eff.Choose + Eff.State + Lens
{-# LANGUAGE FlexibleContexts, ScopedTypeVariables #-}
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, UndecidableInstances #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Data.Typeable
import Control.Eff
import Control.Eff.State.Lazy as EST
import Control.Eff.Choose
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Control.Lens
import Control.Monad.State
data Hoge = Hoge
{ _Foo :: Int
, _Bar :: String
} deriving (Show, Read, Eq, Ord)
@tokiwoousaka
tokiwoousaka / gist:ace99e3a5dcb81c6b67e
Last active August 29, 2015 14:09
Stateにたいして色んな処理が書けるようになったよー https://github.com/tokiwoousaka/reasonable-lens
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Control.Monad.State
import Control.Lens
type StateIO s a = StateT s IO a
data Hoge a = Hoge
{ _foo :: a
, _bar :: String
f :: Maybe Int
f = do
x <- return 5
y <- return 3
return $ x + y
f' :: Maybe Int
f' = do
x <- return 5
import Control.Monad.State
main :: IO ()
main = print $ runState f 1000 --状態の初期値を1000にしてfを実行
f :: State Int ()
f = do
x <- sub1
sub2 x
@tokiwoousaka
tokiwoousaka / gist:1a06887f58e7465ef6e9
Last active August 29, 2015 14:10
SeasonalFruitsを「SeasonとFruitのタプル」のリストと定義、即ち、季節と果物の直積の部分集合を表す。SeasonalFruits -> [Season] という型から、query関数はSeasonalFruitsから季節の部分集合への写像であると読める。
module Main where
import Control.Monad
type Season = String
type Fruit = String
type SeasonalFruits = [(Season, Fruit)]
dt :: SeasonalFruits
dt =
[ ("春", "いちご")
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
module Main where
----
-- Coyoneda
data CoYoneda f x = forall b. CoYoneda (b -> x) (f b)
instance Functor (CoYoneda f) where
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Control.Monad.State
import Control.Lens
data Point = Point
{ _pointX :: Int
, _pointY :: Int
} deriving Show
{-# LANGUAGE RankNTypes #-}
module Main where
type Bool' = forall a. a -> a -> a
true :: Bool'
true = const
false :: Bool'
false = const id