Skip to content

Instantly share code, notes, and snippets.

@LSLeary
LSLeary / Memo.hs
Last active December 26, 2025 18:15
Classic Ord-generic Memoisation
{-# LANGUAGE GHC2021, BlockArguments, ExplicitNamespaces, DerivingVia #-}
module Memo (
-- * Memoisation Functions
memo, memoFix,
-- * Memoised Function Type
type (-->),
toMemo, ($$),
@LSLeary
LSLeary / SelectiveBinds.hs
Last active December 17, 2025 05:24
Selective binds
bindIntegralS :: (Selective f, Integral a) => f a -> (a -> f b) -> f b
bindIntegralS fn k = pivot 0 (findLB (-1) 0) (findUB 0 1)
where
pivot !m = ifS $ fn <&> (< m)
findLB m ub = pivot m (findLB (m * 2) m) (bs m ub)
findUB lb m = pivot m (bs lb m) (findUB m (m * 2))
bs lb ub
| ub - lb <= 1 = k lb
| otherwise = pivot mid (bs lb mid) (bs mid ub)
where mid = (lb + ub) `div` 2
@LSLeary
LSLeary / ArchWS.hs
Created November 18, 2025 11:55
Arch-dependent coercions?
{-# OPTIONS_GHC -Wno-inaccessible-code -Wno-overlapping-patterns #-}
{-# LANGUAGE GHC2021, GADTs #-}
module ArchWS (ArchWS(..), archWS) where
-- base
import Data.Coerce (Coercible, coerce)
import Data.Type.Coercion (Coercion(..))
import Data.Word (Word32, Word64)
import Data.Int (Int32, Int64)
@LSLeary
LSLeary / Atom.hs
Last active November 12, 2025 14:59
A monad for scheduling IO actions within STM transactions
{-# LANGUAGE DerivingVia, BlockArguments #-}
module Atom (
Atom, atom, atom_,
stm, io, embed,
throwAtom, catchAtom,
) where
-- base
import Data.Monoid (Ap(..))
@LSLeary
LSLeary / Par.hs
Created October 18, 2025 01:52
Parallel Monoid/Applicative
{-# LANGUAGE DerivingVia #-}
module Par where
-- GHC/base
import GHC.Conc (par, pseq)
-- base
import Data.Monoid (Ap(..))
@LSLeary
LSLeary / Help.hs
Created September 19, 2025 07:22
Rebind xmonad's help command
module Help (helpAt) where
-- containers
import Data.Map.Strict (Map, (!))
import Data.Map.Strict qualified as M
-- xmonad
import XMonad hiding ((|||))
@LSLeary
LSLeary / Enumeration.hs
Last active December 29, 2025 01:39
Church-encoded lists for constant-space enumeration (experimental)
{-# LANGUAGE RankNTypes, TypeFamilies, LambdaCase, BlockArguments #-}
module Enumeration (
Enumeration(..),
fromFoldable,
) where
-- GHC/base
import GHC.Exts (build)
import GHC.IsList (IsList(..))
@LSLeary
LSLeary / PrevLayout.hs
Created September 11, 2025 16:38
xmonad: switch to the previous layout (experimental)
module PrevLayout (prevLayout) where
import XMonad
import qualified XMonad.StackSet as W
prevLayout :: X ()
prevLayout = do
ss@W.StackSet{ W.current = c@W.Screen{ W.workspace = ws }} <- gets windowset
mp <- findPrev (description (W.layout ws)) (W.layout ws)
case mp of
@LSLeary
LSLeary / Free.hs
Last active August 22, 2025 10:56
The Church-encoded Higher-Order Free Monad for f
{-# LANGUAGE RankNTypes, QuantifiedConstraints, BlockArguments, LambdaCase #-}
module Free where
-- base
import Data.Coerce (coerce)
type f ~> g = forall x. f x -> g x
@LSLeary
LSLeary / Heap.hs
Last active August 4, 2025 14:28
A heap on poset keys. Neither the performance nor stability are good, but I doubt we can do much better.
{-# LANGUAGE RoleAnnotations, DerivingStrategies, LambdaCase #-}
module Heap (
Poset(..), Prefix(..),
Heap, size,
empty, singleton,
insert, (<+>), fromList,
pop, assocs,
) where