GithubHelp home page GithubHelp logo

cubical-free-monads's Introduction

Verified (cubical) free monads

An experiment in Cubical Agda.

License: MIT

Free monads are not legit monads

Free monads aren't real monads according to the monad laws. For example, for all monads m >>= return should equal m. However, in free monads, the data structure Bind m Return is clearly not equal to m.

In a sense, free monads are simply deferring the responsibility of satisfying the monad laws to the interpreter that actually runs Bind.

They can be in Cubical Agda

This is where Cubical Agda and Higher Inductive Types come in. With HITs, you can define you own equalities, and functions that pattern match on them have to prove that they respect that equality.

FreeReader, in FreeReader.agda, defines all of the monad laws as higher inductive paths:

-- The Reader monad, as a free monad
data FreeReader (R : Set) : Set  Set₁ where
  Pure : A  FreeReader R A
  Bind : FreeReader R A  (A  FreeReader R B)  FreeReader R B
  Ask : FreeReader R R

  -- Monad laws
  LeftId :  {A B}
     (a : A)
     (f : A  FreeReader R B)
     Bind (Pure a) f ≡ f a
  RightId :  {A}
     (m : FreeReader R A)
     Bind m Pure ≡ m
  Assoc :  {A B C}
     (m : FreeReader R A)
     (f : A  FreeReader R B)
     (g : B  FreeReader R C)
     Bind (Bind m f) g ≡ Bind m (λ x  Bind (f x) g)

Then, it can be proven to follow the functor, applicative functor, and monad laws.

(This is a lot better than without cubical type theory: usually, it's impossible to even prove the normal Reader monad is a functor, since that requires function extensionality.)

File layout

  • Class.agda has the Funtor, Applicative, and Monad classes, including all their laws
  • FreeReader.agda has FreeReader R's instances, including all the proofs that they're legit
  • Interpreter.agda has the runFree function, which runs FreeReader, and proves that it respects the monad laws

Non-ASCII Unicode used

Char   Emacs
 →      \r
 λ      \lambda
 ≡      \==
 ₁      \_1
 ₂      \_2
 ∘      \o
 ∀      \forall
 ∙      \.
 ⟨      \<
 ⟩      \>
 ℓ      \ell
 ′      \'1
 ″      \'2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.