Skip to content

Instantly share code, notes, and snippets.

View snowleopard's full-sized avatar

Andrey Mokhov snowleopard

View GitHub Profile
@copumpkin
copumpkin / Relations.agda
Created July 9, 2014 04:15
Relations form a semiring!
-- See also https://gist.github.com/copumpkin/2636229
module Relations where
open import Level
open import Function
open import Algebra
open import Data.Empty
open import Data.Sum as Sum
open import Data.Product as Prod
open import Relation.Binary
@jtobin
jtobin / foo.hs
Created February 15, 2016 21:39
Independence and Applicativeness
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE LambdaCase #-}
import Control.Applicative.Free
import Control.Monad
import Control.Monad.Free
import Control.Monad.Primitive
import System.Random.MWC.Probability (Prob)
import qualified System.Random.MWC.Probability as MWC
Definition associative {A:Type} (op: A -> A -> A) :=
forall x y z, op x (op y z) = op (op x y) z.
Definition commutative {A:Type} (op: A -> A -> A) :=
forall x y, op x y = op y x.
Definition is_unit_left {A:Type} (op: A -> A -> A) one :=
forall x, op one x = x.
Definition is_unit_right {A:Type} (op: A -> A -> A) one :=

I had a fun refactoring example in Haskell today I wanted to share. So, I've got a structure with a nested Maybe inside, which looked like this:

Maybe (Vector.Vector (Maybe (Direction, [Departure])))

I wanted to get that second-level Maybe folded into the first as it didn't provide any semantic meaning. So I start by writing the type definition:

@treeowl
treeowl / BasicNat.hs
Last active December 13, 2023 15:12
Fast total sorting of arbitrary Traversable containers
{-# LANGUAGE DataKinds, TypeFamilies, TypeOperators, GADTs,
ScopedTypeVariables, TypeOperators #-}
-- | Type-level natural numbers and singletons, with proofs of
-- a few basic properties.
module BasicNat (
-- | Type-level natural numbers
Nat (..)
, type (+)
{-# LANGUAGE Rank2Types, FunctionalDependencies, FlexibleInstances #-}
import Control.Monad (liftM, ap)
import qualified Data.Set as S
newtype SM a = SM {fromSM :: forall r. SetM a r => r}
class SetM a r | r -> a where
{-Instances of 'SetM' must satisfy the following laws:
* @'fromSet' s = 'fromList' $ 'Data.Set.toList' s@
@Profpatsch
Profpatsch / shake.hs
Created December 5, 2017 09:47
Abstraction in Shake
main = do
let buildDir = "_build"
-- how to express on a non-string level that everything should only ever be put into "_build"?
-- how to never repeat that information anywhere else, much less do problematic string operations
-- with the `"_build"` constant?
staticDir = "static"
staticFiles = map (staticDir <>) [ "foo" "bar" "baz" ]
(buildDir </> staticDir </> "*") %> \outFile ->
-- we only get `out`, which is some unknown string
@Profpatsch
Profpatsch / simple-types.nix
Last active March 31, 2018 18:10
Simple type system for nix expressions, with scalars, nested types, products, sums and unions.
# A simple type system to check plain nix values
# and get detailed error messages on type mismatch.
#
# Contains support for
# scalars (simple values)
# recursive types (lists of t and attrs of t)
# products (attribute sets with named fields)
# sums (tagged unions where you can match on the different cases)
# unions (untagged unions of the specified types)
# We can’t type functions (lambda expressions). Maybe in the future.
@Gabriella439
Gabriella439 / fibonacci.hs
Created March 25, 2018 00:52
Efficient fibonacci numbers using infinite precision integer arithmetic
import Numeric.Natural (Natural)
import qualified Data.Semigroup
-- | @fibonacci n@ computes the @nth@ fibonacci number efficiently using infinite
-- precision integer arithmetic
--
-- Try @fibonacci 1000000@
fibonacci :: Natural -> Natural
fibonacci n = x01 (Data.Semigroup.mtimesDefault n m)
@sjoerdvisscher
sjoerdvisscher / laws.hs
Last active July 24, 2018 08:16
First class checkable laws using the free-functors package
{-# LANGUAGE
TypeFamilies
, KindSignatures
, ScopedTypeVariables
, ConstraintKinds
, FlexibleInstances
, FlexibleContexts
, DeriveGeneric
, DeriveAnyClass
, TypeApplications