C#:
public static B Bracket<A, B>(Func<A> before, Action<A> after, Func<A, B> during);
Scala:
def bracket[A, B](before: => A, after: A => Unit)(during: A => B): B
C#:
public static B Bracket<A, B>(Func<A> before, Action<A> after, Func<A, B> during);
Scala:
def bracket[A, B](before: => A, after: A => Unit)(during: A => B): B
module Lens where | |
open import Level | |
open import Function | |
open import Relation.Binary | |
open import Data.Product | |
open import Relation.Binary.Product.Pointwise | |
open import Data.Sum | |
open import Relation.Binary.Sum | |
open import Data.Empty | |
open import Function.Inverse |
It’s often the case, when programming in Rust, that one needs to convert a value of one type to a corresponding value of another type. Some typical examples include:
Option
;I hereby claim:
To claim this, I am signing this object:
We've already looked at two different indexed monads in our tour so far, so let's go for a third whose regular counterpart isn't as well known: the privilege monad.
The regular privilege monad allows you to express constraints on which operations a given component is allowed to perform. This lets the developers of seperate interacting components be statically assured that other components can't access their private state, and it gives you a compile-time guarantee that any code that doesn't have appropriate permissions cannot do things that would require those permissions. Unfortunately, you cannot easily, and sometimes cannot at all, build code in the privilege monad that gains or loses permissions as the code runs; in other words, you cannot (in general) raise or lower your own privilege level, not even when it really should be a
Recently, Rust 1.26 was released, and with it came stable access to a heavily desired feature: impl Trait
.
For those unfamiliar, impl Trait
lets you write something like this:
fn foo<'a, A, B>(x: &'a mut A) -> impl Bar<'a, B> { ... }
and have it be translated to something like this:
The indexed state monad is not the only indexed monad out there; it's not even the only useful one. In this tutorial, we will explore another indexed monad, this time one that encapsulates the full power of delimited continuations: the indexed continuation monad.
The relationship between the indexed and regular state monads holds true as well for the indexed and regular continuation monads, but while the indexed state monad allows us to keep a state while changing its type in a type-safe way, the indexed continuation monad allows us to manipulate delimited continuations while the return type of the continuation block changes arbitrarily. This, unlike the regular continuation monad, allows us the full power of delimited continuations in a dynamic language like Scheme while still remaining completely statically typed.
/** | |
* You can encode Haskell's `RankNTypes` extension in terms of | |
* the much less immediately powerful `PolymorphicComponents` | |
* extension. Java has a direct analogue to | |
* `PolymorphicComponents`: method-level generics! | |
* | |
* This class is useful for the demonstration, but does not | |
* itself make use of higher-ranked types. It simulates | |
* Haskell's `(->)` type constructor. | |
*/ |
Have you ever had to write code that made a complex series of succesive modifications to a single piece of mutable state? (Almost certainly yes.)
Did you ever wish you could make the compiler tell you if a particular operation on the state was illegal at a given point in the modifications? (If you're a fan of static typing, probably yes.)
If that's the case, the indexed state monad can help!
Motivation
{-# OPTIONS --cubical-compatible --safe --postfix-projections #-} | |
module Fumula where | |
open import Level renaming (suc to ℓsuc; zero to ℓzero) | |
open import Function using (id) | |
open import Data.Product | |
open import Algebra | |
open import Algebra.Morphism | |
open import Relation.Binary | |
import Relation.Binary.Reasoning.Setoid as SetoidReasoning | |
import Algebra.Properties.Ring as RingProperties |