Skip to content

Instantly share code, notes, and snippets.

@Solonarv
Solonarv / Timeout.hs
Last active March 6, 2019 18:05
Minimal timeout library for Haskell, depending only on base.
-- | A note: This is pretty much strictly worse than the functionality
-- offered by <hackage.haskell.org/package/async> . Use this library if
-- you cannot be bothered to depend on async.
module Timeout where
import Control.Concurrent
import Control.Exception
data TimeoutException = TimeoutException Int
instance Show TimeoutException where
@Solonarv
Solonarv / Dyck.hs
Last active March 5, 2019 01:11
Simple parser for the Dyck language https://en.wikipedia.org/wiki/Dyck_language
{-# LANGUAGE TypeFamilies #-}
module Dyck where
import Control.Applicative hiding (many)
import Control.Monad
import Data.Void
import Text.Megaparsec
import Text.Megaparsec.Char
newtype Dyck = Some [Dyck]
--
-- Is there a more efficient/elegant way to do this?
--
-- Objective: To disable/enable the mouse cursor for specific windows
--
checkMouseDisable :: X ()
checkMouseDisable = do
data Expr = Val Double | Op (Double -> Double -> Double) Expr Expr
binOpP :: Parser (e -> e -> e) -- ^ binary operator
-> Parser e -- ^ parser for operands
-> Parser e
binOpP opP elP = do
x <- elP
asum
[ do op <- opP; y <- binOpP opP elP; pure (op x y)
, pure x
@Solonarv
Solonarv / named-args.hs
Created January 25, 2019 15:05
Simple named arguments, without reordering.
{-# language OverloadedLabels #-} -- other extensions omitted
import Data.Kind
import GHC.OverloadedLabels
import GHC.TypeLits
infix 1 ::#
data NamedType = Symbol ::# Type
data Tag (t :: NamedType) where Tag :: Tag t
instance IsLabel s (Tag t) where
@Solonarv
Solonarv / vinyl-capability.hs
Last active January 10, 2019 03:28
Monadic capabilities using vinyl.
{-# language FunctionalDependencies #-}
{-# language KindSignatures #-}
{-# language DerivingVia #-}
{-# language ScopedTypeVariables #-}
{-# language TypeApplications #-}
-- some extensions elided, GHC should tell you what they are
module Capability where
import Control.Monad.Reader
import Control.Monad.State
@Solonarv
Solonarv / pointwise-vector.hs
Created December 24, 2018 16:21
Pointwise Semigroup lifting via MonadZip
newtype Pointwise f a = Pointwise { getPointwise :: f a }
deriving newtype (Functor, Applicative, Alternative, Monad, MonadPlus, MonadZip)
deriving stock (Eq, Ord, Show, Read)
instance (MonadZip f, Semigroup a) => Semigroup (Pointwise f a) where
Pointwise xs <> Pointwise ys = Pointwise (mzipWith (<>) xs ys)
-- Now Maybe (Pointwise (Vector (Sum a))) is the monoid you're looking for
@Solonarv
Solonarv / ST-via-state.hs
Created December 11, 2018 16:32
ST and State implemented in terms of each other.
import GHC.Exts (Any)
import Unsafe.Coerce (unsafeCoerce)
import Control.Monad.State
import Data.IntMap (IntMap)
import qualified Data.IntMap (IntMap)
-- Don't export /any/ of these constructors!
type role ST' nominal representative
@Solonarv
Solonarv / scotty-hello.hs
Last active November 30, 2018 20:54
Simple Hello World
#!/bin/env stack
-- stack script --resolver lts-12.16 --package scotty --install-ghc
{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty
main = scotty 80 $ do
get "/" $ do
html $ "Hello World!"
@Solonarv
Solonarv / Finite.hs
Last active November 21, 2018 23:10
Correctly representing the TO category.
module Finite where
-- | A simple natural number type
data N = Z | S N deriving (Eq, Ord)
-- | A simple length-indexed "vector"
data Vec n a where
VZ :: Vec Z a
VS :: a -> Vec n a -> Vec (S n) a