-
Notifications
You must be signed in to change notification settings - Fork 8
church-encoded Free monad #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
829292b
47eae65
d9f2666
491e327
583b885
a018fac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| λ(f : Type → Type) → λ(a : Type) → ∀(r : Type) → (a → r) → (f r → r) → r | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| let Applicative = | ||
| https://raw.githubusercontent.com/FormationAI/dhall-bhat/8afbcee6dfd092764df9e9456fe2a2650748dce2/Applicative/Type | ||
|
|
||
| in λ(f : Type → Type) | ||
| → { ap = | ||
| λ(a : Type) | ||
| → λ(b : Type) | ||
| → λ(mf : ./Type f (a → b)) | ||
| → λ(mx : ./Type f a) | ||
| → λ(r : Type) | ||
| → λ(kp : b → r) | ||
| → λ(kf : f r → r) | ||
| → mf r (λ(ff : a → b) → mx r (λ(xx : a) → kp (ff xx)) kf) kf | ||
| , pure = | ||
| λ(a : Type) | ||
| → λ(x : a) | ||
| → λ(r : Type) | ||
| → λ(kp : a → r) | ||
| → λ(kf : f r → r) | ||
| → kp x | ||
| } | ||
| ∧ ./functor f | ||
| : Applicative (./Type f) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| let Monad = | ||
| https://raw.githubusercontent.com/FormationAI/dhall-bhat/8afbcee6dfd092764df9e9456fe2a2650748dce2/Monad/Type | ||
|
|
||
| in λ ( f | ||
| : Type → Type | ||
| ) | ||
| → λ(g : Type → Type) | ||
| → λ(monad : Monad g) | ||
| → let join = | ||
| ( https://raw.githubusercontent.com/FormationAI/dhall-bhat/6f66fc588c9d49a662794a32b29962341c373eb4/Monad/package.dhall | ||
| g | ||
| monad | ||
| ).join | ||
|
|
||
| in λ(a : Type) | ||
| → λ(n : ∀(x : Type) → f x → g x) | ||
| → λ(m : ./Type f a) | ||
| → m (g a) (monad.pure a) (λ(q : f (g a)) → join a (n (g a) q)) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably look similar to the |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| let Functor = | ||
| https://raw.githubusercontent.com/FormationAI/dhall-bhat/8afbcee6dfd092764df9e9456fe2a2650748dce2/Functor/Type | ||
|
|
||
| in λ(f : Type → Type) | ||
| → { map = | ||
| λ(a : Type) | ||
| → λ(b : Type) | ||
| → λ(g : a → b) | ||
| → λ(m : ./Type f a) | ||
| → λ(r : Type) | ||
| → λ(kp : b → r) | ||
| → λ(kf : f r → r) | ||
| → m r (λ(x : a) → kp (g x)) kf | ||
| } | ||
| : Functor (./Type f) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this can be λ(f : Type → Type)
→ ../../Mu/functor (../../FreeF/Type f) (../../FreeF/bifunctor f)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trying this out with the alternative representation; this doesn't quite work because it requires |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| λ(f : Type → Type) | ||
| → λ(g : Type → Type) | ||
| → λ(a : Type) | ||
| → λ(n : ∀(x : Type) → f x → g x) | ||
| → λ(m : ./Type f a) | ||
| → λ(r : Type) | ||
| → λ(kp : a → r) | ||
| → λ(kf : g r → r) | ||
| → m r kp (λ(q : f r) → kf (n r q)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| λ(f : Type → Type) | ||
| → λ(a : Type) | ||
| → λ(phi : f a → a) | ||
| → λ(m : ./Type f a) | ||
| → m a (λ(x : a) → x) phi |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| let Applicative = | ||
| https://raw.githubusercontent.com/FormationAI/dhall-bhat/8afbcee6dfd092764df9e9456fe2a2650748dce2/Applicative/Type | ||
|
|
||
| in λ(f : Type → Type) | ||
| → λ(g : Type → Type) | ||
| → λ(applicative : Applicative g) | ||
| → λ(a : Type) | ||
| → λ(phi : f (g a) → g a) | ||
| → λ(m : ./Type f a) | ||
| → m (g a) (applicative.pure a) phi |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| let Functor = | ||
| https://raw.githubusercontent.com/FormationAI/dhall-bhat/8afbcee6dfd092764df9e9456fe2a2650748dce2/Functor/Type | ||
|
|
||
| in λ(f : Type → Type) | ||
| → λ(functor : Functor f) | ||
| → λ(a : Type) | ||
| → λ(x : f a) | ||
| → λ(r : Type) | ||
| → λ(kp : a → r) | ||
| → λ(kf : f r → r) | ||
| → kf (functor.map a r kp x) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| let Monad = | ||
| https://raw.githubusercontent.com/FormationAI/dhall-bhat/8afbcee6dfd092764df9e9456fe2a2650748dce2/Monad/Type | ||
|
|
||
| in λ(f : Type → Type) | ||
| → { bind = | ||
| λ(a : Type) | ||
| → λ(b : Type) | ||
| → λ(m : ./Type f a) | ||
| → λ(g : a → ./Type f b) | ||
| → λ(r : Type) | ||
| → λ(kp : b → r) | ||
| → λ(kf : f r → r) | ||
| → m r (λ(x : a) → g x r kp kf) kf | ||
| } | ||
| ∧ ./applicative f | ||
| : Monad (./Type f) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| λ(f : Type → Type) | ||
| → { iter = | ||
| ./iter f | ||
| , iterA = | ||
| ./iterA f | ||
| , retract = | ||
| ./retract f | ||
| , wrap = | ||
| ./wrap f | ||
| , liftF = | ||
| ./liftF f | ||
| , foldFree = | ||
| ./foldFree f | ||
| } | ||
| ∧ ./monad f ⫽ ./transformer |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| let FreeF = ./../FreeF/Type | ||
|
|
||
| in λ(f : Type → Type) | ||
| → λ(a : Type) | ||
| → { cata = | ||
| λ(b : Type) | ||
| → λ(alg : ./../algebra (FreeF f a) b) | ||
| → λ(fa : ./Type f a) | ||
| → fa | ||
| b | ||
| (λ(x : a) → alg ((FreeF f a b).Pure x)) | ||
| (λ(x : f b) → alg ((FreeF f a b).Free x)) | ||
| } | ||
| : ./../Recursive/Type (./Type f a) (FreeF f a) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be λ(f : Type → Type)
→ λ(a : Type)
→ ../../Mu/recursive (../../FreeF/Type f a) |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| let Monad = | ||
| https://raw.githubusercontent.com/FormationAI/dhall-bhat/8afbcee6dfd092764df9e9456fe2a2650748dce2/Monad/Type | ||
|
|
||
| in λ ( f | ||
| : Type → Type | ||
| ) | ||
| → λ(a : Type) | ||
| → λ(monad : Monad f) | ||
| → let join = | ||
| ( https://raw.githubusercontent.com/FormationAI/dhall-bhat/6f66fc588c9d49a662794a32b29962341c373eb4/Monad/package.dhall | ||
|
mstksg marked this conversation as resolved.
Outdated
|
||
| f | ||
| monad | ||
| ).join | ||
|
|
||
| in λ(m : ./Type f a) → m (f a) (monad.pure a) (join a) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. λ(f : Type → Type)
→ λ(monad : Monad f) -- note instance immediately after type
→ λ(a : Type)
→ ./recursive.cata
a
( λ(free : ../../FreeF/Type f a (f a))
→ merge { Pure = monad.pure, Free = join } free)and the → ./recursive.cata (f a) (merge { Pure = monad.pure, Free = join })
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically, anything that applies |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| let Free = ./Type | ||
|
|
||
| let FreeF = ./../FreeF/Type | ||
|
|
||
| let Functor = | ||
| https://raw.githubusercontent.com/FormationAI/dhall-bhat/8afbcee6dfd092764df9e9456fe2a2650748dce2/Functor/Type | ||
|
|
||
| in λ(f : Type → Type) | ||
| → λ(functor : Functor f) | ||
| → λ(a : Type) | ||
| → let cata = (./recursive f a).cata | ||
|
|
||
| in let embed | ||
| : FreeF f a (Free f a) → Free f a | ||
| = λ(fm : FreeF f a (Free f a)) | ||
| → λ(r : Type) | ||
| → λ(kp : a → r) | ||
| → λ(kf : f r → r) | ||
| → merge | ||
| { Pure = | ||
| λ(x : a) → kp x | ||
| , Free = | ||
| λ(x : f (Free f a)) | ||
| → kf | ||
| ( functor.map | ||
| (Free f a) | ||
| r | ||
| (λ(y : Free f a) → y r kp kf) | ||
| x | ||
| ) | ||
| } | ||
| fm | ||
|
|
||
| in { embed = | ||
| embed | ||
| , project = | ||
| ./../lambek | ||
| (./Type f a) | ||
| (FreeF f a) | ||
| cata | ||
| embed | ||
| (./../FreeF/functor f functor a) | ||
| } | ||
| : ./../Steppable/Type (./Type f a) (FreeF f a) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. λ(f : Type → Type)
→ λ(a : Type)
→ ../../Mu/steppable (../../FreeF/Type f a) |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| let Monad = | ||
| https://raw.githubusercontent.com/FormationAI/dhall-bhat/8afbcee6dfd092764df9e9456fe2a2650748dce2/Monad/Type | ||
|
|
||
| in let Transformer = | ||
| https://raw.githubusercontent.com/FormationAI/dhall-bhat/8afbcee6dfd092764df9e9456fe2a2650748dce2/Transformer/Type | ||
|
|
||
| in { lift = | ||
| λ(m : Type → Type) | ||
| → λ(monad : Monad m) | ||
| → λ(a : Type) | ||
| → ./liftF m { map = monad.map } a | ||
| } | ||
| : Transformer ./Type |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| let Functor = | ||
| https://raw.githubusercontent.com/FormationAI/dhall-bhat/8afbcee6dfd092764df9e9456fe2a2650748dce2/Functor/Type | ||
|
|
||
| in λ(f : Type → Type) | ||
| → λ(a : Type) | ||
| → λ(functor : Functor f) | ||
| → λ(x : f (./Type f a)) | ||
| → λ(r : Type) | ||
| → λ(kp : a → r) | ||
| → λ(kf : f r → r) | ||
| → kf (functor.map (./Type f a) r (λ(m : ./Type f a) → m r kp kf) x) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| λ(f : Type → Type) → λ(a : Type) → λ(b : Type) → < Pure : a | Free : f b > | ||
|
mstksg marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| let Functor = | ||
| https://raw.githubusercontent.com/FormationAI/dhall-bhat/8afbcee6dfd092764df9e9456fe2a2650748dce2/Functor/Type | ||
|
|
||
| in λ(f : Type → Type) | ||
| → λ(functor : Functor f) | ||
| → λ(c : Type) | ||
| → { map = | ||
| λ(a : Type) | ||
| → λ(b : Type) | ||
| → λ(g : a → b) | ||
| → λ(m : ./Type f c a) | ||
| → merge | ||
| { Pure = | ||
| λ(x : c) → (./Type f c b).Pure x | ||
| , Free = | ||
| λ(x : f a) → (./Type f c b).Free (functor.map a b g x) | ||
| } | ||
| m | ||
| } | ||
| : Functor (./Type f c) | ||
|
mstksg marked this conversation as resolved.
Outdated
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should now look like
Also, it doesn’t exist on master yet, but I’ve started putting types like this (i.e., recursive types) into a “data/” directory, so this would then be “data/Free/Type”. There are some examples of this on the kind-polymorphism branch.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, even though the two types are the same, I think there's some benefit in separating the fixed-point from the base functor. Basically like how we have
List aandXNor, and we haveNaturalwithMaybe, and we haveNonEmptyandAndMaybe. And one benefit in implementingFreedirectly is that we can work directly withFreeas a "normal function", instead of importing an intermediate data type to use it.(edit: Although, now that I think about it,
mergedoesn't require importing constructor names, so it'd be possible to "make" aFreevalue without importingFreeF, but you'd need to importFreeFto use aFreevalue.)In the end it might just be a stylistic thing, and I think there could be benefits both ways.