Skip to content

Instantly share code, notes, and snippets.

@LSLeary
LSLeary / Transform.hs
Last active July 23, 2023 04:58
Deriving semidirect products for transformation monoids
{-# LANGUAGE DerivingVia, PatternSynonyms #-}
{-# LANGUAGE UndecidableInstances, MonoLocalBinds #-}
module Transform where
import Data.Functor ((<&>))
import Data.Monoid (Sum(..), Product(..), Ap(..))
test1a :: Transformable p s => Transform p s
@LSLeary
LSLeary / Local.hs
Created July 17, 2023 13:36
A "local" quasiquoter, such that [local|<name>|] = Current.Module.<name>.
module Local (local) where
import Data.Char (isUpper)
import Data.Functor ((<&>))
import Language.Haskell.TH.Syntax
( Q, Exp(VarE, ConE), Type(ConT)
, Module(..), Name(..), OccName(..), NameFlavour(NameQ)
)
import Language.Haskell.TH.Lib (thisModule)
@LSLeary
LSLeary / Triangles.hs
Created July 5, 2023 21:35
Fancy if-then-else with triangles!
module Triangles ((<|), (|>)) where
(<|) :: a -> Bool -> Maybe a
a <| True = Just a
_ <| False = Nothing
infix 2 <|
(|>) :: Maybe a -> a -> a
Just a |> _ = a
Nothing |> a = a
@LSLeary
LSLeary / Fresh.hs
Created June 21, 2023 14:06
Generate fresh Typeable types.
module Fresh
( Fresh, runFresh, withFresh
) where
import Data.Typeable
import Control.Monad.State (StateT, evalStateT, get, put)
import Control.Monad.Trans (MonadTrans)
@LSLeary
LSLeary / Sub.hs
Last active June 9, 2023 21:58
Parametrickery: Subtyping & Monotonicity
{-# LANGUAGE DataKinds #-}
module Sub where
data Sub = S Sub
data Three (s :: Sub) a b c where
One :: a -> Three s a b c
Two :: b -> Three (S s ) a b c
module Cube where
import Data.Functor.Const (Const(..))
import Data.Functor.Product (Product(..))
import Data.Functor.Sum (Sum(..))
type (&&) = Product
type (||) = Sum
@LSLeary
LSLeary / watchfile
Last active March 8, 2023 23:07
A reasonable approximation of ghcid for arbitrary files and interpreters, with nice scrolling and searching via less. Only needs inotifywait on the $PATH.
#! /usr/bin/env sh
# Example usage:
# $ watchfile Test.idr idris2 --check
# $ watchfile test.sh shellcheck
file=$1; shift
tmp="/tmp/watch.$(basename "$file")"
@LSLeary
LSLeary / IC.hs
Last active May 18, 2023 10:19
A thread-safe IC implementation supporting both eager parallel propagation and lazy demand driven evaluation.
{-# LANGUAGE GeneralisedNewtypeDeriving, DerivingVia #-}
{-# LANGUAGE BlockArguments, LambdaCase, NamedFieldPuns #-}
module Control.Concurrent.IC
( Adaptive, adaptively
, static, dynamic
, ICVar, newICVar, newICVarIO
, demand, propagate
@LSLeary
LSLeary / Strutless.hs
Last active January 25, 2023 05:05
Strut-avoiding scratchpads?
module Strutless where
import XMonad
import XMonad.StackSet (RationalRect)
import qualified XMonad.StackSet as W
import Data.Set (Set)
import XMonad.Util.Types (Direction2D)
import XMonad.Util.Rectangle (toRatio)
import XMonad.Util.NamedScratchpad (customFloating)
@LSLeary
LSLeary / write-pr
Last active December 20, 2022 04:00
Write good commit messages, then let a script write your PR for you—or at least the bulk of it.
#! /usr/bin/env sh
# Configuration: where PRs are written
prdir=$HOME/PRs
# Argument: the git rev or ref upon which the PR is based
base=$1
repo=$(basename "$(git rev-parse --show-toplevel)")