Skip to content

Instantly share code, notes, and snippets.

@noughtmare
noughtmare / LPS.hs
Created October 10, 2024 21:01
Longest palindromic substring (WIP)
{-# LANGUAGE PatternSynonyms, ViewPatterns #-}
{-# OPTIONS_GHC -Wall #-}
import Data.List
import Data.Ord
isPalindrome :: Eq a => [a] -> Bool
isPalindrome xs = xs == reverse xs
lps, lps' :: Eq a => [a] -> [a]
lps = maximumBy (comparing length) . filter isPalindrome . subsequences
@noughtmare
noughtmare / Gram5.hs
Last active August 18, 2024 09:59
Data dependent parsing with explicit variables
{- cabal:
build-depends: base, recover-rtti, tasty-bench, tasty-bench-fit
-}
{-# LANGUAGE GHC2021, GADTs, DataKinds, LambdaCase, TypeFamilies #-}
{-# OPTIONS_GHC -Wall #-}
-- DATA-DEPENDENT PARSING WITH VARIABLES
import Prelude hiding (Monad (..))
import Data.Char (intToDigit)
{-# LANGUAGE GHC2021, GADTs, DataKinds #-}
import Control.Monad (ap, (>=>), replicateM_, guard)
import Control.Applicative
import Prelude hiding (or)
import Data.Kind
import Data.Char
asumMap :: Alternative f => (a -> f b) -> [a] -> f b
asumMap f x = asum (map f x)
{-# LANGUAGE GHC2021, GADTs, DataKinds #-}
import Control.Monad (ap, (>=>))
import Control.Applicative
import Prelude hiding (or)
import Data.Kind
import Data.Char
type Ctx :: Type
type Ctx = [Type]
{- cabal:
build-depends: base, liquidhaskell == 0.9.6.*
-}
{- project:
allow-newer: bytestring
-}
{-# LANGUAGE GHC2021 #-}
{-# OPTIONS_GHC -Wall -fplugin=LiquidHaskell #-}
-- Liquid Haskell checks termination for us, but you can disable it if you want.
@noughtmare
noughtmare / 1-Parser.agda
Last active June 30, 2024 13:10
Total parser combinators in guarded cubical agda.
{-# OPTIONS --guarded --rewriting --cubical -WnoUnsupportedIndexedMatch #-}
module 1-Parser where
open import 2-Guarded
open import Data.Product hiding (_<*>_)
open import Data.Char hiding (_≤_)
open import Data.Bool hiding (_≤_)
open import Function
@noughtmare
noughtmare / murmurHash3_x64_128.hs
Last active March 14, 2024 00:19
Murmur3 x64 128 hash
{-# LANGUAGE MagicHash #-}
import GHC.Exts
import Foreign
import GHC.Word
import Control.Arrow ((>>>))
rotl64 :: Word64 -> Int -> Word64
rotl64 (W64# x#) (I# i#) = W64# ((x# `uncheckedShiftL64#` i#) `or64#` (x# `uncheckedShiftRL64#` (64# -# i#)))
@noughtmare
noughtmare / 1brc.hs
Last active March 14, 2024 11:36
1brc solution based on @Bodigrim's solution, but with linearly probed primitive hash table
#!/usr/bin/env cabal
{- cabal:
build-depends: base >= 4.19, bytestring, primitive >= 0.9, mmap
default-language: GHC2021
ghc-options: -Wall -O2 -fllvm
-}
{-# LANGUAGE ExtendedLiterals #-}
{-# LANGUAGE MagicHash, UnboxedTuples, UnliftedDatatypes #-}
import Data.ByteString (ByteString)
@noughtmare
noughtmare / AbstractMachine.hs
Created December 18, 2023 21:26
Sestoft Lazy Abstract Machine
import qualified Data.Map.Strict as Map
data Exp = Var String | Lam String Exp | App Exp String | Let String Exp Exp
| Con String [String] | Case Exp (Map.Map String ([String], Exp))
deriving Show
data State = State Heap Control Environment Stack deriving Show
data Heap = Heap !Int !(Map.Map String (Exp, Environment)) deriving Show
type Control = Exp
@noughtmare
noughtmare / StrictUntil.hs
Created November 8, 2023 23:07
Why no strict until'?
import Prelude hiding (until)
import Test.Tasty.Bench
{-# NOINLINE until #-}
until :: (a -> Bool) -> (a -> a) -> a -> a
until p f = go
where
go x | p x = x
| otherwise = go (f x)