Skip to content

Instantly share code, notes, and snippets.

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 / 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
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
module Region76 where
-- Trick for expressing contexts like (∀ a. Binary a => Binary (f a))
--
-- This type class says that if we have dictionary for data type `a' we can
-- construct dictionary for `f a' or equivalently that if `a' is instance of
-- Binary `f a' is instance too.
class Binary2 f where
binaryDict :: BinaryDict a -> BinaryDict (f a)
data BinaryDict a where
BinaryDict :: Binary a => BinaryDict a
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Sum where
import Data.Vector.Fixed (S,Z,Fun(..))
import Data.Vector.Fixed.Cont (Fn,Arity(..))
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
module TySet where
type InsertRes x set = If (Member x set) set (x ': set)
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
@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
@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 / 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)