Monads Oooh, scary! wren ng thornton wrnthorn@indiana.edu Cognitive Science & Computational Linguistics Indiana University Bloomington wren ng thornton Probability Smoothing for NLP – p. 1/7
Outline of the talk • Categories • Functors • Monads wren ng thornton Probability Smoothing for NLP – p. 2/7
Categories • Objects (e.g., types) ◦ Int , Bool , Int × Int , . . . • Morphisms (e.g., functions) ◦ f :Char → Int , . . . • Composition ◦ ∀ f : A → B . ∀ g : B → C . ∃ g ◦ f : A → C • Identity morphisms ◦ ∀ A . ∃ id A : A → A such that � ∀ f : A → B . f ◦ id A = f � ∀ f : B → A . id A ◦ f = f wren ng thornton Probability Smoothing for NLP – p. 3/7
Functors • A map F : C → D from category C to category D ◦ Let’s assume C = D • F takes C -objects to D -objects ◦ data List A = Nil | Cons A ( List A ) ◦ data Maybe A = Nothing | Just A ◦ data Tree A = Tip A | Bin ( Tree A ) ( Tree A ) • F takes C -morphisms to D -morphisms ◦ map List : ( A → B ) → ( List A → List B ) ◦ map Maybe : ( A → B ) → ( Maybe A → Maybe B ) ◦ map Tree : ( A → B ) → ( Tree A → Tree B ) • And follows some laws ◦ ∀ A . F ( id A ) = id ( F A ) ◦ ∀ f . ∀ g . ( F f ) ◦ ( F g ) = F ( f ◦ g ) wren ng thornton Probability Smoothing for NLP – p. 4/7
Monads • A functor T with some extra stuff • A “unit” natural transformation (aka pure , return , point ,. . . ) ◦ η :Id ˙ → T . . . What?! ◦ ∀ A . η A : A → T A . . . not quite ◦ η : ∀ A . A → T A • And a “join” natural transformation (or “bind” ( > =) instead) > ◦ µ : T ◦ T ˙ → T ◦ concat: ∀ A . List ( List A ) → List A • Following some laws ◦ µ A ◦ η A = id A ◦ µ A ◦ T η A = id A ◦ µ A ◦ T µ A = µ T A ◦ µ A wren ng thornton Probability Smoothing for NLP – p. 5/7
Monads wren ng thornton Probability Smoothing for NLP – p. 6/7
∼ fin. wren ng thornton Probability Smoothing for NLP – p. 7/7
Recommend
More recommend