Skip to content

Instantly share code, notes, and snippets.

{-# LANGUAGE DefaultSignatures, DeriveGeneric, TypeOperators, FlexibleContexts, StandaloneDeriving, ScopedTypeVariables, FlexibleInstances, DeriveFunctor, DeriveFoldable, DeriveTraversable #-}
module Main (
main
) where
import Data.Text (Text)
import qualified Data.Map as Map
import Data.Foldable (Foldable)
import Data.Array
import Data.Maybe
import Data.Ord
import Data.Foldable (toList)
import Data.Char
import Data.List
type Sudoku = Array Pos (Maybe Color)
type Pos = (Int, Int)
type Color = Int
@Cedev
Cedev / lazyUnfoldTree3-nomfix-delayed.hs
Created January 6, 2015 18:35
General lazy tree unfold with no MonadFix and delayed path compression
import Data.Tree hiding (unfoldTreeM_BF, unfoldForestM_BF)
import Data.Traversable
import Prelude hiding (sequence)
import Control.Monad.Free
import Data.Functor.Identity
unfoldTreeM_BF :: Monad m => (b->m (a, [b])) -> b -> m (Tree a)
@Cedev
Cedev / LLVM_General_Pure_PrettyPrint.hs
Created January 11, 2015 05:44
pretty print LLVM in in pure haskell (large unimplemented sections)
{-
Copyright (c) 2014 Stephen Diehl
Copyright (c) 2015 Cedric Shock
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
@Cedev
Cedev / prfCompiler.hs
Created January 11, 2015 05:47
Compiler from ArrowLike primitive recursive functions to LLVM
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ConstraintKinds #-}
@Cedev
Cedev / Control.PrimRec.hs
Last active August 29, 2015 14:13
Compiler from ArrowLike interface for primitive recursive functions to LLVM
module Control.PrimRec (
ArrowLike (..),
PrimRec (..),
module Control.Category,
module Data.Nat
) where
import Control.Category
import Data.Nat
import Control.Applicative
import Data.List
import Data.Maybe
data Term = Var Int | Lam Int Term | App Term Term deriving (Show)
data BTerm = BVar Int | BLam BTerm | BApp BTerm BTerm deriving (Show) -- BVar 0 refers to the closest lambda
lam2db :: Term -> Maybe BTerm
lam2db = go []
where
@Cedev
Cedev / distributions.hs
Created April 10, 2015 18:18
Calculate some interesting statistics about how hard it is to prove that dice aren't the normal distribution
import Statistics.Distribution
import Statistics.Distribution.Normal
data D n = D {n :: n, s :: n}
instance Integral n => Distribution (D n) where
cumulative d x = cumulativeD d (floor x)
instance Integral n => DiscreteDistr (D n) where
probability d x = probabilityD d (fromIntegral x)
deleteLast :: (a -> Bool) -> [a] -> [a]
deleteLast delete = snd . go
where
go [] = (False, [])
go (x:xs) = (delete x || deleteLater, if not (delete x) || deleteLater then x:xs' else xs')
where
(deleteLater, xs') = go xs
filterLast p = deleteLast (not . p)
@Cedev
Cedev / TypeMap.hs
Created September 15, 2015 03:00
A map from types to values indexed by those types
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RankNTypes #-}
module Data.TypeMap (
TypeMap (),
null,
size,
member,