juan_gandhi: (Default)
 which is applicative but not a monad (solution from stackoverflow)

link

juan_gandhi: (Default)
It's about the first example of algebras/coalgebras from http://okmij.org/ftp/tagless-final/course/lecture.pdf


So, we have a theory, or, rather a language (free theory, that is), looking e.g. like this:


<expr> :::= add(<expr>, <expr>)
<expr> := neg(<expr>)
<expr> := 1

As a functor, it is T(A) = {1} + A + A×A, so that an algebra is T(A) -> A, consisting of i: {1} -> A, f: A->A, m: A×A->A.

A free algebra for such a theory, aka "initial algebra", "least fixpoint" is the "set" of all possible expressions (that is, sentences in this language).
Or, not expressions, but their syntax trees, for some reason called "abstract" (AST).

Since Kiselyov had suggested a "biggest fixpoint", aka "final coalgebra", there is some kind of interest, how does that thing look like?

Suppose we did not have the binary operation m. Then the theory would consist of two constants and a unary operation; algebras would consist of i: {1} -> A and f: A->A. An initial algebra would be , with component-wise transitions, f0 and f1. You can find all the details in wikipedia, since we are essentially talking about the functor 1+A.
For that functor, the terminal coalgebra ("final") is ~, where ~ is the set of natural numbers with a point added, so that transition goes back, 1->0, 2->1, etc, while the new point, ω, remains intact. For an arbitrary coalgebra A -> T(A), consisting of a partial endomorphism f, an element x of A maps to such n that fn(x) == 1. We can call such an n a degree of x.

Now let's throw in the binary operation m: A×A->A. Initial algebra is still a set of expressions of our language. But a final coalgebra?

It is an infinite binary tree with  ~ at all nodes. An arbitrary coalgebra consists of two partial functions (with domains not intersecting): f': A⇸A, m': A⇸A×A. So for such a coalgebra, elements of A map to the following:

  - to number n if f' is defined on x and f'n(x) == 1;

 - to the pair of trees built for m'(x)∈A×A;

 - to number 0 if x == 1

The story behind these coalgebras being final fixpoints goes back to the category of adjoint pairs, but that's not important.

If you see how this thing works, you can, I guess, build all kind of final coalgebras for finite free theories (aka languages). All this, I think, is covered in the chapter "Model Theory" of Johnstone.

I'll be glad if you point out that something is not clear here and needs a clarification.

juan_gandhi: (Default)
Say, you have a monad M, there's a category CM of M-algebras, M[X] => X, with proper properties, and a category of M-coalbegras, X => M[X], with proper properties.
What are these coalgebras, X => M[X]? They are just objects of Kleisli category CM for the monad M. Definition? Same objects, but arrows of the form X => M[X].

That's it. So, a terminal ("final", as Kiselyov says) coalgebra is just a terminal object in CM.

E.g. for the functor 1+_ (which is a monad) in Sets, the Kleisli category consists of the same objects and partial functions; and, btw, the terminal object ("final coalgebra") is (see https://en.wikipedia.org/wiki/Initial_algebra#Final_coalgebra) ℕ∪{ω} with "pred" as the partial endomorphism.

Do you need details?


now what

Jun. 4th, 2020 04:49 pm
juan_gandhi: (Default)

Evaluation order, it's all about initial or "final" "algebras", right? That's about Eilenberg-Moore category and Kleisli category.

Which, incidentally, are initial and terminal objects in the category of adjunctions producing the same monad.

Oh my. 

juan_gandhi: (Default)
If you google it, you'll find tons of very weird things. (And I believe there's no such thing in, hmm, categories, as "monadic pair".) 
juan_gandhi: (Default)
I just figured out yesterday that yes, Tree[T] is a monad over T. Obvious, but. Same with BinaryTree[T]. Also, BinaryTree[T] does not seem to me applicative.

(My previous attempt to slap together a monad-looking functor were wrong.)  
juan_gandhi: (Default)
A friend just asked, what's a monad. She's a paleontologist.
Here's what I wrote.

If you write code, you have functions. A function takes an argument. But where do we have the arguments? Can be a list of arguments, an Optional argument, a Future argument, an argument depending on other arguments.
There is a way to generalize lifting a function to all these kinds of "argument inside". If our "argument provider" can take a function and produce something that takes a provider and returns a provider, the lifting operation is called `map`. If `map` is available, the provider is a functor.
Now imagine we have a function that returns another provider. So we have a provider inside a provider. It sucks, no way to reach the data. But! If there is a `flatten`, takes a provider of providers and produces a provider of values, it's better. And there's a symmetrical operation, take a value, wrap it into a provider. If these operations are reasonably balanced, we have a MONAD. This `data provider` with properties described above.

This is a programming aspect of monads; formally, they come from category theory. 
juan_gandhi: (Default)






  
  implicit class Optimist[T](opt: Result[T]) {
    def iHope: T = opt.fold(
      identity,
      errors => throw new InstantiationException(errors.mkString(";")))
   }



use case:

  def asCat(source: Category): Cat = convert2Cat(source)(_.toString, _.toString) iHope
 
juan_gandhi: (Default)

The Giry monad was originally developed by Lawvere in 1962, prior to the recognition of the explicit relationship between monads and adjunctions. It wasn’t until 1965 that the constructions of Eilenberg-Moore, and Kleisi, showed that every adjoint pair gives rise to a monad.

Lawvere’s construction was written up as an appendix to a proposal to the International Atomic Energy Commission. At that time, Lawvere was working for a “think tank’‘ in California, and the purpose of the proposal was to provide a means for verifying compliance with limitations on nuclear weapons. In the 1980’s, Giry was collaborating with another French mathematician at that time who was also working with the French intelligence agency, and was able to obtain a copy of the appendix. Giry then developed and extended some of the ideas in the appendix. 
juan_gandhi: (Default)
  public static <T> T headOrNull(Iterable<T> ts) {
    Iterator<T> it = ts.iterator();
    return it.hasNext() ? it.next() : null;
  }
juan_gandhi: (Default)
I get totally pissed off and call those people plain idiots. Kleisli monad...
juan_gandhi: (Default)
 https://vpatryshev.wordpress.com/2017/01/11/three-popular-fp-myths-2

That's James and Saunders bashing (in a generalized monoidal sense).
juan_gandhi: (Default)

List a = Codensity Endo a = forall r. (a -> r -> r) -> r -> r

nil :: List a
nil = \f z -> z

cons :: a -> List a -> List a
cons x xs = \f z -> f x (xs f z)

append :: List a -> List a -> List a
append xs ys = \f z -> xs f (ys f z)

foldr :: (a -> r -> r) -> r -> List a -> r
foldr f z xs = xs f z

Basically, it's like lambda.

Src: https://golem.ph.utexas.edu/category/2012/09/where_do_monads_come_from.html#c042100
juan_gandhi: (Default)
As promised (to myself), started writing the "three myths of FP".

https://vpatryshev.wordpress.com/2017/01/07/three-popular-fp-myths/
 - part 1, "not every monad is strong" 

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

June 2025

S M T W T F S
1 2345 6 7
8 9 10 11 121314
15161718 1920 21
22232425262728
2930     

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 26th, 2025 07:05 am
Powered by Dreamwidth Studios