2 objective 1 2.1 m a ( ) Haskell Rank2Types 2 newtype Object f g = Object { runobject :: forall a. f a -> g (a, Object f g) } 1 a g a Functor g a ::

Similar documents
monad.gby

fp.gby


2

3 3.1 algebraic datatype data k = 1 1,1... 1,n1 2 2,1... 2,n2... m m,1... m,nm 1 m m m,1,..., m,nm m 1, 2,..., k 1 data Foo x y = Alice x [y] B

5 IO Haskell return (>>=) Haskell Haskell Haskell 5.1 Util Util (Util: Tiny Imperative Language) 1 UtilErr, UtilST, UtilCont, Haskell C

org/ghc/ Windows Linux RPM 3.2 GHCi GHC gcc javac ghc GHCi(ghci) GHCi Prelude> GHCi :load file :l file :also file :a file :reload :r :type expr :t exp

haskell.gby

test.gby

連載 構文解析器結合子 山下伸夫 (( 株 ) タイムインターメディア ) 文字列の分解 1 1 Haskell lines Prelude lines :: String -> [String] lines "" = [] lines s = let (l,s'

Parametric Polymorphism

kyoto.gby

bdd.gby

コンパイラ演習 第 7 回

¥×¥í¥°¥é¥ß¥ó¥°±é½¬I Exercise on Programming I [1zh] ` `%%%`#`&12_`__~~~ alse

# let rec sigma (f, n) = # if n = 0 then 0 else f n + sigma (f, n-1);; val sigma : (int -> int) * int -> int = <fun> sigma f n ( : * -> * ) sqsum cbsu

ML Edinburgh LCF ML Curry-Howard ML ( ) ( ) ( ) ( ) 1

Jacques Garrigue

6-1

Emacs ML let start ::= exp (1) exp ::= (2) fn id exp (3) ::= (4) (5) ::= id (6) const (7) (exp) (8) let val id = exp in

Int Int 29 print Int fmt tostring 2 2 [19] ML ML [19] ML Emacs Standard ML M M ::= x c λx.m M M let x = M in M end (M) x c λx.

IPSJ SIG Technical Report Vol.2013-CE-119 No /3/15 enpoly enpoly enpoly 1) 2) 2 C Java Bertrand Meyer [1] 1 1 if person greeting()

第12回 モナドパーサ

Me and Ope: maoe? maoe Maoe and Ope: Me and Ope: Me and Ope: Reactive Programming Excel reactive programming Maoe and Ope: Excel reactive programming

Dive into Algebraic Effects

0 1 q

presen.gby

ex01.dvi

CX-Checker CX-Checker (1)XPath (2)DOM (3) 3 XPath CX-Checker. MISRA-C 62%(79/127) SQMlint 76%(13/17) XPath CX-Checker 3. CX-Checker 4., MISRA-C CX- Ch

第9回 型とクラス

PowerPoint Presentation

Objective Caml 3.12 Jacques Garrigue ( ) with Alain Frisch (Lexifi), OCaml developper team (INRIA)

2

Copyright c 2008 Zhenjiang Hu, All Right Reserved.

shift/reset [13] 2 shift / reset shift reset k call/cc reset shift k shift (...) k 1 + shift(fun k -> 2 * (k 3)) k 2 * (1 + 3) 8 reset shift reset (..

2 3 Pockets Pockest Java [6] API (Backtracking) 2 [7] [8] [3] i == Pockets 2.1 C3PV web [9] Pockets [10]Pockets 1 3 C

C言語によるアルゴリズムとデータ構造


Microsoft Word - AT _A.doc

ScalaFukuoka 2017 Backlog.key

# let st1 = {name = "Taro Yamada"; id = };; val st1 : student = {name="taro Yamada"; id=123456} { 1 = 1 ;...; n = n } # let string_of_student {n

REALV5_A4…p_Ł\1_4A_OCF

untitled

「都市から地方への人材誘致・移住促進に関する調査」

<91498EE88CA D815B2E786C73>

〔 大 会 役 員 〕

橡本体資料+参考条文.PDF

Lecture on

10/ / /30 3. ( ) 11/ 6 4. UNIX + C socket 11/13 5. ( ) C 11/20 6. http, CGI Perl 11/27 7. ( ) Perl 12/ 4 8. Windows Winsock 12/11 9. JAV

ML λ λ 1 λ 1.1 λ λ λ e (λ ) ::= x ( ) λx.e (λ ) e 1 e 2 ( ) ML λx.e Objective Caml fun x -> e x e let 1

2018 IPSJ/SIGSE Software Engineering Symposium (SES2018) 1,a) 1,b) 1,c) Java 2014 Java Java Java Stream Optional 18% Stream 5% Stream JDK6/7

2) TA Hercules CAA 5 [6], [7] CAA BOSS [8] 2. C II C. ( 1 ) C. ( 2 ). ( 3 ) 100. ( 4 ) () HTML NFS Hercules ( )

. IDE JIVE[1][] Eclipse Java ( 1) Java Platform Debugger Architecture [5] 3. Eclipse GUI JIVE 3.1 Eclipse ( ) 1 JIVE Java [3] IDE c 016 Information Pr

,,,,., C Java,,.,,.,., ,,.,, i

ex01.dvi


新・明解Javaで学ぶアルゴリズムとデータ構造

(CC Attribution) Lisp 2.1 (Gauche )

第10回 モジュール

r02.dvi

ohp02.dvi

paper.pdf

Java updated

: : : TSTank 2

S2DaoでもN:Nできます

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1


O(N) ( ) log 2 N

QCon Tokyo 2016" (Everforth)

ohp1.dvi

やさしいJavaプログラミング -Great Ideas for Java Programming サンプルPDF

平成 19 年度 ( 第 29 回 ) 数学入門公開講座テキスト ( 京都大学数理解析研究所, 平成 19 ~8 年月 72 月日開催 30 日 ) 1 PCF (Programming language for Computable Functions) PCF adequacy adequacy

Exam : 1z1-809-JPN Title : Java SE 8 Programmer II Vendor : Oracle Version : DEMO Get Latest & Valid 1z1-809-JPN Exam's Question and Answers 1 from Ac

K227 Java 2

ld-2.dvi

1 CUI CUI CUI 1.1 cout cin redirect.cpp #i n c l u d e <s t r i n g > 3 using namespace std ; 5 6 i n t main ( void ) 7 { 8 s t r i n g s ; 10 c

IPSJ SIG Technical Report Vol.2014-DBS-159 No.6 Vol.2014-IFAT-115 No /8/1 1,a) 1 1 1,, 1. ([1]) ([2], [3]) A B 1 ([4]) 1 Graduate School of Info

JavaからScalaへ

明解Javaによるアルゴリズムとデータ構造

: gettoken(1) module P = Printf exception End_of_system (* *) let _ISTREAM = ref stdin let ch = ref ( ) let read () = (let c =!ch in ch := inp

新版明解C言語 実践編

(search: ) [1] ( ) 2 (linear search) (sequential search) 1

Java演習(4) -- 変数と型 --

Functional Programming

A B 1: Ex. MPICH-G2 C.f. NXProxy [Tanaka] 2:

1 2 3 race conditions 4 race conditions [1] [3] ( 1 ) safetyliveliness ( 2 ) ( 3 ) 2.2 SPIN SPIN[2] AT&T Bell SPIN Promela Promela C LTL

yacc.dvi

明解Java入門編

IPSJ SIG Technical Report Vol.2015-CLE-16 No /5/23 RESTful Web API Web 1,2,3,4,a) 1,3,2,4 5,6 6 Wannous Muhammad 7,1,8 4,2,1 3,2,1 Maxima Web JS

5 p Point int Java p Point Point p; p = new Point(); Point instance, p Point int 2 Point Point p = new Point(); p.x = 1; p.y = 2;

SML#³«È¯ºÇÁ°Àþ

gc.dvi

Arduino UNO IS Report No. Report Medical Information System Laboratory

( ) ( ) lex LL(1) LL(1)

3 Java 3.1 Hello World! Hello World public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World");

Java (5) 1 Lesson 3: x 2 +4x +5 f(x) =x 2 +4x +5 x f(10) x Java , 3.0,..., 10.0, 1.0, 2.0,... flow rate (m**3/s) "flow

Vol.9 No (Feb. 2016) DFDL 1,a) , ad-hoc legacy DFDL Data Format Description Language 1 Fisher DFDL The Data Description

新・明解Java入門

ALG ppt

_...j.f......_..

Transcription:

Haskell 1, 2 fumiexcel@gmail.com 2 IIJ kazu@iij.ad.jp Haskell Haskell Haskell 1 Haskell[1] Haskell [2] Haskell ( OOPL) GUI Haskell Haskell OOPL OOPL [3, 4] ( OOP) [5] OOP Haskell OOPL Haskell Java C++ C OOPL 2 3 4 5 6 7 8

2 objective 1 2.1 m a ( ) Haskell Rank2Types 2 newtype Object f g = Object { runobject :: forall a. f a -> g (a, Object f g) } 1 a g a Functor g a :: Object f g b :: Object f g f :: f a fmap snd (runobject a f) fmap snd (runobject b f) runobject a f runobject b f a b 2.2 Haskell newtype Mealy a b = Mealy { runmealy :: a -> (b, Mealy a b) } Mealy * -> * 2.3 M N a forall a. M a -> N a 1 http://hackage.haskell.org/package/objective 2 https://ghc.haskell.org/trac/haskell-prime/wiki/rank2types

M N Haskell Natural newtype Natural f g = Natural { runnatural :: forall a. f a -> g a } M N a m :: M a f nat :: Natural M N runnatural nat (fmap f m) fmap f (runnatural nat m) obj :: Object M N runobject obj (fmap f m) fmap (f *** id) (runobject obj m) (***) Haskell Control.Arrow (***) :: (a -> c) -> (b -> d) -> (a, b) -> (c, d) f *** g = \(x, y) = (f x, g y) 2.4 Mealy Natural Object fromnatural Req GHC GADTs fromnatural :: Functor g => Natural f g -> Object f g fromnatural (Natural t) = lifto t lifto :: Functor g => (forall x. f x -> g x) -> Object f g lifto t = Object $ fmap (\x -> (x, lifto t)). t data Req a b r where Req :: a -> Req a b b frommealy :: Mealy a b -> Object (Req a b) Identity frommealy (Mealy t) = Object $ \(Req a) -> let (b, m) = t a in Identity (b, frommealy m) Req a b r a GADT(Generalized Algebraic Data Types) b x x Identity x Object (Req a b) Identity Mealy a b 3 Print Increment Counter data Counter a where Print :: Counter () Increment :: Counter Int

3.1 counter Counter counter counter Increment 1 Print counter :: Int -> Object Counter IO counter n = Object (handle n) where handle :: Int -> Counter a -> IO (a, Object Counter IO) handle n Increment = return (n, counter (n + 1)) handle n Print = print n >> return ((), counter n) counter handle 4 GHCi 3 runobject counter > let obj1 = counter 0 > runobject obj1 Print 0 > (_, obj2) <- runobject obj1 Increment > (_, obj3) <- runobject obj2 Print 1 3.2 MVar[7] Haskell IORef MVar (.-) (.-) :: MVar (Object t IO) -> t a -> IO a m.- e = do obj <- takemvar m (a, obj ) <- runobject obj e putmvar m obj return a newmvar do i <- newmvar (counter 0) i.- Print -- 0 i.- Increment i.- Increment i.- Print -- 2 4 2 counter counter handle counter handle handle handle 3 https://www.haskell.org/ghc/

4.1 p :: Object f g q :: Object g h p g q p @>>@ q :: Object f h (@>>@) (@>>@) :: Functor h => Object f g -> Object g h -> Object f h Object m @>>@ Object n = Object $ fmap joino. n. m joino :: Functor h => ((a, Object f g), Object g h) -> (a, Object f h) joino ((x, a), b) = (x, a @>>@ b) echo ( A ) echo :: Functor f => Object f f echo = Object (fmap (\x -> (x, echo))) 4.2 (@>>@) counter handle transformers [9] StateT counter StateT Int IO counter :: Int -> Object Counter IO counter n = countere @>>@ variable n countere :: Object Counter (StateT Int IO) variable :: Int -> Object (StateT Int IO) IO StateT s m get :: Monad m => StateT s m put :: Monad m => s -> StateT s m () OOP s StateT s m s s 4.3 variable variable :: Monad m => s -> Object (StateT s m) m variable s = Object $ \m -> runstatet m s >>= \(a, s ) -> return (a, variable s ) 4.4 handle 2.4 fromnatural countere :: Object Counter (StateT Int IO) countere = fromnatural (Natural handle) handle :: Counter a -> StateT Int IO a handle Increment = do { n <- get; put (n + 1); return n } handle Print = get >>= \n -> lift (print n)

4.5 ( A ) f g Object f g f g h (@>>@) :: Object f g -> Object g h -> Object f h f echo :: Object f f echo OOP M IO M IO Java C C++ OOPL M IO N IO (is-a ) N M N M Hom(M, IO) Hom(N, IO) Hom(N, M) Hom(M, IO) Hom(N, IO) 5 b : B IO B A A + B A + B IO A B IO f : A IO + B g : B IO + B i B : B IO + B

A i A A + B i B B f IO + B [f, g] [id, b] g IO [id, b] [f, g] 5.1 Haskell data Sum f g a = InL (f a) InR (g a) f g [f, g] (@ @) :: Functor m => Object f m -> Object g m -> Object (Sum f g) m a @ @ b = Object $ \r -> case r of InL f -> fmap (fmap (@ @b)) (runobject a f) InR g -> fmap (fmap (a@ @)) (runobject b g) (G)ADT Sum Smalltalk * -> * Operational [10] Operational data Program t a where Return :: a -> Program t a Bind :: t a -> (a -> Program t b) -> Program t b instance Monad (Program t) where return = Return Return a >>= k = k a Bind t c >>= k = Bind t ((>>= k). c) liftp :: t a -> Program t a liftp t = Bind t Return Program m Object t m Program t sequential :: Monad m => Object t m -> Object (Program t) m sequential r = Object $ liftm (fmap sequential). inv r where inv obj (Return a) = return (a, obj) inv obj (Bind e cont) = runobject obj e >>= \(a, obj ) -> inv obj (cont a)

Program A Program B liftl = liftp. InL liftr = liftp. InR Program (Sum A B) Sum A B copair :: (Monad m) => Object (Program s) m -> Object (Program t) m -> Object (Program (Sum s t)) m copair a0 b0 = sequential (go a0 b0) where go a b = Object $ \r -> case r of InL f -> fmap (\a -> go a b) liftm runobject a (liftp f) InR g -> fmap (\b -> go a b ) liftm runobject b (liftp g) Sum A B Program (Sum A B) 5.2 OOP Adapter Proxy Decorator Proxy 2 counter Print Print 5 Limit exceeded Print wrapper :: Int -> Object Counter (Program (Sum Counter IO)) wrapper n = Object $ \r -> case r of Print n < 5 -> liftl Print >> return ((), wrapper (n + 1)) otherwise -> liftr (putstrln "Limit exceeded") >> return ((), wrapper n) Increment -> liftl Increment >>= \x -> return (x, wrapper n) counter :: Object Counter IO counter = wrapper 0 @>>@ sequential (counter @ @ echo) Template Method Adapter Proxy Decorator Template Method 6 6.1 OOP

Object f Maybe Object f (Either a) a newtype (Mortal ) 4 newtype Mortal f a = Mortal { unmortal :: Object f (Either a) } mortal :: (forall x. f x -> Either a (x, Mortal f a)) -> Mortal f a mortal f = Mortal $ Object (fmap (fmap unmortal). f) runmortal :: Mortal f a -> f x -> Either a (x, Mortal f a) runmortal m = fmap (fmap Mortal). runobject (unmortal m) instance Monad (Mortal f) where return a = mortal $ const $ Left a m >>= k = mortal $ \f -> case runmortal m f of Left a -> runmortal (k a) f Right (x, m ) -> return (x, m >>= k) Mortal return a a m >>= k m k Either EitherT 5 Mortal announce Mortal announce :: Monad m => f a -> StateT [Mortal f r] m ([a], [r]) announce f = StateT $ return. h. fmap unzip. partitioneithers. map ( runmortal f) where h (a, (b, c)) = ((a, b), c) announce [Mortal f r] variable announce 6.2 API (->) a Object ((->) a) m f :: a -> b b 4 mortal 5 http://hackage.haskell.org/package/either

f a gennaturals :: Monad m => Int -> Object ((->) Int) m gennaturals n = Object $ \f -> return (f n, gennaturals (n + 1)) (,) a Object ((,) a) m (a, x) x a ($$) ($$) :: (Monad m) => Object ((->) a) m -> Object ((,) a) m -> m x a $$ b = do (x, a ) <- runobject a id ((), b ) <- runobject b (x, ()) a $$ b (->) a (,) a ($$) EitherT ($$) ($$) :: (Monad m) => Object ((->) a) (EitherT a m) -> Object ((,) a) (EitherT a m) -> EitherT a m Void EitherT a m Void eithert return absurd m a Void 6 absurd :: Void -> a Void EitherT lifto lift :: Object m (EitherT a m) linereader linewriter main foo.txt import Control.Monad.Trans.Either import Control.Monad.Trans import System.IO import Data.Void linereader :: FilePath -> IO (Object ((->) String) (EitherT () IO)) linereader path = fmap go $ openfile path ReadMode where go h = lifto $ \cont -> lift (hiseof h) >>= \r -> if r then lift (hclose h) >> left () else lift $ fmap cont $ hgetline h linewriter :: Object ((,) String) IO linewriter = lifto $ \(s, x) -> putstrln s >> return x main = do r <- linereader "foo.txt" eithert return absurd $ r $$ (linewriter @>>@ lifto lift) 6 http://hackage.haskell.org/package/void

7 Java C++ C 7.1 1 1000 7.2 OOHaskell OOHaskell OOHaskell (IORef) IO 7.3 Extensible effects Extensible effects[12] Extensible effects Eff ask runreader ask :: (Typeable e, Member (Reader e) r) => Eff r e runreader :: Typeable e => Eff (Reader e :> r) w -> e -> Eff r w runreader :: Typeable e => e -> Object (Eff (Reader e :> r)) (Eff r) Foo Bar Baz objfoo objbar objbaz Foo Baz runfoo :: Member Baz r => Object (Eff (Foo :> r)) (Eff r) runbar :: Object (Eff (Bar :> r)) (Eff r) runbaz :: Object (Eff (Baz :> r)) (Eff r) Foo Bar Baz runfoo @>>@ runbar @>>@ runbaz :: Object (Eff (Foo :> Bar :> Baz :> r)) (Eff r) Eff Extensible effects 5

7.4 (expression problem) 7 [11] (Elf) (Orc) 0 (Dwarf) 7.4.1 OOPL OOPL 7.4.2 Haskell Entity data Entity = Elf Int Orc spell :: Entity -> Entity spell Entity spell 7.4.3 Haskell data Elf = Elf Int data Orc = Orc class Spell s where spell :: s -> s 7 http://homepages.inf.ed.ac.uk/wadler/papers/expression/expression.txt

instance Spell Elf where spell =... -- Orc instance Spell Orc where spell =... Elf Orc data Entity = forall s. Spell s => Entity s spell [8] newtype Spell a = Spell { spell :: a -> a } class Action f where elf :: Elf -> f Elf orc :: Orc -> f Orc Spell 7.4.4 Spell elf orc data Spell x where Spell :: Spell () type Entity = Object Spell IO elf :: Int -> Entity elf 0 = Object $ \Spell -> return ((), orc) elf n = Object $ \Spell -> return ((), elf (n-1)) orc :: Entity orc = Object $ \Spell -> return ((), orc) spell :: [Entity] -> IO [Entity] spell = mapm update where update = fmap snd. flip runobject Spell dwarf dwarf :: Int -> Entity dwarf 0 = Object $ \Spell -> return ((), orc) dwarf n = Object $ \Spell -> return ((), dwarf (n-1)) return ((), orc)

class Elf t where elf :: Object t IO instance (Elf s, Elf t) => Elf (Sum s t) where elf = elf @ @ elf instance Elf Spell where elf = class Orc t where orc :: Object t IO instance (Orc s, Orc t) => Elf (Sum s t) where orc = orc @ @ orc instance Orc Spell where orc = elf Cure Elf Cure (Elf s, Elf t) => Elf (Sum s t) elf :: Object (Sum Spell Cure) IO OOP 8 Haskell OOP Haskell OOP Oleg Kiselyov IIJ

[1] Simon Marlow et al, Haskell 2010 Language Report, 2010. [2] Simon Marlow, Parallel and Concurrent Programming in Haskell, O Reilly, 2013. [3] Mark Shields and Simon Peyton Jones, Object-Oriented Style Overloading for Haskell, In the Workshop on Multi-Language Infrastructure and Interoperability (BABEL 01), 2001. [4] Andr T. H. Pang and Manuel M. T. Chakravarty, Interfacing Haskell with Object-Oriented Languages, Implementation of Functional Languages, Lecture Notes in Computer Science Volume 3145, pp 20-35, 2005. [5] Oleg Kiselyov and Ralf Laemmel, Haskell s overlooked object system, http://arxiv.org/pdf/cs/0509027.pdf, 2005. [6] Jeremy Gibbons and Oege de Moor, The Fun of Programming, Palgrave, p 203, 2003. [7] Simon Peyton Jones, Andrew D. Gordon and Sigbjorn Finne, Concurrent Haskell, ACM SIGPLAN- SIGACT Symposium on Principles of Programming Languages (PoPL), 1996. [8] B. C. d. S., Oliveira, Ralf Hinze, Andres Löh, Extensible and Modular Generics for the Masses, Trends in Functional Programming, 2006. [9] Mark P Jones, Functional Programming with Overloading and Higher-Order Polymorphism, Advanced School of Functional Programming, 1995. [10] Heinrich Apfelmus, The Operational Monad Tutorial, The Monad.Reader Issue 15, http://themonadreader.files.wordpress.com/2010/01/issue15.pdf, 2010. [11] Philip Wadler, The Expression Problem, Java Genericity mailing list, http://homepages.inf.ed.ac.uk/wadler/papers/expression/expression.txt, 1998. [12] Oleg Kiselyov et al, Extensible Effects, Proceedings of the 2013 ACM SIGPLAN, http://okmij.org/ftp/haskell/extensible/exteff.pdf, 2013. A Object runobject ino outo A.1 1 e f g h (a :: Object e f) (b :: Object f g) (c :: Object g h) a @>>@ (b @>>@ c) = (a @>>@ b) @>>@ c 1 outo (a @>>@ (b @>>@ c)) = { (@>>@) } fmap joino. fmap joino. outo c. outo b. outo a) f = { fmap fusion } fmap (joino. joino). outo c. outo b. outo a = { (joino. joino) } = fmap (\(((x, ef), fg), gh) -> (x, ef @>>@ (fg @>>@ gh))). outo c. outo b. outo a outo ((a @>>@ b) @>>@ c) = { (@>>@) } fmap joino. outo c. (fmap joino. outo b. outo a) f = { outo. ino = id } fmap joino. outo c. fmap joino. outo b. outo a = { } fmap joino. fmap (first joino). outo c. outo b. outo a = { fmap fusion } fmap (joino. first joino). outo c. outo b. outo a

= { joino. first joino } = fmap (\(((x, ef), fg), gh) -> (x, (ef @>>@ fg) @>>@ gh)). outo c. outo b. outo a = { } = fmap (\(((x, ef), fg), gh) -> (x, ef @>>@ (fg @>>@ gh))). outo c. outo b. outo a = { } outo (a @>>@ (b @>>@ c)) A.2 2 obj :: Object f g echo @>>@ obj = obj 2 outo (echo @>>@ obj) = { echo } fmap (\x -> (x, echo))) @>>@ obj = { (@>>@) } fmap joino. outo obj. fmap (\x -> (x, echo)) = { } fmap joino. fmap (first (\x -> (x, echo))). outo obj = { fmap fusion } fmap (joino. first (\x -> (x, echo))). outo obj = { joino } fmap (\(x, m) -> (x, echo @>>@ m)). outo obj = { } fmap (\(x, m) -> (x, m)). outo obj = { fmap id = id } outo obj A.3 3 obj :: Object f g obj @>>@ echo = obj 3 outo (obj @>>@ echo) = { echo } outo (obj @>>@ Object (fmap (\x -> (x, echo)))) = { (@>>@) } fmap joino. fmap (\x -> (x, echo)). outo obj = { } fmap (joino. (\x -> (x, echo))). outo obj = { fmap fusion } fmap (\(x, m) -> (x, m @>>@ echo)). outo obj = { } fmap (\(x, m) -> (x, m)). outo obj = { fmap id = id } outo obj