Skip to content

Instantly share code, notes, and snippets.

@noughtmare
noughtmare / Sudoku.agda
Last active November 10, 2024 22:24
Modeling correct-by-construction Sudoku in Agda
open import Data.Fin as Fin using (Fin ; #_ ; _≟_)
open import Data.Fin.Properties using (suc-injective)
open import Level as Level using (Level)
open import Data.Unit using (⊤ ; tt)
open import Data.Empty
open import Data.Product as Product
open import Data.Product.Properties as Product using (,-injective ; ≡-dec)
open import Data.Sum
open import Relation.Binary.PropositionalEquality
open import Data.Nat using (ℕ ; _+_)
@noughtmare
noughtmare / ProjectEuler1and2.hs
Created November 1, 2024 21:06
Solving project euler 1 and 2 in pure lambda calculus (embedded in Haskell)
newtype SNat = SNat { unSNat :: forall x. (SNat -> x) -> x -> x }
newtype CNat = CNat { unCNat :: forall x. (x -> x) -> x -> x }
newtype Quad a b c d = Quad { unQuad :: forall x. (a -> b -> c -> d -> x) -> x }
newtype BNat = BNat { unBNat :: forall x. (BNat -> x) -> (BNat -> x) -> x -> x}
instance (Show a, Show b, Show c, Show d) => Show (Quad a b c d) where
show q = unQuad q (\a b c d -> show (a,b,c,d))
instance Show CNat where
show cn = show (unCNat cn (+ 1) 0)
instance Show SNat where
@noughtmare
noughtmare / ProjectEuler1.hs
Last active November 2, 2024 13:21
Solving project euler 1 in pure lambda calculus (embedded in Haskell)
newtype SNat = SNat { unSNat :: forall x. (SNat -> x) -> x -> x }
newtype CNat = CNat { unCNat :: forall x. (x -> x) -> x -> x }
newtype Quad a b c d = Quad { unQuad :: forall x. (a -> b -> c -> d -> x) -> x }
newtype BNat = BNat { unBNat :: forall x. (BNat -> x) -> (BNat -> x) -> x -> x}
test =
let
ssuc = \x -> SNat (\s _ -> s x)
s0 = SNat (\_ z -> z)
s2 = ssuc (ssuc s0)
@noughtmare
noughtmare / THParser.hs
Last active October 28, 2024 20:17
Using Template Haskell instead of a parser.
import qualified Language.Haskell.TH.Syntax as TH
data ULC = Var String | Lam String ULC | App ULC ULC deriving (Show, TH.Lift)
quote :: TH.Exp -> Maybe ULC
quote (TH.VarE v) = Just $ Var (TH.nameBase v)
quote (TH.UnboundVarE v) = Just $ Var (TH.nameBase v)
quote (TH.AppE x y) = App <$> quote x <*> quote y
quote (TH.LamE vs x) = foldr (\v xs -> Lam <$> (case v of TH.VarP v' -> Just (TH.nameBase v'); _ -> Nothing) <*> xs) (quote x) vs
quote _ = Nothing
@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