neat trick

This commit is contained in:
Alexander 2025-06-16 19:51:07 -04:00
parent 64acf09214
commit 53154ec9b7
3 changed files with 11 additions and 6 deletions

View file

@ -21,7 +21,7 @@ module type Monad = sig
end
```
Here we have the usual OCaml signatures for functors, applicative functors, and monads respectively. The only thing of note is that I've written the functions in pipe-last[^pipe-last] style. It's more common to see pipe-first style, which agrees with the usual infix operators, but I don't see any of those around to get offended; do you?
Above, we have the usual OCaml signatures for functors, applicative functors, and monads respectively. The only thing of note is that I've written the functions in pipe-last[^pipe-last] style. It's more common to see pipe-first style, which agrees with the usual infix operators, but I don't see any of those around to get offended; do you?
```ocaml
module ApplicativeOfMonad (M : Monad) :
@ -43,7 +43,7 @@ module FunctorOfMonad (M : Monad) :
end
```
Each of these accepts an instance of a less general structure and uses only the elements the module provides to implement an instance of the more general structure.
Each of these accepts an instance of a less general structure and uses only the elements the module provides to implement an instance of the more general structure. The last one is boring, obviously, being just the composition of the former two.
It turns out that there are multiple ways to implement the derivation functors--- also multiple ways to implement a particular monad--- and they don't all behave the same. That makes it hard to predict whether the more-general, derived implementations are the "natural" ones that you expected to get without _ad hoc_ testing, which rather defeats the point of "gratis". On the other hand, the derivations here can be performed pretty mechanically, with little insight, by following the types in much the same way one might mechanically prove an easy proposition.
@ -53,7 +53,7 @@ The modules above that seem to have parameters, do; these modules are called "fu
A subtlety of the OCaml module system is that if a module is defined with a particular `module type` a.k.a. signature attached, e.g. `module M : S = struct...`, all the types that are abstract in the signature `S` will _also_ be abstract in the module itself. This means that the compiler can't see or be convinced that for some `F (M)` with `type t = M.t` in `F`, `M.t` and `(F (M)).t` are equal. This is because both types are abstract, meaning the underlying type is not available. To fix this, we can explicitly expose the equality by using the `with type` construct. In the above, `Functor with type 'a t = 'a M.t`--- for example--- exposes the equality of the two types, so that functions defined as expecting arguments of `'a t` can accept `'a M.t`, and _vice versa_.
[^falsehood]: Unsurprisingly, that's a lie. But isn't it always? You have to buy a monad first.
[^falsehood]: Unsurprisingly, that's a lie. Isn't it always? You have to buy a monad first.
[^1ml]: See [1ML](https://people.mpi-sws.org/~rossberg/1ml/1ml-jfp-draft.pdf) for an OCaml-like language without this stratification.

View file

@ -1,5 +1,10 @@
<h1><a href="/resume.pdf" style="text-decoration: none; color: var(--darkest-color)">acl</a>.cool</h1>
<h1>
<a href="/resume.pdf" style="text-decoration: none; color: var(--darkest-color)">acl</a><a
href="https://en.wikipedia.org/wiki/Special:Random"
style="text-decoration: none; color: var(--darkest-color)">.</a><a href="https://git.acl.cool/al/pages"
style="text-decoration: none; color: var(--darkest-color)">cool</a>
</h1>
<div id="writings" style="color: var(--lighter-color); line-height: var(--ui-spacing);"></span>
<p><strong>Welcome! Below are links to things I've made or just enjoy.</strong></p>
<p><strong>Welcome! Below are links to things I've made or just enjoy.</strong></p>

View file

@ -1,6 +1,6 @@
#! /usr/bin/env bash
if [[ $# -lt 1 ]] || ! { pygmentize -L lexers | grep -qw "$1"; }; then
if [[ $# -lt 1 ]] || ! pygmentize -L lexers | grep -qw "$1"; then
printf "<code>"
cat
printf "</code>"