Skip to content

Instantly share code, notes, and snippets.

View gergoerdi's full-sized avatar

Gergő Érdi gergoerdi

View GitHub Profile
; program 9digits
; Register layout:
;
; n1-n9 the digits
;
; div the divisor to check
; ndiv n[div]
; k
; nk n[k]
;
@gergoerdi
gergoerdi / gist:3376887
Created August 17, 2012 08:03
Untyped lambda-calculus
module Lambda where
open import Data.Nat
open import Data.Fin
data Exp : ℕ → Set where
Var : {n : ℕ} → Fin n → Exp n
Lam : {n : ℕ} → Exp (suc n) → Exp n
_·_ : {n m : ℕ} → Exp n → Exp m → Exp (n ⊓ m)
@gergoerdi
gergoerdi / gist:3376973
Created August 17, 2012 08:22
Untyped lambda-calculus (Haskell)
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
data Nat = Z | Suc Nat
data Fin n where
FZ :: Fin (Suc n)
FS :: Fin n -> Fin (Suc n)
inj :: Fin n -> Fin (Suc n)
inj FZ = FZ
@gergoerdi
gergoerdi / ReadableGeneratedNames.hs
Created October 1, 2012 16:06
Using Tardis to turn generated names into nicely readable ones
{-# LANGUAGE DataKinds, KindSignatures #-}
{-# LANGUAGE GADTs, StandaloneDeriving #-}
import Prelude hiding (mapM)
import Data.Traversable (mapM)
import Control.Monad.Tardis
import Control.Monad.Free
import Data.Stream (Stream(..))
import qualified Data.Stream as Stream
import Data.Set (Set)
import Data.Vect
%default total
permutations : Vect n a -> Vect (fact n) (Vect n a)
permutations [] = [[]]
permutations {n = S n} (x :: xs) = rewrite multCommutative (S n) (fact n) in
concat $ map (inserts x) (permutations xs)
where
inserts : a -> Vect k a -> Vect (S k) (Vect (S k) a)
import Data.Vect
import Data.Fin
%default total
fins : Vect n (Fin n)
fins {n = Z} = []
fins {n = S n} = FZ :: map FS fins
permutations : {n : Nat} -> Vect (fact n) (Vect n a -> Vect n a)
import Data.Vect
import Data.Fin
%default total
fins : Vect n (Fin n)
fins {n = Z} = []
fins {n = S n} = FZ :: map FS fins
Permutation : Nat -> Type
@gergoerdi
gergoerdi / gist:5324462
Created April 6, 2013 02:33
Trying to observe a difference between `pure id <*> x` and `x`
import Control.Applicative.Free
import Control.Applicative
import Control.Monad.Identity
import Data.Monoid
count :: (Functor eff) => Free eff a -> Int
count = getSum . runIdentity . analyze (const $ Identity $ Sum 1)
test :: (Functor eff) => Free eff a -> (Int, Int)
@gergoerdi
gergoerdi / Shapeshifting.agda
Last active December 18, 2015 07:16
Canonical representation of polymorphic functions over containers (http://stackoverflow.com/a/34346484/477476)
open import Data.Product
open import Function using (_∘_; id)
data Cont : Set₁ where
_<|_ : (S : Set) → (P : S → Set) → Cont
[_]C : Cont → Set → Set
[ S <| P ]C X = Σ[ s ∈ S ] (P s → X)
-- http://twistedoakstudios.com/blog/Post4706_breaking-a-toy-hash-function
import Data.List (findIndex)
import Control.Monad (guard)
import Data.Maybe (fromMaybe)
import Data.SBV.Bridge.Boolector
import Control.Monad.State
import Control.Applicative
codebook :: [Char]