Skip to content

Instantly share code, notes, and snippets.

View pthariensflame's full-sized avatar
🌈
💛🤍💜🖤

Laine Taffin Altman pthariensflame

🌈
💛🤍💜🖤
View GitHub Profile
@pthariensflame
pthariensflame / Fumula.agda
Last active August 14, 2023 05:45
Rings characterized by a fused multiply-add operator and negative one
{-# 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

impl Trait is Always Existential

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:

Keybase proof

I hereby claim:

  • I am pthariensflame on github.
  • I am pthariensflame (https://keybase.io/pthariensflame) on keybase.
  • I have a public key ASAWo3m0yLUuPWsOW4wNjNGe2eHXFE8mWliCPzgMeFtAbQo

To claim this, I am signing this object:

Rust’s Standard Conversion Traits

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:

  • borrowing a vector as a slice;
  • converting a Rust-style string to a C-style string;
  • embedding an existing value in an Option;
  • getting an iterator from a collection;
  • rendering a value of a structured type as a string;
@pthariensflame
pthariensflame / Lens.agda
Last active December 30, 2015 17:39
Residual representations of `Lens` and `PLens` in Agda, and the canonical embedding of the former into the latter.
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
@pthariensflame
pthariensflame / IndexedPrivilege.md
Last active November 7, 2019 10:58
An introduction to the indexed privilege monad in Haskell, Scala and C#.

The Indexed Privilege Monad in Haskell, Scala, and C#

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.

Motivation

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

@pthariensflame
pthariensflame / BracketInManyLanguages.md
Last active December 18, 2015 14:49
The signature of the `bracket` function in C#, Scala, Kotlin, Ceylon, Haskell, Agda, and Spellcode.

C#:

public static B Bracket<A, B>(Func<A> before, Action<A> after, Func<A, B> during);

Scala:

def bracket[A, B](before: =&gt; A, after: A =&gt; Unit)(during: A =&gt; B): B
@pthariensflame
pthariensflame / IndexedCont.md
Last active April 3, 2022 00:30
An introduction to the indexed continuation monad in Haskell, Scala, and C#.

The Indexed Continuation Monad in Haskell, Scala, and C#

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.

Motivation

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.

@pthariensflame
pthariensflame / IndexedState.md
Last active June 15, 2022 18:42
An introduction to the indexed state monad in Haskell, Scala, and C#.

The Indexed State Monad in Haskell, Scala, and C#

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

@pthariensflame
pthariensflame / higherkindedtypes.java
Last active May 2, 2022 08:05
Higher-Ranked types in Java
/**
* 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.
*/