SlideShare a Scribd company logo
Walking
insidea
datastructure1 — jaiyalas @ funth - 2017 oct 05
algebraicdatatype
2 — jaiyalas @ funth - 2017 oct 05
algebraicdatatype
review3 — jaiyalas @ funth - 2017 oct 05
· zero: 0
· unit: 1
· variable: a, b, x, ...
· product: (×)
· coproduct: (+)
· fixed-point: μ
4 — jaiyalas @ funth - 2017 oct 05
1.basefunctor
2.typefunctor†
†
type functor = base functor + fixed-point
5 — jaiyalas @ funth - 2017 oct 05
Unit = 1
Maybe a = 1 + a
Pair a b = a × b
Either a b = a + b
data () = ()
data Maybe a = Nothing | Just a
data a b = (a, b)
data Either a b = Left a | Right b
6 — jaiyalas @ funth - 2017 oct 05
Unit = 1
Maybe a = 1 + a
Pair a b = a × b
Either a b = a + b
data () = ()
data Maybe a = Nothing | Just a
data a b = (a, b)
data Either a b = Left a | Right b
7 — jaiyalas @ funth - 2017 oct 05
Unit = 1
Maybe a = 1 + a
Pair a b = a × b
Either a b = a + b
data () = ()
data Maybe a = Nothing | Just a
data a b = (a, b)
data Either a b = Left a | Right b
8 — jaiyalas @ funth - 2017 oct 05
Unit = 1
Maybe a = 1 + a
Pair a b = a × b
Either a b = a + b
data () = ()
data Maybe a = Nothing | Just a
data a b = (a, b)
data Either a b = Left a | Right b
9 — jaiyalas @ funth - 2017 oct 05
ListF a x = 1 + a × x
List a = μx . (1 + a × x)
BTreeF a x = 1 + a × x × x
BTree a = μx . (1 + a × x × x)
data ListF a x = [] | a : x
type List = Fix ListF
data BTreeF a x = Nil | Fork a x x
type BTree = Fix BTreeF
10 — jaiyalas @ funth - 2017 oct 05
ListF a x = 1 + a × x
List a = μx . (1 + a × x)
BTreeF a x = 1 + a × x × x
BTree a = μx . (1 + a × x × x)
data ListF a x = [] | a : x
type List = Fix ListF
data BTreeF a x = Nil | Fork a x x
type BTree = Fix BTreeF
11 — jaiyalas @ funth - 2017 oct 05
polynomialfunctor
12 — jaiyalas @ funth - 2017 oct 05
advantage?13 — jaiyalas @ funth - 2017 oct 05
Beauty14 — jaiyalas @ funth - 2017 oct 05
Properties15 — jaiyalas @ funth - 2017 oct 05
disadvantage?
16 — jaiyalas @ funth - 2017 oct 05
outermost
destruction17 — jaiyalas @ funth - 2017 oct 05
traversal18 — jaiyalas @ funth - 2017 oct 05
19 — jaiyalas @ funth - 2017 oct 05
20 — jaiyalas @ funth - 2017 oct 05
introducing..
21 — jaiyalas @ funth - 2017 oct 05
zipper22 — jaiyalas @ funth - 2017 oct 05
alternative
presentation
for2-tree23 — jaiyalas @ funth - 2017 oct 05
24 — jaiyalas @ funth - 2017 oct 05
25 — jaiyalas @ funth - 2017 oct 05
26 — jaiyalas @ funth - 2017 oct 05
27 — jaiyalas @ funth - 2017 oct 05
spine+2-tree
28 — jaiyalas @ funth - 2017 oct 05
data BT a = Leaf a
| Fork a (BT a) (BT a)
data Rib a = LRib a (Bt a)
| RRib a (Bt a)
data Zipper a = Zipper [Rib a] (BT a)
29 — jaiyalas @ funth - 2017 oct 05
data BT a = Leaf a
| Fork a (BT a) (BT a)
data Rib a = LRib a (Bt a)
| RRib a (Bt a)
data Zipper a = Zipper [Rib a] (BT a)
30 — jaiyalas @ funth - 2017 oct 05
data BT a = Leaf a
| Fork a (BT a) (BT a)
data Rib a = LRib a (Bt a)
| RRib a (Bt a)
data Zipper a = Zipper [Rib a] (BT a)
31 — jaiyalas @ funth - 2017 oct 05
complex
structure?32 — jaiyalas @ funth - 2017 oct 05
one-holecontext
33 — jaiyalas @ funth - 2017 oct 05
34 — jaiyalas @ funth - 2017 oct 05
35 — jaiyalas @ funth - 2017 oct 05
data D a
= C0
| C1 a
| C2 a (D a)
| C3 (D a) Int (D a)
--
--
36 — jaiyalas @ funth - 2017 oct 05
data D a
= C0
| C1 a
| C2 a (D a)
| C3 (D a) Int (D a)
-- path: ?
-- left: ?
37 — jaiyalas @ funth - 2017 oct 05
data D a
= C0
| C1 a
| C2 a (D a)
| C3 (D a) Int (D a)
-- path: ?
-- left: 1
38 — jaiyalas @ funth - 2017 oct 05
data D a
= C0
| C1 a
| C2 a (D a)
| C3 (D a) Int (D a)
-- path: ?
-- left: 1 + (D a)
39 — jaiyalas @ funth - 2017 oct 05
data D a
= C0
| C1 a
| C2 a (D a)
| C3 (D a) Int (D a)
-- path: Int × (D a) + (D a) × Int
-- left: 1 + (D a)
40 — jaiyalas @ funth - 2017 oct 05
mechanical
apprach?41 — jaiyalas @ funth - 2017 oct 05
the
Partial
Differentiation42 — jaiyalas @ funth - 2017 oct 05
basicrules
43 — jaiyalas @ funth - 2017 oct 05
substitutionrule
44 — jaiyalas @ funth - 2017 oct 05
fixed-pointrule
45 — jaiyalas @ funth - 2017 oct 05
observation
46 — jaiyalas @ funth - 2017 oct 05
1-holectx:
list47 — jaiyalas @ funth - 2017 oct 05
data List a = Nil
| Cons a (List a)
-- ListF a x = 1 + a × x
-- List a = μx.ListF a x
48 — jaiyalas @ funth - 2017 oct 05
49 — jaiyalas @ funth - 2017 oct 05
[a,b,c,..,m,n,o,..,x,y,z]
50 — jaiyalas @ funth - 2017 oct 05
[a,b,c,..,m],n,[o,..,x,y,z]
51 — jaiyalas @ funth - 2017 oct 05
1-holectx:
lambdaTerm52 — jaiyalas @ funth - 2017 oct 05
data Term a = Lit a
| Var Int
| Abs (Term a)
| App (Term a) (Term a)
-- F a x = a + Int + x + x × x
-- T a = μx.F a x
53 — jaiyalas @ funth - 2017 oct 05
54 — jaiyalas @ funth - 2017 oct 05
with
data TermStep a = Lambda
| AppFun (Term a)
| AppArg (Term a)
55 — jaiyalas @ funth - 2017 oct 05
-- for a term:
App (Abs (Var 0))
(App (Abs (Lit 2))
(Lit 1))
-- one can reach a node
Lit 2
-- and leave a context:
([ AppArg (Abs (Var 0))
, AppFun (Lit 1)
, Lambda
], ())
56 — jaiyalas @ funth - 2017 oct 05
1-holectx:
rosetree57 — jaiyalas @ funth - 2017 oct 05
data Rose a = Rose a [Rose a]
-- RoseF a x = a × (List x)
-- = a × (μy.ListF x y)
-- = a × (μy.1 + x × y)
-- Rose a = μx.RoseF a x
58 — jaiyalas @ funth - 2017 oct 05
59 — jaiyalas @ funth - 2017 oct 05
richmutually-recursivedatatype?
60 — jaiyalas @ funth - 2017 oct 05
type Size = Int
type Name = String
type Attack = Int
type HP = Int
data Player = Player Name Bag HP
data Bag = Bag [Item]
data Item = Weapon Name Attack
| Potion Name PotionEffect
| SmallBag Size Bag
data PotionEffect = Poison Int | Healing Int
61 — jaiyalas @ funth - 2017 oct 05
yougetblessed!allhealing-potionsget"hp+10"
62 — jaiyalas @ funth - 2017 oct 05
bless :: Player -> Player
bless (Player n bag ss) = Player n (updateBag bag) ss
updateBag :: Bag -> Bag
updateBag (Bag xs) = Bag $ map updatePotion xs
updatePotion :: Item -> Item
updatePotion (Potion n pe) = Potion n (updateHealing pe)
updatePotion (SmallBag s bag) = SmallBag s (updateBag bag)
updatePotion x = x
updateHealing :: PotionEffect -> PotionEffect
updateHealing (Healing i) = Healing (i + 10)
updateHealing x = x
63 — jaiyalas @ funth - 2017 oct 05
bless :: Player -> Player
bless (Player n bag ss) = Player n (updateBag bag) ss
updateBag :: Bag -> Bag
updateBag (Bag xs) = Bag $ map updatePotion xs
updatePotion :: Item -> Item
updatePotion (Potion n pe) = Potion n (updateHealing pe)
updatePotion (SmallBag s bag) = SmallBag s (updateBag bag)
updatePotion x = x
updateHealing :: PotionEffect -> PotionEffect
updateHealing (Healing i) = Healing (i + 10)
updateHealing x = x
64 — jaiyalas @ funth - 2017 oct 05
65 — jaiyalas @ funth - 2017 oct 05
non-recursivemaptrick
66 — jaiyalas @ funth - 2017 oct 05
1.typeextension
2.generaltraversal
67 — jaiyalas @ funth - 2017 oct 05
updateHealing :: PotionEffect -> PotionEffect
updateHealing (Healing i) = Healing (i + 10)
updateHealing x = x
bless' :: Player -> Player
bless' p = everywhere (mkT updateHealing) p
-- mkT = type extension
-- everywhere = general traversal
68 — jaiyalas @ funth - 2017 oct 05
1.typeextension
69 — jaiyalas @ funth - 2017 oct 05
{- Data.Typeable -}
-- abstract class
class Typeable a where
typeOf :: a -> TypeRep
-- type-safe cast operation
cast :: (Typeable a, Typeable b) => a -> Maybe b
cast = ... typeOf ...
70 — jaiyalas @ funth - 2017 oct 05
mkT :: (Typeable a, Typeable b)
=> (b -> b) -> a -> a
mkT f = case cast f of
Just g -> g
Nothing -> id
updateHealing' :: Typeable a => a -> a
updateHealing' = mkT updateHealing
71 — jaiyalas @ funth - 2017 oct 05
*Main> updateHealing' (HP 10)
HP 10
*Main> updateHealing' (Potion "A" (Healing 10))
Potion "A" (Healing 10)
*Main> updateHealing' (Healing 10)
Healing 20
72 — jaiyalas @ funth - 2017 oct 05
data Player = Player Name Bag HP
deriving (Typeable)
data Bag = Bag [Item]
deriving (Typeable)
data Item = Weapon Name Attack
| Potion Name PotionEffect
| SmallBag Size Bag
deriving (Typeable)
data PotionEffect = Poison Int
| Healing Int
deriving (Typeable)
73 — jaiyalas @ funth - 2017 oct 05
2.generaltraversal
74 — jaiyalas @ funth - 2017 oct 05
one-layertraversal
{- Data.Data -}
class Typeable a => Data a where
gmapT :: (forall b. Data b => b -> b) -> a -> a
instance Typeable a => Data [a] where
gmapT f [] = []
gmapT f (x : xs) = (f x) : (f xs)
instance Data Player where
gmapT f (Player name bag hp) =
Player (f name) (f bag) (f hp)
75 — jaiyalas @ funth - 2017 oct 05
recursivetraversal
-- bottom-up
everywhere :: Data a
=> (forall b. Data b => b -> b)
-> a -> a
everywhere f x = f (gmapT (everywhere f) x)
-- top-down
everywhere' :: Data a
=> (forall b. Data b => b -> b)
-> a -> a
everywhere' f x = gmapT (everywhere' f) (f x)
76 — jaiyalas @ funth - 2017 oct 05
data Player = Player Name Bag HP
deriving (Typeable, Data)
data Bag = Bag [Item]
deriving (Typeable, Data)
data Item = Weapon Name Attack
| Potion Name PotionEffect
| SmallBag Size Bag
deriving (Typeable, Data)
data PotionEffect = Poison Int
| Healing Int
deriving (Typeable, Data)
77 — jaiyalas @ funth - 2017 oct 05
bless' p = everywhere (mkT updateHealing) p
jack = Player
"Jack"
(Bag [ Potion "A" (Healing 1)
, Potion "B" (Poison 5)
, SmallBag 1 (Bag [Potion "C" (Healing 5)])
]) 10
*Main> bless' jack
Player
"Jack"
(Bag [ Potion "A" (Healing 11)
, Potion "B" (Poison 5)
, SmallBag 1 (Bag [Potion "C" (Healing 15)])
]) 10
78 — jaiyalas @ funth - 2017 oct 05
TL;DR79 — jaiyalas @ funth - 2017 oct 05
Zipper[1/3]80 — jaiyalas @ funth - 2017 oct 05
2-tree's
alternative
presentation81 — jaiyalas @ funth - 2017 oct 05
resumable
traversal82 — jaiyalas @ funth - 2017 oct 05
spine+2-tree
83 — jaiyalas @ funth - 2017 oct 05
1-HoleCtx[2/3]
84 — jaiyalas @ funth - 2017 oct 05
generalizedzipper
85 — jaiyalas @ funth - 2017 oct 05
PartialDifferentiation
polynomialfunctor
86 — jaiyalas @ funth - 2017 oct 05
resumable
structure87 — jaiyalas @ funth - 2017 oct 05
Non-RecursiveMap
[3/3]
88 — jaiyalas @ funth - 2017 oct 05
richmutural-recursivestructure
89 — jaiyalas @ funth - 2017 oct 05
1.typeextension
2.generaltraversal
3.haskellext+deriving90 — jaiyalas @ funth - 2017 oct 05
· zipper
· one-hole context
· non-recursive map
thankyou
91 — jaiyalas @ funth - 2017 oct 05
appendix92 — jaiyalas @ funth - 2017 oct 05
about
93 — jaiyalas @ funth - 2017 oct 05

More Related Content

PDF
Igraph
PDF
Bit Twiddling Hacks: Integers
PDF
Introduction to R for data science
PPTX
Combined Functions
PDF
Pc12 sol c04_4-2
PDF
Mathematica for Physicits
PDF
Python for Data Science and Scientific Computing
PPTX
Exploratory Analysis Part1 Coursera DataScience Specialisation
Igraph
Bit Twiddling Hacks: Integers
Introduction to R for data science
Combined Functions
Pc12 sol c04_4-2
Mathematica for Physicits
Python for Data Science and Scientific Computing
Exploratory Analysis Part1 Coursera DataScience Specialisation

More from Yun-Yan Chi (13)

PDF
Tezos Taipei Meetup #2 - 15/06/2019
PDF
for "Parallelizing Multiple Group-by Queries using MapReduce"
PDF
Program Language - Fall 2013
PDF
Machine X Language
PDF
Examples for loopless
PDF
Insert 2 Merge
PDF
Any tutor
PDF
Data type a la carte
PDF
Genetic programming
PDF
Paper presentation: The relative distance of key point based iris recognition
PDF
Deriving a compiler and interpreter for a Multi-level
PDF
Number System in Haskell
PDF
Constructing List Homomorphisms from Proofs
Tezos Taipei Meetup #2 - 15/06/2019
for "Parallelizing Multiple Group-by Queries using MapReduce"
Program Language - Fall 2013
Machine X Language
Examples for loopless
Insert 2 Merge
Any tutor
Data type a la carte
Genetic programming
Paper presentation: The relative distance of key point based iris recognition
Deriving a compiler and interpreter for a Multi-level
Number System in Haskell
Constructing List Homomorphisms from Proofs
Ad

Recently uploaded (20)

PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
medical staffing services at VALiNTRY
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
System and Network Administration Chapter 2
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
history of c programming in notes for students .pptx
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Reimagine Home Health with the Power of Agentic AI​
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Softaken Excel to vCard Converter Software.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
2025 Textile ERP Trends: SAP, Odoo & Oracle
medical staffing services at VALiNTRY
Navsoft: AI-Powered Business Solutions & Custom Software Development
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
System and Network Administration Chapter 2
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Odoo Companies in India – Driving Business Transformation.pdf
CHAPTER 2 - PM Management and IT Context
Computer Software and OS of computer science of grade 11.pptx
Upgrade and Innovation Strategies for SAP ERP Customers
How to Choose the Right IT Partner for Your Business in Malaysia
history of c programming in notes for students .pptx
How to Migrate SBCGlobal Email to Yahoo Easily
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Ad

Traversing on Algebraic Datatype