Skip to content

Instantly share code, notes, and snippets.

View edsko's full-sized avatar

Edsko de Vries edsko

View GitHub Profile
type family Repeat (n :: Nat) (a :: *) :: [*] where
Repeat 'Zero a = '[]
Repeat ('Succ n) a = a ': Repeat n a
fromHomSum :: SNat n -> NS f (Repeat n a) -> f a
fromHomSum SZero ns = case ns of {}
fromHomSum (SSucc _) (Z a) = a
fromHomSum (SSucc n) (S as) = fromHomSum n as
toHomProd :: [f a] -> (forall n. SListI (Repeat n a) => SNat n -> NP f (Repeat n a) -> b) -> b
module Blog where
import Prelude hiding ((.), id)
import Control.Category
import Data.Bifunctor
{-------------------------------------------------------------------------------
Fixed points
-------------------------------------------------------------------------------}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# OPTIONS_GHC -Wall -fno-warn-incomplete-patterns #-}
module HdWalletNotes where
{-------------------------------------------------------------------------------
This module is a simple Haskell model of BIP-32, with a focus on why and
how hardening works. We start with some types for which we need no further
@edsko
edsko / cabal-run.c
Last active August 14, 2018 08:13
Utility to find and run an executable in the `dist-newstyle` directory
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
int findUp(char* cwd, const char* needle, unsigned char typ);
void findDown(char* cwd, const char* needle, unsigned char typ, char* out);
@edsko
edsko / Collect.hs
Last active September 10, 2018 15:15
Applicative-only, spaceleak-free version of 'WriterT'
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
module Main where
import Data.Functor.Identity
import Data.Monoid
@edsko
edsko / DepFn.hs
Created October 23, 2018 14:06
Testing dependent functions with QuickCheck
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE TypeInType #-}
@edsko
edsko / callbacks.hs
Last active January 14, 2019 06:43
Alleviating callback hell in Haskell
{-------------------------------------------------------------------------------
Discussion of ContT in terms of callbacks
For an alternative exposition, see
<http://www.haskellforall.com/2012/12/the-continuation-monad.html>.
-------------------------------------------------------------------------------}
{-# OPTIONS_GHC -Wall #-}
import Control.Exception
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- | Error handling
--
-- Intended for qualified import
--
-- > import Ouroboros.Storage.Util.ErrorHandling (ErrorHandling(..))
-- > import qualified Ouroboros.Storage.Util.ErrorHandling as EH
-- | Convert 'Double' to fixed precision
--
-- For precision 'E1', we have
--
-- > 1.000 1.010 1.040 1.049 1.050 1.051 1.090 1.100
-- > floor | 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.1
-- > round | 1.0 1.0 1.0 1.0 1.0(*) 1.1 1.1 1.1
-- > ceiling | 1.0 1.1 1.1 1.1 1.1 1.1 1.1 1.1
--
-- (*): See <https://en.wikipedia.org/wiki/IEEE_754#Rounding_rules>
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# OPTIONS_GHC -Wall #-}
import Control.Monad.Reader
import Data.Coerce
import Test.QuickCheck.Gen
import Test.QuickCheck.Monadic