Skip to content

Instantly share code, notes, and snippets.

View gelisam's full-sized avatar

Samuel Gélineau gelisam

View GitHub Profile
@gelisam
gelisam / Main.hs
Created February 5, 2014 02:15
A (contrived) use for NullaryTypeClasses
{-# LANGUAGE NullaryTypeClasses, Rank2Types #-}
import Prelude hiding (log)
import Unsafe.Coerce
import System.IO.Unsafe
class NeedsInit
provideInit :: (NeedsInit => a) -> (() -> a)
provideInit = unsafeCoerce
@gelisam
gelisam / Main.hs
Created March 3, 2014 13:58
ExistentialQuantification example
-- in reply to http://www.reddit.com/r/haskell/comments/1zf3ay/trying_to_design_a_gui_library/
{-# LANGUAGE ExistentialQuantification #-}
import Text.Printf
type Key = String
data Widget s = Widget
@gelisam
gelisam / Main.hs
Created March 9, 2014 17:59
Example Haskell implementation requested by SrPeixinho.
-- In response to [1].
--
-- I am aiming for readability, not conciseness. Nevertheless, a note
-- explaining why the original javascript code is so much shorter:
-- Javascript skips all error checking unless you perform explicit checks.
-- Haskell enforces all error checking unless you explicitly ignore errors.
-- I have thus implemented many wrapping functions whose purpose is to use
-- incomplete pattern-matching to ignore errors.
--
-- I am using the aeson package for JSON and the download-curl package for
@gelisam
gelisam / Main.hs
Created March 11, 2014 04:45
Typed terms with type promotion and "type categories", in reply to https://groups.google.com/forum/#!topic/haskellers-montreal/cmn24NLaAgU
{-# LANGUAGE GADTs, StandaloneDeriving, TypeFamilies, MultiParamTypeClasses #-}
module Main where
import Data.Word
type family Promote a b where
Promote Word8 Word16 = Word16
Promote Word16 Word8 = Word16
Promote a a = a
@gelisam
gelisam / Main.hs
Created March 15, 2014 03:16
Example of using Bound with a non-monad
-- A small imperative language with a single anonymous mutable variable.
-- In response to http://www.reddit.com/r/haskell/comments/207mcn/binding_type_variables_using_the_bound_library/
{-# LANGUAGE DeriveFunctor #-}
module Main where
import Control.Monad.Trans.Class
import Control.Monad.Trans.State
import Bound
@gelisam
gelisam / everything.h
Created April 5, 2014 14:59
Many, many valid C declarations
// primitive types which cannot be signed
//void ordinary_void;
bool ordinary_bool;
float ordinary_float;
double ordinary_double;
long double ordinary_long_double;
// types which can be signed
// char
char ordinary_signed_char;
signed char explicit_signed_char;
@gelisam
gelisam / nat.elf
Created April 18, 2014 19:16
example Twelf code
% in reply to http://www.reddit.com/r/haskell/comments/23cajh/how_can_i_avoid_this_function_to_typecheck/
% the datatype nat, with constructors z and s.
nat : type.
z : nat.
s : nat -> nat.
% a bunch of synonyms
zero = z : nat.
one = s z : nat.
@gelisam
gelisam / Regression.hs
Created May 3, 2014 17:58
RankNTypes Regression
{-# LANGUAGE RankNTypes #-}
applyId :: (forall a. a -> a) -> b -> b
applyId f = f
passes :: [b] -> [b]
passes = map (applyId id)
-- works with ghc 7.6, fails with ghc 7.8
fails :: [b] -> [b]
@gelisam
gelisam / Main.agda
Created May 24, 2014 19:54
why Haskell doesn't have a type-level identity function
-- in reply to http://www.reddit.com/r/haskell/comments/26dshj/why_doesnt_haskell_allow_type_aliases_in_the/
module Main where
open import Data.Nat
open import Data.List renaming (monad to List-Monad)
open import Category.Monad
open import Relation.Binary.PropositionalEquality
-- Unlike Haskell, Agda supports arbitrary functions from type to type.
@gelisam
gelisam / Main.hs
Created June 29, 2014 16:07
GLFW-b example
import Control.Applicative
import Control.Monad
import Graphics.Rendering.OpenGL.GL -- from package "OpenGL"
import Graphics.UI.GLFW as GLFW -- from package "GLFW-b"
windowWidth, windowHeight :: Int
(windowWidth, windowHeight) = (640, 480)
initialDots :: [Vector2 GLfloat]
initialDots = [ Vector2 0.5 0.5