Giter Club home page Giter Club logo

monae's Introduction

Monadic effects and equational reasoning in Coq

Docker CI

This Coq library contains a hierarchy of monads with their laws used in several examples of monadic equational reasoning.

Meta

Building and installation instructions

The easiest way to install the latest released version of Monadic effects and equational reasoning in Coq is via OPAM:

opam repo add coq-released https://coq.inria.fr/opam/released
opam install coq-monae

It installs two directories in coq/user-contrib: monae and monaeImpredicativeSet.

To instead build and install manually (with GNU make), do:

git clone https://github.com/affeldt-aist/monae.git
cd monae
make -j 4
make install

Overview

This repository contains a formalization of monads including examples of monadic equational reasoning and several models. This includes for example the formalization of the following papers:

  • [Gibbons and Hinze, Just do It: Simple Monadic Equational Reasoning, ICFP 2011] (except Sect. 10.2)
  • [Gibbons, Unifying Theories of Programming with Monads, UTP 2012] (up to Sect. 7.2)
  • [Mu, Equational Reasoning for Non-determinism Monad: A Case study of Spark Aggregation, TR-IIS-19-002, Academia Sinica]
  • [Mu, Calculating a Backtracking Algorithm: An exercise in Monadic Program Derivation, TR-IIS-29-003, Academia Sinica]
  • [Mu, Functional Pearls: Reasoning and Derivation of Monadic Programs, A case study of non-determinism and state, 2017]
    • This is a draft paper. In the first release, we formalized this draft up to Sect. 5. The contents have been since superseded by [mu2019tr2] and [mu2019tr3].
  • [Mu and Chiang, Deriving Monadic Quicksort (Declarative Pearl), 2020]

This library has been applied to other formalizations:

  • application to program semantics (see file smallstep.v)
  • formalization of monad composition [Jones and Duponcheel, Composing Monads, Yale RR 1993] (Sections 2 and 3)
  • formalization of monad transformers [Jaskelioff, Modular Monad Transformers, ESOP 2009] (up to Sect. 4)
    • completed with details from [Jaskelioff, Lifting of Operations in Modular Monadic Semantics, PhD 2009]
    • see directory impredicative_set for the formalization of [Jaskelioff, Modular Monad Transformers, ESOP 2009] (from Sect. 5)
  • formalization of the geometrically convex monad (main reference: [Cheung, Distributive Interaction of Algebraic Effects, PhD Thesis, U. Oxford, 2017])

Available monads

Files

  • basics:
    • monae_lib.v: simple additions to base libraries
    • hierarchy.v: hierarchy of monadic effects
    • category.v: formalization of concrete categories (generalization of the bottom layers of hierarchy.v)
    • monad_transformer.v: monad transformers
      • completed by ifmt_lifting.v and iparametricty_codensity.v in the directory impredicative_set
        • the directory impredicative_set contains a lighter version of Monae where monads live in Set and that compiles with impredicative-set
  • utility lemmas:
  • models of monads:
  • applications:
    • monad_composition.v: composing monads
    • smallstep.v: semantics for an imperative language, with equivalence operational semantics/denotation and sample imperative programs
    • example_*.v: various examples (Spark aggregation, the n-queens puzzle, tree relabeling, Monty Hall problem, monad transformers, quicksort, OCaml programs, etc.)

About Installation with Windows 10 or 11

Installation of monae on Windows is less simple. First install infotheo following the instructions for Windows 10. Once infotheo is installed (with opam), do:

  • opam install coq-monae or git clone [email protected]:affeldt-aist/monae.git; opam install .

Original License

Before version 0.2, monae was distributed under the terms of the GPL-3.0-or-later license

monae's People

Contributors

affeldt-aist avatar ayumusaito avatar celestinesauvage avatar davidnowak avatar garrigue avatar t6s avatar tzskp1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

monae's Issues

Adding monadic fixpoint to define Mu's quicksort

The definition of qperm function in the quicksort branch requires an interaction between fixpoints and binds to show its termination.
Currently there is no such mechanism, and it seems hard to finish the definition.

One possible solution is in http://leventerkok.github.io/papers/mfix.pdf : the monadic fixpoint operator mfix.
Let's see if this mfix can be added to monae with useful laws and if this enables quicksort.

non forgetful inheritance detected

monae/category.v

Lines 906 to 907 in 30808b4

HB.instance Definition _ := isMonad.Build C F bindE joinretM joinMret joinA.
(* TODO: eliminate Warning: non forgetful inheritance detected *)

monae/category.v

Lines 959 to 961 in 30808b4

HB.instance Definition monad_of_adjoint_mixin :=
isMonad.Build C M bindE join_left_unit join_right_unit join_associativity.
(* TODO: eliminate Warning: non forgetful inheritance detected *)

@t6s

Remove when available from analysis

monae/gcm_model.v

Lines 659 to 667 in 5bfc22a

Section move_to_classical_sets_ext.
Lemma eq_bigcup_cond :
forall (T U : Type) (P Q : set U) (X Y : U -> set T),
P = Q -> (forall i, P i -> X i = Y i) -> \bigcup_(i in P) X i = \bigcup_(i in Q) Y i.
Proof.
move=> ? ? P Q X Y pq XY.
by rewrite eqEsubset; split=> x; case=> j; rewrite -?pq=> ?; rewrite -?XY // => ?; eexists j; rewrite -?pq // -XY //.
Qed.
End move_to_classical_sets_ext.

A similar lemma should appear in analysis soon. Let us keep this one for a while until it is subsumed
instead of moving it to infotheo.

@t6s

notation broken

The notations on top of hierarchy.bind (>>=, the do notation) seem to be broken with HB.
They parse but do not always display.

typo in Record isMonadArray

In Record isMonadArray in hierarchy.v,

  agetpustskip : forall i, aget i >>= aput i = skip ;

this line has a typo: agetpustskip -> agetputskip ?

A shorter proof of image_preserves_convex_hull, using Fintype.fin_all_exists

(Put it here because I do not have writing rights, and not really worth forking for just that)

Lemma image_preserves_convex_hull (A B : finType) (f : {affine {dist A} -> {dist B}})
  (Z : set {dist A}) : f @` hull Z = hull (f @` Z).
Proof.
rewrite predeqE => b; split.
  case=> a [n [g [e [Hg]]]] ->{a} <-{b}.
  exists n, (f \o g), e; split.
    move=> b /= [i _] <-{b} /=.
    by exists (g i) => //; apply Hg; exists i.
  by rewrite affine_function_Sum.
case=> n [g [e [Hg]]] ->{b}.
suff [h Hh] : exists h : 'I_n -> dist_convType A, forall i, h i \in Z /\ f (h i) = g i.
  exists (\Sum_e h).
    exists n; exists h; exists e; split => //.
    move=> a [i _] <-.
    move: (Hh i) => [].
    by rewrite in_setE.
  rewrite affine_function_Sum; apply eq_convn => // i /=.
  by case: (Hh i).
apply (@fin_all_exists _ _ (fun i hi => hi \in Z /\ f hi = g i)) => i.
case: (Hg (g i)); first by exists i.
move=> a // HZa Hfa.
exists a; split; by rewrite // in_setE.
Qed.

Minimization of dependencies

First of all, I would like to thank all the contributors to this library!
For our project, we've been searching for a Coq library of monad theory and among few alternatives we chose monae.
Currently, we need just a basic stuff: basic hierarchy (functors, natural transformations, monads, nondet monads) and basic instances (list, state, etc). We've been really happy to find everything we need in monae.
However, a big list of library dependencies was a bit disappointing.

For example, a dependency on coq-infotheo was surprising, as well as dependencies on mathcomp-fingroup, mathcomp-solvable, etc. From what I understood after a quick pass over the sources, coq-infotheo is used mostly for the probability monad. The boolp.v library from mathcomp-analysis is used heavily for "classical" reasoning. I haven't understood how mathcomp-fingroup, mathcomp-field, etc are used.

Since the monad theory is a cornerstone of PL theory, it would be really nice to have a core library of monad theory with a minimalistic list of dependencies. If you'll be interested in achieving this goal I would be happy to help with this and prepare a PR.

I think there are two possible ways to achieve this:
(1) split the library into two: something like monae-core with a basic hierarchy of monads and common theory,
and e.g. monae-prob with the probability monad
(2) use opam's depopts feature and install various advanced features of the library (e.g. prob monad) only if the user already has all required packages installed.

reduce dependency wrt infotheo

by move=> ? ?; rewrite /bind classical_sets_ext.bigcup_of_singleton image_id.

bigcup_of_singleton and bigcup_image will be in the next version of mathcomp-analysis
and this dependency wrt infotheo can be removed

NB: bigcup_of_singleton became bigcup_of_set1

code duplicated in infotheo

monae/example_array.v

Lines 24 to 43 in 56087f1

(* TODO: Pr to infotheo *)
Local Open Scope zarith_ext_scope.
Reserved Notation "n %:Z" (at level 2, left associativity, format "n %:Z").
Definition natZ := nosimpl Z_of_nat.
Notation "n %:Z" := (natZ n) : zarith_ext_scope.
Notation "z .+1Z" := (Z.succ z) (at level 2, left associativity,
format "z .+1Z") : zarith_ext_scope.
Lemma add1Z z : (1 + z)%Z = z.+1Z.
Proof. by rewrite Z.add_1_l. Qed.
Lemma natZ0 : 0%:Z = 0%Z. Proof. exact: Nat2Z.inj_0. Qed.
Lemma natZS n : n.+1%:Z = n%:Z.+1Z.
Proof. by rewrite -Zpos_P_of_succ_nat. Qed.
Local Close Scope zarith_ext_scope.
(* TODO: Pr to infotheo (end) *)

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.