Skip to content

Instantly share code, notes, and snippets.

@Solonarv
Solonarv / prism_when.hs
Created November 20, 2018 15:51
Generalization of 'when' to work with any Prism
{-# LANGUAGE TemplateHaskell #-}
import Control.Lens.TH
import Control.Lens.Prism
whenP :: Applicative m => APrism s t a b -> s -> (a -> m ()) -> m ()
whenP p s act = case matching p s of
Left _ -> pure ()
Right a -> act a
whenP' :: Applicative m => APrism s t a b -> a -> m () -> m ()
@Solonarv
Solonarv / liftA2-assoc.hs
Created November 20, 2018 14:21
Associativity of liftA2
-- op :: a -> a -> a
-- `op` associative
-- We wish to prove that 'liftA2 op' is associative.
-- x, y, z :: a
-- f such that Applicative f
-- fx, fy, fz :: f a
(op x . op y) z
@Solonarv
Solonarv / DAlGa.hs
Created November 19, 2018 19:52
DAG in the style of algebraic-graphs.
module DAlGa where
import Data.Foldable
import qualified Data.Set as S
import qualified Data.Map.Strict as M
-- Note: this is identical to algebraic-graphs' Graph type,
-- but has different semantics in that 'Connect' is not commutative.
data DAG v = Empty | Vertex v | Connect (DAG v) (DAG v) | Overlay (DAG v) (DAG v)
module DAG where
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
data DAG a = DAG { dagValues :: IntMap a, dagEdges :: IntMap IntSet }
type NodeIndex = Int
@Solonarv
Solonarv / rpn.hs
Created November 18, 2018 00:46
Simple RPN calculator. Takes the program from command-line arguments.
module Main where
import System.Environment (getArgs)
import Control.Applicative
import Data.Maybe
import Text.Read
main :: IO ()
main = do
program <- parse . unwords <$> getArgs
-- | Run an action, discarding any changes it makes to the state
locally :: MonadState s m => m a -> m a
locally act = get >>= \old -> act >> put old
-- Example:
foo = do
results <- locally $ do
modify ([args] :)
traverse statementBuilder exprs
use results
{-# LANGUAGE DerivingStrategies #-}
import Data.HashMap.Strict -- package unordered-containers
import Data.Hashable -- package hashable
import Data.Int
import GHC.Generic
-- Attempting to model factorio's circuit system
data Channel = Wooden_Chest | Iron_Chest | Steel_Chest | Storage_Tank
@Solonarv
Solonarv / SKI.hs
Last active October 31, 2018 20:55
module SKI where
data SKI = S | K | I | A SKI SKI
deriving (Eq, Show)
reduce :: SKI -> SKI
reduce (A (A (A S x) y) z) = reduce (A (A x z) (A y z))
reduce (A (A K x) y) = reduce y
reduce (A I x) = reduce x
reduce a@(A f x) = let f' = reduce f in if f==f' then a else reduce (A f' x)
module Machine.Turing where
data Stream a = a :| Stream a
data Tape a = Tape (Stream a) a (Stream a)
data Move = GoLeft | Stay | GoRight
applyMove :: Move -> Tape a -> Tape a
applyMove GoLeft (Tape (l :| ls) x rs) = Tape ls l (x :| rs)
{-# LANGUAGE DerivingStrategies, LambdaCase #-}
module Bijection (type Bijection, swap) where
import Data.Semigroup
import Data.Monoid
newtype Bijection a = Bijection { appBijection :: a -> a }
deriving (Semigroup, Monoid) via (Endo a)