A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/Fuco1/emacs-cats below:

Fuco1/emacs-cats: Functors, Monads, Categories for Emacs

This package implements various abstractions from Category Theory highly inspired by Haskell.

The complete (Haskell) documentation can be found on the hackage.

Because Emacs runtime does not possess any abilities of type inference, we must explicitly pass the pure and return constructors to some functions where it is impossible to get from context. Other than this, the usage and interface is the same as the Haskell libraries.

The macro cats-do can be used to sequence monadic actions in an imperative way. Here is an example of a state monad:

(defclass my-state ()
  ((config-file
    :initarg :config-file
    :accessor get-config-file)
   (working-dir
    :initarg :working-dir
    :accessor get-working-dir)))

(defun my-exec-in-wd (cmd)
  "Prepare an action running CMD in current state's working directory."
  (cats-do
   ;; map get-working-dir over the internal state, save the result in
   ;; variable `wd'
   (:= wd (cats-fmap #'get-working-dir (cats-state-get)))
   ;; the first argument to return is only used to determine the
   ;; "instance" of return for state, because Elisp cannot determine
   ;; this automatically during runtime.
   (cats-return (cats-data-state)
     (with-temp-buffer
       (let ((default-directory wd))
         (shell-command-to-string cmd))))))

More comprehensive examples are in the examples directory.

Classes are implemented by defining one or several cl-defgeneric methods which need to be implemented (we call this an instance). For example, to make a list an instance of class Functor, it needs to implemment (cl-defmethod cats-fmap (fn (this list))) method.

Following is a list of classes and corresponding methods.

A type f is a Functor if it provides a function cats-fmap which, given any types a and b lets you apply any function from (a -> b) to turn an f a into an f b, preserving the structure of f.

Example: a list is a functor with cats-fmap equal to mapcar.

Minimal implementation of Functor is:

The following laws must be satisfied:

;; Identity
  (cats-fmap #'identity a)
= (identity a)

;; Composition
  (cats-fmap (lambda (x) (funcall f (funcall g x))) structure)
= (cats-fmap f (cats-fmap g structure))

Instances for built-in Elisp types:

Instances for types provided by Cats:

A functor with application, providing operations to embed pure expressions (cats-pure), and sequence computations and combine their results (cats-apply). Unlike Monads, Applicative application can not decide what to do next based on the result of previous action.

Each Applicative must also implement Functor.

Minimal implementation of Applicative is:

Additional methods:

Instances for built-in Elisp types:

Instances for types provided by Cats:

The Monad class defines the basic operations over a monad, an abstract datatype of actions. Unlike Applicative, Monad binding can decide what to do next based on the result of previous action.

Each Monad must also implement Applicative.

Minimal implementation of Monad is:

Additional methods:

The following laws must be satisfied:

;; Left identity
  (cats-bind (cats-return monad-type a) fn)
= (funcall fn a)

;; Right identity
  (cats-bind m (lambda (x) (cats-return monad-type x)))
= m

;; Associativity
  (cats-bind m (lambda (x) (cats-bind (funcall k x) h)))
= (cats-bind (cats-bind m k) h)

Instances for built-in Elisp types:

Instances for types provided by Cats:

The class of monoids (types with an associative binary operation that has an identity).

Minimal implementation of Monoid is:

Additional methods:

The following laws must be satisfied:

;; Right Identity
(cats-mappend a (cats-mempty a)) = a

;; Left Identity
(cats-mappend (cats-mempty a) a) = a

;; Associativity (semigroup)
  (cats-mappend a (cats-mappend b c))
= (cats-mappend (cats-mappend a b) c)

;; Concatenation
  (cats-mconcat list-of-a)
= (cats-foldr #'cats-mappend (cats-mempty item-of-a) list-of-a)

Instances for built-in Elisp types:

Instances for types provided by Cats:

The Foldable class represents data structures that can be reduced to a summary value one element at a time.

Minimal implementation of Foldable is:

Instances for built-in Elisp types:

Instances for types provided by Cats:

Functors representing data structures that can be transformed to structures of the same shape by performing an Applicative (or, therefore, Monad) action on each element from left to right.

Each Traversable must also implement Functor and Foldable.

Minimal implementation of Traversable is:

Instances for built-in Elisp types:

Instances for types provided by Cats:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4