SlideShare a Scribd company logo
1 of 21
Download to read offline
Mendler-style
Recursion Schemes for
Mixed-Variant Datatypes
Ki Yung Ahn Tim Sheard Marcelo P. Fiore
kya@cs.pdx.edu sheard@cs.pdx.edu Marcelo.Fiore@cl.cam.ac.uk
TYPES 2013
Outline
Mendler-style Recursion Schemes
mit (iteration)
msfit (iteration with syntactic Inverse)
Untyped HOAS (Regular Mixed-Variant Datatype)
Formatting Untyped HOAS (using msfit at kind *)
Simply-Typed HOAS (Indexed Mixed-Variant Datatype)
Formatting Untyped HOAS (using msfit at kind * β†’ *)
The Nax language
Need for yet another Mendler-style Recursion Scheme
already inconsistent as a logic (via Curry--Howard correspondence)
without even having to introduce any term-level recursion
since we can already define general recursion using this
(iso-) Recursive Types
in Functional Languages
Having both unrestricted formation
and unrestricted elimination
of ΞΌ leads to inconsistency
data T a r = C (r β†’ a)
w : ΞΌ (T a) β†’ a -- encoding of (Ξ»x. x x) in the untyped Ξ»-calc
w = Ξ»v. case (unIn v) of (C x) β†’ f (C x)
-- w (C w) amounts to a well-known diverging term in Ξ»-calculus
fix : (a β†’ a) β†’ a -- the Y combinator (Ξ»f.(Ξ»x.f(xx))(Ξ»x.f(xx))))
fix = Ξ»f. (Ξ»x. f (w x)) (In (C (Ξ»x. f (w x))))
To recover consistency, one could
either restrict formation (conventional approach)
or restrict elimination (Mendler-style approach)
Conventional Iteration over
Recursive Types
allow formation of only
positive recursive types
(i.e., when F has a map)
freely eliminate (i.e. use unIn) recursive values
Mendler-style Iteration over
Recursive Types
elimination is possible
only through mit
freely form ANY recursive type!!
note, no (ΞΌ-elim) rule
Mendler-style Iteration
-- Mu at different kinds (there are many more, one at each kind)
data Mu0 (f :: * β†’ *) = In0 (f (Mu0 f ))
data Mu1 (f :: (*β†’*)β†’(*β†’*)) i= In1 (f (Mu1 f ) i)
-- Mendler-style iterators at different kinds
mit0 :: Phi0 f a β†’ Mu f β†’ a
mit0 phi (In0 x) = phi (mit0 phi) x
mit1 :: Phi1 f a i β†’ Mu f i β†’ a i
mit1 phi (In1 x) = phi (mit1 phi) x
type Phi0 f = βˆ€r. (r β†’ a ) β†’ (f r β†’ a )
type Phi1 f = βˆ€r. (βˆ€i. r i β†’ a i) β†’ (βˆ€i. f r i β†’ a i)
βœ” Uniformly defined at different
kinds (can handle non-regular
datatypes quite the same way
as handling regular datatypes)
βœ” Terminates for ANY datatype
(can embed Mu and mit into Fw.
see Abel, Matthes and Uustalu TCS'04)
βœ” However, not very useful for
mixed-variant datatypes
eliminating In0 or In1 by pattern matching
is only allowed in Mendler-style iterators
recursive call
Mendler-style Iteration
with a syntactic inverse
-- Mu' at different kinds (we only use two of them in this talk)
data Mu'0 (f :: * β†’ *) a = In'0 (f (Mu'0 f a)) | Inverse0 a
data Mu'1 (f :: (*β†’*)β†’(*β†’*)) a i = In'1 (f (Mu'1 f a) i)| Inverse1 (a i)
-- msfit at different kinds
msfit0 :: Phi'0 f a β†’ (βˆ€a. Mu'0 f a) β†’ a
msfit0 phi (In'0 x) = phi Inverse0 (msfit0 phi) x
msfit1 :: Phi'1 f a i β†’ (βˆ€a. Mu'1 f a i) β†’ a i
msfit1 phi (In'1 x) = phi Inverse1 (msfit1 phi) x
type Phi'0 f a = βˆ€r. (a β†’ r a ) β†’ (r a β†’ a ) β†’ (f r a β†’ a )
type Phi'1 f a = βˆ€r. (βˆ€i. a i β†’ r a i) β†’ (βˆ€i. r a i β†’ a i) β†’ (βˆ€i. f (r a) i β†’ a i)
βœ” Terminates for ANY datatype
(can embed Mu and msfit into
Fw. see Ahn and Sheard ICFP'11)
βœ” msfit is quite useful for
mixed-variant datatypes,
due to "inverse" operation in
addition to "recursive call"
eliminating In'0 or In'1 by pattern matching
is only allowed in Mendler-style iterators
recursive callinverse
-- using general recursion at type level
data Exp = Lam (Exp β†’ Exp) | App Exp Exp
-- using fixpoint (Mu'0) over non-recursive base structure (ExpF)
data ExpF r = Lam (r β†’ r) | App r r
type Exp' a = Mu'0 ExpF a -- (Exp' a) may contain Inverse
type Exp = βˆ€a . Exp' a -- Exp does not contain Inverse
lam :: (βˆ€a. Exp' a β†’ Exp' a) -> Exp -- it's not (Exp β†’ Exp) β†’ Exp
lam f = In'0 (Lam f ) -- f can handle Inverse containing values
-- but can never examine its content
app :: Exp β†’ Exp β†’ Exp
app e1 e2 = In'0 (App e1 e2)
Untyped HOAS
(a regular mixed-variant datatype)
Formatting Untyped HOAS
using msfit0 at kind *
showExp :: Exp β†’ String
showExp e = msfit0 phi e vars where
phi :: Phi'0 ExpF ([String] β†’ String)
phi inv showE (Lam z) =
Ξ»(v:vs) β†’ "(Ξ»"++v++"β†’"++ showE (z (inv (const v))) vs ++")"
phi inv showE (App x y) =
Ξ»vs β†’ "("++ showE x vs ++" "++ showE y vs ++")"
type Phi'0 f a = βˆ€ r. (a β†’ r a) β†’ (r a β†’ a) β†’ (f r a β†’ a)
vars = [ "x"++show n | n<-[0..] ] :: [String]
recursive callinverse
Simply-Typed HOAS
-- using general recursion at type level
data Exp t where -- Exp :: * β†’ *
Lam :: (Exp t1 β†’ Exp t2) β†’ Exp (t1 β†’ t2)
App :: Exp (t1 β†’ t2) β†’ Exp t1 β†’ Exp t2
-- using fixpoint (Mu'1) over non-recursive base structure (ExpF)
data ExpF r t where -- ExpF :: (* β†’ *) β†’ (* β†’ *)
Lam :: (r t1 β†’ r t2) β†’ ExpF r (t1 β†’ t2)
App :: r (t1 β†’ t2) β†’ r t1 β†’ ExpF r t2
type Exp' a t = Mu'1 ExpF a t -- (Exp' a t) might contain Inverse
type Exp t = βˆ€a . Exp' a t -- (Exp t) does not contain Inverse
lam :: (βˆ€a . Exp' a t1 β†’ Exp' a t2) β†’ Exp (t1 β†’ t2)
lam f = In'1 (Lam f ) -- f can handle Inverse containing values
-- but can never examine its content
app :: Exp (t1 β†’ t2) β†’ Exp t1 β†’ Exp t2
app e1 e2 = In'1 (App e1 e2)
(a non-regular mixed-variant datatype)
newtype Id a = MkId { unId :: a }
evalHOAS :: Exp t β†’ Id t
evalHOAS e = msfit1 phi e where
phi :: Phi'1 ExpF Id {- v :: t1 -} {- f :: r Id t1 -> r Id t2 -}
phi inv ev (Lam f ) = MkId(Ξ»v β†’ unId(ev (f (inv (MkId v)))))
phi inv ev (App e1 e2) = MkId(unId(ev e1) (unId(ev e2)))
type Phi'1 f a = βˆ€r. (βˆ€i. a i β†’ r a i) β†’ (βˆ€i. r a i β†’ a i) β†’ (βˆ€i. f (r a) i β†’ a i)
This is example is a super awesome cooooool discovery that
System Fw can express Simply-Typed HOAS evaluation!!!
Evaluating Simply-Typed HOAS
using msfit1 at kind *β†’*
recursive callinverse
The Nax language
βœ”Built upon the idea of Mendler-style recursion schemes
βœ”Supports an extension of Hindley--Milner type inference to make it
easier to use Mendler-style recursion schemes and indexed datatypes
βœ”Handling different notions of recursive type operators (Mu, Mu') still
needs more rigorous clarification in the theory, but intuitively,
Mu0 f = βˆ€a. Mu'0 f a
Mu1 f = βˆ€a. Mu'1 f a i
...
So, we hide Mu' from users as if there is one kind of Mu
and Mu' is only used during the computation of msfit
βœ”Supports term-indexed types as well as type-indexed types.
Our term indexed calculus, System Fi (to appear in TLCA 2013),
extends System Fw with erasable term indices.
Need for yet another
Mendler-style recursion scheme
There are more recursion schemes other than mit and msfit
E.g., Mendler-style primitive recursion (mpr) can cast from abstract
recursive type (r) to concrete recursive type (Mu f ).
Phi0 f a = βˆ€r. (r β†’ Mu f ) β†’ (r β†’ a ) β†’ (f r β†’ a )
Phi1 f a = βˆ€r. (βˆ€i. r i β†’ Mu f i) β†’ (βˆ€i. r i β†’ a i) β†’ (βˆ€i. f r i β†’ a i)
With mpr, one can write constant time predecessor for natural
numbers and tail function for lists by using the cast operation.
Next example motivates an extension to mpr that can uncast from
concrete recursive type (Mu f i) to abstract recursive type (r i)
Phi1 f a = βˆ€r. (βˆ€i. Mu f i β†’ r i) β†’
(βˆ€i. r i β†’ Mu f i) β†’ (βˆ€i. r i β†’ a i) β†’ (βˆ€i. f r i β†’ a i)
Recursion scheme with above Phi1 type does not terminate for mixed-
variant datatypes -- needs some additional restriction on uncast.
data V r t where V :: (r t1 β†’ r t2) β†’ V r (t1 β†’ t2)
type Val t = Mu V t
val f = In1 (V f )
evalHOAS :: Exp t β†’ Val t
evalHOAS e = msfit1 phi e where
phi :: Phi'1 ExpF (Mu1 V) -- f :: r Val t1 -> r Val t2 , v :: Val t1
phi inv ev (Lam f ) = val(Ξ»v β†’ ev (f (inv v)))
phi inv ev (App e1 e2) = unVal (ev e1) (ev e2)
Only if we had unVal :: Val (t1 β†’ t2) β†’ (Val t1 β†’ Val t2) it would be
possible to write this, but unVal does not seem to be definable
using any known Mendler-style recursion scheme.
Evaluating Simply-Typed HOAS
into a user defined value domain
New recursion scheme to write unVal
data V r t where V :: (r t1 β†’ r t2) β†’ V r (t1 β†’ t2)
type Val t = Mu V t
val f = In1 (V f )
unVal v = unId(mprsi phi v) where
phi :: Phi1 V Id
phi uncast cast (V f) = Id(Ξ»v. cast (f (uncast v)))
mprsi :: Phi1 f a β†’ Mu f i β†’ a i
mprsi phi (In1 x) = phi id id (mrpsi phi) x
-- size restriction over indices in both uncast and cast
type Phi1 f a = βˆ€ r j. (βˆ€i. (i < j) β‡’ Mu f i β†’ r i) β†’ -- uncast
(βˆ€i. (i < j) β‡’ r i β†’ Mu f i) β†’ -- cast
(βˆ€i. r i β†’ a i) β†’ (r j β†’ a j)
-- or, maybe without the size restriction over indices in cast
type Phi1 f a = βˆ€ r j. (βˆ€i. (i < j) β‡’ Mu f i β†’ r i) β†’ -- uncast
(βˆ€i. r i β†’ Mu f i) β†’ -- cast
(βˆ€i. r i β†’ a i) β†’ (f r j β†’ a j)
Preliminary idea that still need further
studies for the termination proof
How are Mu and Mu' related?
data Mu0 (f :: * β†’ *) = In0 (f (Mu0 f ))
data Mu'0 (f :: * β†’ *) a = In'0 (f (Mu'0 f a)) | Inverse0 a
type Phi0 f = βˆ€r. (r β†’ a ) β†’ (f r β†’ a )
mit0 :: Phi0 f a β†’ Mu f β†’ a
mit0 phi (In0 x) = phi (mit0 phi) x
type Phi'0 f a = βˆ€ r. (a β†’ r a) β†’ (r a β†’ a) β†’ (f (r a) β†’ a)
msfit0 :: Phi'0 f a β†’ (βˆ€a. Mu'0 f a) β†’ a
msfit0 phi (In'0 x) = phi Inverse0 (msfit0 phi) x
Can we coerece from (βˆ€a. Mu'0 f a) to Mu f ? Yes!!!
Can we coerece from Mu f to (βˆ€a. Mu'0 f a) ? Not exactly...
Corecion from Mu' to Mu
data E r = Lam (r -> r) | App r r
type Expr = Mu0 E
type Exp' a = Mu'0 E a
type Exp = (forall a. Exp' a) -- .i.e (forall a. Rec0 E a)
exp2expr :: Exp -> Expr -- i.e., (forall a. Rec0 E a) -> Mu0 E
exp2expr = msfit0 phi where
phi inv p2r (Lam f) = In0(Lam((x -> p2r (f (inv x)))))
phi inv p2r (App e1 e2) = In0(App (p2r e1) (p2r e2))
Attempt from Mu to Mu'
msfit' :: (forall r. (a -> r a) -> (r a -> a) -> f (r a) -> a) -> Mu'0 f a -> a
msfit' phi (In'0 x) = phi Inverse0 (msfit' phi) x
msfit' phi (Inverse0 z) = z
exp'2expr :: Exp' Expr -> Expr -- i.e., Mu'0 E (Mu0 E) -> Mu0 E
exp'2expr = msfcata' phi where
phi inv p2r (Lam f) = In0(Lam((x -> p2r (f (inv x)))))
phi inv p2r (App e1 e2) = In0(App (p2r e1) (p2r e2))
-- using general recursion and pattern matching against In0
expr2exp' :: Expr -> Exp' Expr -- i.e., Mu0 E -> Mu'0 E (Mu0 E)
expr2exp' (In0(Lam f)) = In'0(Lam (x -> expr2exp' (f (exp'2expr x))))
expr2exp' (In0(App e1 e2)) = In'0(App (expr2exp' e1) (expr2exp' e2))
Why from Mu' to Mu possible
but not the other way?
data E r = Lam (r β†’ r) | App r r
type Exp' a = Mu'0 ExpF a -- (Exp' a) may contain Inverse
type Exp = βˆ€a . Exp' a -- Exp does not contain Inverse
lam :: (βˆ€a. Exp' a β†’ Exp' a) -> Exp -- it's not (Exp β†’ Exp) β†’ Exp
lam f = In'0 (Lam f ) -- f can handle Inverse containing values
-- but can never examine its content
type Expr = Mu0 E
lam :: Expr β†’ Expr
lam f = In0 (Lam f)
Assume that Expr = Exp = (βˆ€a . Exp' a)
There are less functions with type
(βˆ€a. Exp' a) β†’ (βˆ€a. Exp' a) than
functions with type (βˆ€a. Exp' a β†’ Exp' a)
Thanks for listening.
Questions or Suggestions?
===============

More Related Content

What's hot

Core csharp and net quick reference
Core csharp and net quick referenceCore csharp and net quick reference
Core csharp and net quick referenceilesh raval
Β 
Chapter 1 (functions).
Chapter 1 (functions).Chapter 1 (functions).
Chapter 1 (functions).Eko Wijayanto
Β 
Monoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and CatsMonoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and CatsPhilip Schwarz
Β 
Lesson 6: The derivative as a function
Lesson 6: The derivative as a functionLesson 6: The derivative as a function
Lesson 6: The derivative as a functionMatthew Leingang
Β 
Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scalaPiotr ParadziΕ„ski
Β 
Top down and botttom up 2 LATEST.
Top down     and botttom up 2 LATEST.Top down     and botttom up 2 LATEST.
Top down and botttom up 2 LATEST.Gerwin Ocsena
Β 
Category Theory in 10 Minutes
Category Theory in 10 MinutesCategory Theory in 10 Minutes
Category Theory in 10 MinutesJordan Parmer
Β 
It's All About Morphisms
It's All About MorphismsIt's All About Morphisms
It's All About MorphismsUberto Barbini
Β 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)Siddhesh Pange
Β 
Functor Composition
Functor CompositionFunctor Composition
Functor CompositionPhilip Schwarz
Β 
Ch06
Ch06Ch06
Ch06Hankyo
Β 
Why functional programming and category theory strongly matters
Why functional programming and category theory strongly mattersWhy functional programming and category theory strongly matters
Why functional programming and category theory strongly mattersPiotr ParadziΕ„ski
Β 
Quality Python Homework Help
Quality Python Homework HelpQuality Python Homework Help
Quality Python Homework HelpPython Homework Help
Β 
Introduction to Iteratees (Scala)
Introduction to Iteratees (Scala)Introduction to Iteratees (Scala)
Introduction to Iteratees (Scala)Alexander Lehmann
Β 
Lesson 5: Continuity
Lesson 5: ContinuityLesson 5: Continuity
Lesson 5: ContinuityMatthew Leingang
Β 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
Β 
Quality Python Homework Help
Quality Python Homework HelpQuality Python Homework Help
Quality Python Homework HelpPython Homework Help
Β 

What's hot (20)

Core csharp and net quick reference
Core csharp and net quick referenceCore csharp and net quick reference
Core csharp and net quick reference
Β 
Chapter 1 (functions).
Chapter 1 (functions).Chapter 1 (functions).
Chapter 1 (functions).
Β 
Monoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and CatsMonoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and Cats
Β 
Lesson 6: The derivative as a function
Lesson 6: The derivative as a functionLesson 6: The derivative as a function
Lesson 6: The derivative as a function
Β 
Functions (1)
Functions (1)Functions (1)
Functions (1)
Β 
Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scala
Β 
Top down and botttom up 2 LATEST.
Top down     and botttom up 2 LATEST.Top down     and botttom up 2 LATEST.
Top down and botttom up 2 LATEST.
Β 
Category Theory in 10 Minutes
Category Theory in 10 MinutesCategory Theory in 10 Minutes
Category Theory in 10 Minutes
Β 
It's All About Morphisms
It's All About MorphismsIt's All About Morphisms
It's All About Morphisms
Β 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
Β 
Cyclcone a safe dialect of C
Cyclcone a safe dialect of CCyclcone a safe dialect of C
Cyclcone a safe dialect of C
Β 
Python data handling
Python data handlingPython data handling
Python data handling
Β 
Functor Composition
Functor CompositionFunctor Composition
Functor Composition
Β 
Ch06
Ch06Ch06
Ch06
Β 
Why functional programming and category theory strongly matters
Why functional programming and category theory strongly mattersWhy functional programming and category theory strongly matters
Why functional programming and category theory strongly matters
Β 
Quality Python Homework Help
Quality Python Homework HelpQuality Python Homework Help
Quality Python Homework Help
Β 
Introduction to Iteratees (Scala)
Introduction to Iteratees (Scala)Introduction to Iteratees (Scala)
Introduction to Iteratees (Scala)
Β 
Lesson 5: Continuity
Lesson 5: ContinuityLesson 5: Continuity
Lesson 5: Continuity
Β 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
Β 
Quality Python Homework Help
Quality Python Homework HelpQuality Python Homework Help
Quality Python Homework Help
Β 

Similar to Can two fixpoint types in Mendler style unite?

Optics Fourier Transform I
Optics Fourier Transform IOptics Fourier Transform I
Optics Fourier Transform Idiarmseven
Β 
Domain Transformations
Domain TransformationsDomain Transformations
Domain TransformationsDmitri Nesteruk
Β 
Numerical Fourier transform based on hyperfunction theory
Numerical Fourier transform based on hyperfunction theoryNumerical Fourier transform based on hyperfunction theory
Numerical Fourier transform based on hyperfunction theoryHidenoriOgata
Β 
Programmable PN Sequence Generators
Programmable PN Sequence GeneratorsProgrammable PN Sequence Generators
Programmable PN Sequence GeneratorsRajesh Singh
Β 
senior seminar
senior seminarsenior seminar
senior seminarJose Stewart
Β 
the fourier series
the fourier seriesthe fourier series
the fourier seriessafi al amu
Β 
Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskellgoncharenko
Β 
Some properties of m sequences over finite field fp
Some properties of m sequences over finite field fpSome properties of m sequences over finite field fp
Some properties of m sequences over finite field fpIAEME Publication
Β 
Humble introduction to category theory in haskell
Humble introduction to category theory in haskellHumble introduction to category theory in haskell
Humble introduction to category theory in haskellJongsoo Lee
Β 
Application of derivatives
Application of derivativesApplication of derivatives
Application of derivativesindu thakur
Β 
A Language Designer’s Workbench. A one-stop shop for implementation and verif...
A Language Designer’s Workbench. A one-stop shop for implementation and verif...A Language Designer’s Workbench. A one-stop shop for implementation and verif...
A Language Designer’s Workbench. A one-stop shop for implementation and verif...Eelco Visser
Β 
5.5 Injective and surjective functions. A handout.
5.5 Injective and surjective functions. A handout.5.5 Injective and surjective functions. A handout.
5.5 Injective and surjective functions. A handout.Jan Plaza
Β 
3.1 derivative of a function
3.1 derivative of a function3.1 derivative of a function
3.1 derivative of a functionbtmathematics
Β 
Module of algelbra analyses 2
Module of algelbra analyses 2Module of algelbra analyses 2
Module of algelbra analyses 2Bui Loi
Β 
Notes on exact and approximate bayesian implementation
Notes on exact and approximate bayesian implementationNotes on exact and approximate bayesian implementation
Notes on exact and approximate bayesian implementationLeo Vivas
Β 

Similar to Can two fixpoint types in Mendler style unite? (20)

Optics Fourier Transform I
Optics Fourier Transform IOptics Fourier Transform I
Optics Fourier Transform I
Β 
Domain Transformations
Domain TransformationsDomain Transformations
Domain Transformations
Β 
Numerical Fourier transform based on hyperfunction theory
Numerical Fourier transform based on hyperfunction theoryNumerical Fourier transform based on hyperfunction theory
Numerical Fourier transform based on hyperfunction theory
Β 
Programmable PN Sequence Generators
Programmable PN Sequence GeneratorsProgrammable PN Sequence Generators
Programmable PN Sequence Generators
Β 
senior seminar
senior seminarsenior seminar
senior seminar
Β 
the fourier series
the fourier seriesthe fourier series
the fourier series
Β 
09. haskell Context
09. haskell Context09. haskell Context
09. haskell Context
Β 
Bound
BoundBound
Bound
Β 
Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskell
Β 
Some properties of m sequences over finite field fp
Some properties of m sequences over finite field fpSome properties of m sequences over finite field fp
Some properties of m sequences over finite field fp
Β 
Humble introduction to category theory in haskell
Humble introduction to category theory in haskellHumble introduction to category theory in haskell
Humble introduction to category theory in haskell
Β 
Application of derivatives
Application of derivativesApplication of derivatives
Application of derivatives
Β 
A Language Designer’s Workbench. A one-stop shop for implementation and verif...
A Language Designer’s Workbench. A one-stop shop for implementation and verif...A Language Designer’s Workbench. A one-stop shop for implementation and verif...
A Language Designer’s Workbench. A one-stop shop for implementation and verif...
Β 
5.5 Injective and surjective functions. A handout.
5.5 Injective and surjective functions. A handout.5.5 Injective and surjective functions. A handout.
5.5 Injective and surjective functions. A handout.
Β 
Signal Processing Homework Help
Signal Processing Homework HelpSignal Processing Homework Help
Signal Processing Homework Help
Β 
Iteration Techniques
Iteration TechniquesIteration Techniques
Iteration Techniques
Β 
160280102001 c1 aem
160280102001 c1 aem160280102001 c1 aem
160280102001 c1 aem
Β 
3.1 derivative of a function
3.1 derivative of a function3.1 derivative of a function
3.1 derivative of a function
Β 
Module of algelbra analyses 2
Module of algelbra analyses 2Module of algelbra analyses 2
Module of algelbra analyses 2
Β 
Notes on exact and approximate bayesian implementation
Notes on exact and approximate bayesian implementationNotes on exact and approximate bayesian implementation
Notes on exact and approximate bayesian implementation
Β 

Recently uploaded

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
Β 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
Β 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
Β 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
Β 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
Β 
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | DelhiFULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhisoniya singh
Β 
Integration and Automation in Practice: CI/CD in MuleΒ Integration and Automat...
Integration and Automation in Practice: CI/CD in MuleΒ Integration and Automat...Integration and Automation in Practice: CI/CD in MuleΒ Integration and Automat...
Integration and Automation in Practice: CI/CD in MuleΒ Integration and Automat...Patryk Bandurski
Β 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
Β 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
Β 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
Β 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
Β 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
Β 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
Β 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
Β 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
Β 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
Β 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
Β 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
Β 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
Β 

Recently uploaded (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
Β 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
Β 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
Β 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Β 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
Β 
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | DelhiFULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhi
Β 
Integration and Automation in Practice: CI/CD in MuleΒ Integration and Automat...
Integration and Automation in Practice: CI/CD in MuleΒ Integration and Automat...Integration and Automation in Practice: CI/CD in MuleΒ Integration and Automat...
Integration and Automation in Practice: CI/CD in MuleΒ Integration and Automat...
Β 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
Β 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
Β 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
Β 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
Β 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Β 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
Β 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Β 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Β 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
Β 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
Β 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
Β 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Β 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Β 

Can two fixpoint types in Mendler style unite?

  • 1. Mendler-style Recursion Schemes for Mixed-Variant Datatypes Ki Yung Ahn Tim Sheard Marcelo P. Fiore kya@cs.pdx.edu sheard@cs.pdx.edu Marcelo.Fiore@cl.cam.ac.uk TYPES 2013
  • 2. Outline Mendler-style Recursion Schemes mit (iteration) msfit (iteration with syntactic Inverse) Untyped HOAS (Regular Mixed-Variant Datatype) Formatting Untyped HOAS (using msfit at kind *) Simply-Typed HOAS (Indexed Mixed-Variant Datatype) Formatting Untyped HOAS (using msfit at kind * β†’ *) The Nax language Need for yet another Mendler-style Recursion Scheme
  • 3. already inconsistent as a logic (via Curry--Howard correspondence) without even having to introduce any term-level recursion since we can already define general recursion using this (iso-) Recursive Types in Functional Languages
  • 4. Having both unrestricted formation and unrestricted elimination of ΞΌ leads to inconsistency data T a r = C (r β†’ a) w : ΞΌ (T a) β†’ a -- encoding of (Ξ»x. x x) in the untyped Ξ»-calc w = Ξ»v. case (unIn v) of (C x) β†’ f (C x) -- w (C w) amounts to a well-known diverging term in Ξ»-calculus fix : (a β†’ a) β†’ a -- the Y combinator (Ξ»f.(Ξ»x.f(xx))(Ξ»x.f(xx)))) fix = Ξ»f. (Ξ»x. f (w x)) (In (C (Ξ»x. f (w x)))) To recover consistency, one could either restrict formation (conventional approach) or restrict elimination (Mendler-style approach)
  • 5. Conventional Iteration over Recursive Types allow formation of only positive recursive types (i.e., when F has a map) freely eliminate (i.e. use unIn) recursive values
  • 6. Mendler-style Iteration over Recursive Types elimination is possible only through mit freely form ANY recursive type!! note, no (ΞΌ-elim) rule
  • 7. Mendler-style Iteration -- Mu at different kinds (there are many more, one at each kind) data Mu0 (f :: * β†’ *) = In0 (f (Mu0 f )) data Mu1 (f :: (*β†’*)β†’(*β†’*)) i= In1 (f (Mu1 f ) i) -- Mendler-style iterators at different kinds mit0 :: Phi0 f a β†’ Mu f β†’ a mit0 phi (In0 x) = phi (mit0 phi) x mit1 :: Phi1 f a i β†’ Mu f i β†’ a i mit1 phi (In1 x) = phi (mit1 phi) x type Phi0 f = βˆ€r. (r β†’ a ) β†’ (f r β†’ a ) type Phi1 f = βˆ€r. (βˆ€i. r i β†’ a i) β†’ (βˆ€i. f r i β†’ a i) βœ” Uniformly defined at different kinds (can handle non-regular datatypes quite the same way as handling regular datatypes) βœ” Terminates for ANY datatype (can embed Mu and mit into Fw. see Abel, Matthes and Uustalu TCS'04) βœ” However, not very useful for mixed-variant datatypes eliminating In0 or In1 by pattern matching is only allowed in Mendler-style iterators recursive call
  • 8. Mendler-style Iteration with a syntactic inverse -- Mu' at different kinds (we only use two of them in this talk) data Mu'0 (f :: * β†’ *) a = In'0 (f (Mu'0 f a)) | Inverse0 a data Mu'1 (f :: (*β†’*)β†’(*β†’*)) a i = In'1 (f (Mu'1 f a) i)| Inverse1 (a i) -- msfit at different kinds msfit0 :: Phi'0 f a β†’ (βˆ€a. Mu'0 f a) β†’ a msfit0 phi (In'0 x) = phi Inverse0 (msfit0 phi) x msfit1 :: Phi'1 f a i β†’ (βˆ€a. Mu'1 f a i) β†’ a i msfit1 phi (In'1 x) = phi Inverse1 (msfit1 phi) x type Phi'0 f a = βˆ€r. (a β†’ r a ) β†’ (r a β†’ a ) β†’ (f r a β†’ a ) type Phi'1 f a = βˆ€r. (βˆ€i. a i β†’ r a i) β†’ (βˆ€i. r a i β†’ a i) β†’ (βˆ€i. f (r a) i β†’ a i) βœ” Terminates for ANY datatype (can embed Mu and msfit into Fw. see Ahn and Sheard ICFP'11) βœ” msfit is quite useful for mixed-variant datatypes, due to "inverse" operation in addition to "recursive call" eliminating In'0 or In'1 by pattern matching is only allowed in Mendler-style iterators recursive callinverse
  • 9. -- using general recursion at type level data Exp = Lam (Exp β†’ Exp) | App Exp Exp -- using fixpoint (Mu'0) over non-recursive base structure (ExpF) data ExpF r = Lam (r β†’ r) | App r r type Exp' a = Mu'0 ExpF a -- (Exp' a) may contain Inverse type Exp = βˆ€a . Exp' a -- Exp does not contain Inverse lam :: (βˆ€a. Exp' a β†’ Exp' a) -> Exp -- it's not (Exp β†’ Exp) β†’ Exp lam f = In'0 (Lam f ) -- f can handle Inverse containing values -- but can never examine its content app :: Exp β†’ Exp β†’ Exp app e1 e2 = In'0 (App e1 e2) Untyped HOAS (a regular mixed-variant datatype)
  • 10. Formatting Untyped HOAS using msfit0 at kind * showExp :: Exp β†’ String showExp e = msfit0 phi e vars where phi :: Phi'0 ExpF ([String] β†’ String) phi inv showE (Lam z) = Ξ»(v:vs) β†’ "(Ξ»"++v++"β†’"++ showE (z (inv (const v))) vs ++")" phi inv showE (App x y) = Ξ»vs β†’ "("++ showE x vs ++" "++ showE y vs ++")" type Phi'0 f a = βˆ€ r. (a β†’ r a) β†’ (r a β†’ a) β†’ (f r a β†’ a) vars = [ "x"++show n | n<-[0..] ] :: [String] recursive callinverse
  • 11. Simply-Typed HOAS -- using general recursion at type level data Exp t where -- Exp :: * β†’ * Lam :: (Exp t1 β†’ Exp t2) β†’ Exp (t1 β†’ t2) App :: Exp (t1 β†’ t2) β†’ Exp t1 β†’ Exp t2 -- using fixpoint (Mu'1) over non-recursive base structure (ExpF) data ExpF r t where -- ExpF :: (* β†’ *) β†’ (* β†’ *) Lam :: (r t1 β†’ r t2) β†’ ExpF r (t1 β†’ t2) App :: r (t1 β†’ t2) β†’ r t1 β†’ ExpF r t2 type Exp' a t = Mu'1 ExpF a t -- (Exp' a t) might contain Inverse type Exp t = βˆ€a . Exp' a t -- (Exp t) does not contain Inverse lam :: (βˆ€a . Exp' a t1 β†’ Exp' a t2) β†’ Exp (t1 β†’ t2) lam f = In'1 (Lam f ) -- f can handle Inverse containing values -- but can never examine its content app :: Exp (t1 β†’ t2) β†’ Exp t1 β†’ Exp t2 app e1 e2 = In'1 (App e1 e2) (a non-regular mixed-variant datatype)
  • 12. newtype Id a = MkId { unId :: a } evalHOAS :: Exp t β†’ Id t evalHOAS e = msfit1 phi e where phi :: Phi'1 ExpF Id {- v :: t1 -} {- f :: r Id t1 -> r Id t2 -} phi inv ev (Lam f ) = MkId(Ξ»v β†’ unId(ev (f (inv (MkId v))))) phi inv ev (App e1 e2) = MkId(unId(ev e1) (unId(ev e2))) type Phi'1 f a = βˆ€r. (βˆ€i. a i β†’ r a i) β†’ (βˆ€i. r a i β†’ a i) β†’ (βˆ€i. f (r a) i β†’ a i) This is example is a super awesome cooooool discovery that System Fw can express Simply-Typed HOAS evaluation!!! Evaluating Simply-Typed HOAS using msfit1 at kind *β†’* recursive callinverse
  • 13. The Nax language βœ”Built upon the idea of Mendler-style recursion schemes βœ”Supports an extension of Hindley--Milner type inference to make it easier to use Mendler-style recursion schemes and indexed datatypes βœ”Handling different notions of recursive type operators (Mu, Mu') still needs more rigorous clarification in the theory, but intuitively, Mu0 f = βˆ€a. Mu'0 f a Mu1 f = βˆ€a. Mu'1 f a i ... So, we hide Mu' from users as if there is one kind of Mu and Mu' is only used during the computation of msfit βœ”Supports term-indexed types as well as type-indexed types. Our term indexed calculus, System Fi (to appear in TLCA 2013), extends System Fw with erasable term indices.
  • 14. Need for yet another Mendler-style recursion scheme There are more recursion schemes other than mit and msfit E.g., Mendler-style primitive recursion (mpr) can cast from abstract recursive type (r) to concrete recursive type (Mu f ). Phi0 f a = βˆ€r. (r β†’ Mu f ) β†’ (r β†’ a ) β†’ (f r β†’ a ) Phi1 f a = βˆ€r. (βˆ€i. r i β†’ Mu f i) β†’ (βˆ€i. r i β†’ a i) β†’ (βˆ€i. f r i β†’ a i) With mpr, one can write constant time predecessor for natural numbers and tail function for lists by using the cast operation. Next example motivates an extension to mpr that can uncast from concrete recursive type (Mu f i) to abstract recursive type (r i) Phi1 f a = βˆ€r. (βˆ€i. Mu f i β†’ r i) β†’ (βˆ€i. r i β†’ Mu f i) β†’ (βˆ€i. r i β†’ a i) β†’ (βˆ€i. f r i β†’ a i) Recursion scheme with above Phi1 type does not terminate for mixed- variant datatypes -- needs some additional restriction on uncast.
  • 15. data V r t where V :: (r t1 β†’ r t2) β†’ V r (t1 β†’ t2) type Val t = Mu V t val f = In1 (V f ) evalHOAS :: Exp t β†’ Val t evalHOAS e = msfit1 phi e where phi :: Phi'1 ExpF (Mu1 V) -- f :: r Val t1 -> r Val t2 , v :: Val t1 phi inv ev (Lam f ) = val(Ξ»v β†’ ev (f (inv v))) phi inv ev (App e1 e2) = unVal (ev e1) (ev e2) Only if we had unVal :: Val (t1 β†’ t2) β†’ (Val t1 β†’ Val t2) it would be possible to write this, but unVal does not seem to be definable using any known Mendler-style recursion scheme. Evaluating Simply-Typed HOAS into a user defined value domain
  • 16. New recursion scheme to write unVal data V r t where V :: (r t1 β†’ r t2) β†’ V r (t1 β†’ t2) type Val t = Mu V t val f = In1 (V f ) unVal v = unId(mprsi phi v) where phi :: Phi1 V Id phi uncast cast (V f) = Id(Ξ»v. cast (f (uncast v))) mprsi :: Phi1 f a β†’ Mu f i β†’ a i mprsi phi (In1 x) = phi id id (mrpsi phi) x -- size restriction over indices in both uncast and cast type Phi1 f a = βˆ€ r j. (βˆ€i. (i < j) β‡’ Mu f i β†’ r i) β†’ -- uncast (βˆ€i. (i < j) β‡’ r i β†’ Mu f i) β†’ -- cast (βˆ€i. r i β†’ a i) β†’ (r j β†’ a j) -- or, maybe without the size restriction over indices in cast type Phi1 f a = βˆ€ r j. (βˆ€i. (i < j) β‡’ Mu f i β†’ r i) β†’ -- uncast (βˆ€i. r i β†’ Mu f i) β†’ -- cast (βˆ€i. r i β†’ a i) β†’ (f r j β†’ a j) Preliminary idea that still need further studies for the termination proof
  • 17. How are Mu and Mu' related? data Mu0 (f :: * β†’ *) = In0 (f (Mu0 f )) data Mu'0 (f :: * β†’ *) a = In'0 (f (Mu'0 f a)) | Inverse0 a type Phi0 f = βˆ€r. (r β†’ a ) β†’ (f r β†’ a ) mit0 :: Phi0 f a β†’ Mu f β†’ a mit0 phi (In0 x) = phi (mit0 phi) x type Phi'0 f a = βˆ€ r. (a β†’ r a) β†’ (r a β†’ a) β†’ (f (r a) β†’ a) msfit0 :: Phi'0 f a β†’ (βˆ€a. Mu'0 f a) β†’ a msfit0 phi (In'0 x) = phi Inverse0 (msfit0 phi) x Can we coerece from (βˆ€a. Mu'0 f a) to Mu f ? Yes!!! Can we coerece from Mu f to (βˆ€a. Mu'0 f a) ? Not exactly...
  • 18. Corecion from Mu' to Mu data E r = Lam (r -> r) | App r r type Expr = Mu0 E type Exp' a = Mu'0 E a type Exp = (forall a. Exp' a) -- .i.e (forall a. Rec0 E a) exp2expr :: Exp -> Expr -- i.e., (forall a. Rec0 E a) -> Mu0 E exp2expr = msfit0 phi where phi inv p2r (Lam f) = In0(Lam((x -> p2r (f (inv x))))) phi inv p2r (App e1 e2) = In0(App (p2r e1) (p2r e2))
  • 19. Attempt from Mu to Mu' msfit' :: (forall r. (a -> r a) -> (r a -> a) -> f (r a) -> a) -> Mu'0 f a -> a msfit' phi (In'0 x) = phi Inverse0 (msfit' phi) x msfit' phi (Inverse0 z) = z exp'2expr :: Exp' Expr -> Expr -- i.e., Mu'0 E (Mu0 E) -> Mu0 E exp'2expr = msfcata' phi where phi inv p2r (Lam f) = In0(Lam((x -> p2r (f (inv x))))) phi inv p2r (App e1 e2) = In0(App (p2r e1) (p2r e2)) -- using general recursion and pattern matching against In0 expr2exp' :: Expr -> Exp' Expr -- i.e., Mu0 E -> Mu'0 E (Mu0 E) expr2exp' (In0(Lam f)) = In'0(Lam (x -> expr2exp' (f (exp'2expr x)))) expr2exp' (In0(App e1 e2)) = In'0(App (expr2exp' e1) (expr2exp' e2))
  • 20. Why from Mu' to Mu possible but not the other way? data E r = Lam (r β†’ r) | App r r type Exp' a = Mu'0 ExpF a -- (Exp' a) may contain Inverse type Exp = βˆ€a . Exp' a -- Exp does not contain Inverse lam :: (βˆ€a. Exp' a β†’ Exp' a) -> Exp -- it's not (Exp β†’ Exp) β†’ Exp lam f = In'0 (Lam f ) -- f can handle Inverse containing values -- but can never examine its content type Expr = Mu0 E lam :: Expr β†’ Expr lam f = In0 (Lam f) Assume that Expr = Exp = (βˆ€a . Exp' a) There are less functions with type (βˆ€a. Exp' a) β†’ (βˆ€a. Exp' a) than functions with type (βˆ€a. Exp' a β†’ Exp' a)
  • 21. Thanks for listening. Questions or Suggestions? ===============