Skip to content

Instantly share code, notes, and snippets.

@LSLeary
LSLeary / AnnotatedArrows.hs
Created September 14, 2026 11:52
Type annotated composition arrows!
{-# LANGUAGE RequiredTypeArguments #-}
module AnnotatedArrows (
(<--<), (>--),
(--<), (>-->),
) where
infixr 9 <--<, >--
(<--<) :: (b -> c) -> (a -> b) -> a -> c
@LSLeary
LSLeary / Control.Monad.Trans.Masked.hs
Last active September 12, 2026 07:15
Composable Masking
{-#
LANGUAGE
RankNTypes, DataKinds,
RoleAnnotations, StandaloneKindSignatures,
DerivingVia, BlockArguments
#-}
module Control.Monad.Trans.Masked (
Masked,
Interruptibility(..),
@LSLeary
LSLeary / Fresh.hs
Last active August 11, 2026 09:00
Unique type generation via linearity
{-# OPTIONS_GHC -Wno-unused-foralls #-}
{-#
LANGUAGE
UnliftedNewtypes, UnboxedTuples, DataKinds, LinearTypes, TypeFamilies,
RoleAnnotations, RequiredTypeArguments, LiberalTypeSynonyms
#-}
module Fresh (
Source, create, destroy,
type ( # ), fresh,
{-# LANGUAGE GADTs, PatternSynonyms, LambdaCase, BlockArguments #-}
module Traced (
Traced(..),
catchTraced,
trace,
) where
-- base
import Type.Reflection (typeOf, pattern App)
@LSLeary
LSLeary / Apart.hs
Last active April 5, 2026 00:52
Transform a functional decision to boolean in/equality
{-#
LANGUAGE
GHC2021, GADTs, DataKinds, ExplicitNamespaces, BlockArguments, LambdaCase
#-}
module Apart (apart, equal, boolean) where
import Data.Void (Void)
import Data.Type.Equality ((:~:)(..), type (==))
import Data.Bifunctor
@LSLeary
LSLeary / Stack.hs
Last active March 5, 2026 16:35
Funny stacks
{-#
LANGUAGE
GHC2021, BlockArguments, DataKinds, TypeFamilies, RequiredTypeArguments
#-}
module Stack (
Stack, run,
push, dup, rot,
apply, plus, minus, times,
) where
@LSLeary
LSLeary / Is.hs
Last active January 24, 2026 09:45
Match on type
{-#
LANGUAGE
GHC2021, PatternSynonyms, ViewPatterns, ExplicitNamespaces, GADTs
#-}
module Is (pattern Is) where
-- base
import Type.Reflection
import Data.Type.Equality (type (~~))
@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)