Skip to content

Instantly share code, notes, and snippets.

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
module Region76 where
@Shimuuar
Shimuuar / jackknife.hs
Created June 15, 2014 19:31
Jackknife
import Data.Monoid
-- | Type class for monoidal accumulators
class Monoid m => Accumulator m a where
-- | Convert value to 1-element accumulator
unit :: a -> m
unit a = cons a mempty
-- | Prepend value to accumulator
cons :: a -> m -> m
cons a m = unit a <> m
import Debug.Trace
xs1,xs2 :: [Int]
xs1 = map (\x -> (trace "2" 2 + trace "3" 3) * x) [0..]
xs2 = map (let y = trace "2" 2 + trace "3" 3 in \x -> x*y) [0..]
{-
*Main> take 4 xs1
[2
3
0,2
@Shimuuar
Shimuuar / magick.hs
Created August 28, 2012 18:02 — forked from qnikst/magick.hs
Do magik
import Data.Default
import Debug.Trace
data Proxy a = Proxy
class Magick a where
magickStorage :: Proxy [a] -> String
instance Magick Int where
magickStorage = const "Int"
@Shimuuar
Shimuuar / gist:3501111
Created August 28, 2012 17:25
Do magik
class Magic a where
conjure :: String -> [a]
magikStorage :: [a] -> String
-- Use return type
doMagik :: Magic a => Int -> [a]
doMagik n = concat $ replicate n res
where
res = conjure $ magikStorage res -- Must be lazy in res !!!!
@Shimuuar
Shimuuar / Prime.hs
Created March 16, 2012 13:26 — forked from max630/MainCLI.hs
testing prime speed
{-# LANGUAGE NoMonomorphismRestriction, BangPatterns #-}
module Main where
-- compile: ghc --make -main-is Prime.main Prime.hs -O2
-- $time ./Prime i 500000
-- ./Prime i 500000 4.35s user 0.00s system 99% cpu 4.453 total
-- ./Prime s 500000 7.69s user 0.03s system 99% cpu 7.726 total (!!!)
-- lookup OPT in comments about various optimization points
import System.Environment(getArgs)
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE Rank2Types #-}
@Shimuuar
Shimuuar / output
Created August 14, 2011 12:55
withSystemRandom test case
$ ghc --make foo
[1 of 1] Compiling Main ( foo.hs, foo.o )
Linking foo ...
$ ./foo
"Starting IO"
Warning: Couldn't open "/dev/urandom"
Warning: using system clock for seed instead (quality will be lower)
"Starting IO"
foo: nonexitant: openFile: does not exist (No such file or directory)
@Shimuuar
Shimuuar / gist:1089228
Created July 18, 2011 11:19
GADT with set of allowed parameters
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeOperators #-}
-- Type constructor for list of types which is used in tests for
-- memberhip
data a ::: b
@Shimuuar
Shimuuar / test.hs
Created June 26, 2011 18:12
Benchmark of dispatch by constructor
import Criterion.Main
----------------------------------------------------------------
-- Unit types
data U10 =
U10_01
| U10_02
| U10_03
| U10_04