Skip to content

Instantly share code, notes, and snippets.

@noughtmare
noughtmare / ReifiedEmbeddedAbstractSyntax.hs
Last active April 11, 2023 15:09
Convenient embedded abstract syntax for Haskell.
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# OPTIONS_GHC -Wall #-}
import Data.Reify ( reifyGraph, MuRef(..), Graph(..))
import Data.Function (fix)
import Data.IntMap.Strict qualified as Map
import Data.Foldable ( Foldable(toList) )
import Data.Bifunctor ( Bifunctor(second) )
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE DerivingVia #-}
@noughtmare
noughtmare / ApplicativeObservableSharing.hs
Last active April 6, 2023 12:27
A prototype of parser combinators which support left recursion.
{-# LANGUAGE ExplicitNamespaces #-}
{-# LANGUAGE GADTs #-}
import Control.Applicative
import Data.Type.Equality ( type (:~:)(..) )
import Unsafe.Coerce ( unsafeEqualityProof, UnsafeEquality(UnsafeRefl) )
import Control.Monad
newtype Name a = Name ID
type ID = Int
{-
ORIGINAL LICENSE:
Copyright Stéphane Laurent (c) 2023
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
@noughtmare
noughtmare / GenerateExponents.hs
Last active February 6, 2023 22:15
Generate Exponents
import Control.Applicative
import Data.List (insert, group)
import Test.Tasty.Bench
import Control.DeepSeq
-- only works on infinite lists
-- op must be lazy in its second argument
foldb1 :: (a -> a -> a) -> [a] -> a
foldb1 op = foldb' where
pairwise ~(x:y:rest) = op x y : pairwise rest
@noughtmare
noughtmare / EffeGS.hs
Created December 2, 2022 09:24
A symmetric effect system inspired by game semantics.
{-# LANGUAGE TypeOperators, MultiParamTypeClasses, FlexibleContexts, FlexibleInstances, LambdaCase, DeriveFunctor, DataKinds, GADTs, StandaloneDeriving, NoStarIsType, TypeFamilies, StandaloneKindSignatures, PolyKinds #-}
{-# OPTIONS_GHC -Wno-unticked-promoted-constructors #-}
module EffeGS where
-- TODO:
-- * Answer-type modification
-- * Higher order effects
-- * Cas: the sum/product duality is still asymmetric
-- solution: session types?
@noughtmare
noughtmare / All.hs
Last active September 27, 2022 10:27
Standalone reproducer of missed fusion in vector streams without Skip
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE RoleAnnotations #-}
module All (test) where
import Data.Kind
import Control.Monad.ST
import Control.Monad.Primitive
@noughtmare
noughtmare / type_application_regex
Created March 22, 2022 10:34
A regular expression for finding occurrences of type applications in Haskell code.
^[^-]*(-[^-]+)*(-|[^A-Za-z])either @[(A-Za-z][^@ )]*[ )]
@noughtmare
noughtmare / PackFusion.hs
Last active March 6, 2022 15:24
An alternative implementation of `pack` for bytestrings that can fuse.
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
module Main (main) where
import Test.Tasty.Bench
import qualified Data.ByteString as S
import qualified Data.ByteString.Internal as SI
@noughtmare
noughtmare / PrimMonad.bkp
Created September 12, 2021 10:04
Translation of the PrimMonad class to a Backpack signature with example modules that implement it.
-- vi:syntax=haskell
{-# LANGUAGE UnboxedTuples, MagicHash, TypeFamilies, StandaloneKindSignatures, RankNTypes, AllowAmbiguousTypes, ConstraintKinds, UndecidableInstances #-}
unit primmonad-indef where
signature PrimMonad where
import Data.Kind
import GHC.Prim
type C :: (* -> *) -> * -> Constraint
type family C m s
type PrimMonad m s = (Monad m, C m s)
primitive :: PrimMonad m s => (State# s -> (# State# s, a #)) -> m a