Skip to content

Instantly share code, notes, and snippets.

View gelisam's full-sized avatar

Samuel Gélineau gelisam

View GitHub Profile
@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 / Main.hs
Created April 18, 2014 23:19
unification-fd example
-- in reply to http://www.reddit.com/r/haskell/comments/23dxli/why_are_examples_completely_absent_from_hackage/
{-# LANGUAGE DeriveFunctor, DeriveFoldable, DeriveTraversable #-}
import Control.Unification
import Control.Unification.IntVar
import Control.Monad.Identity
import Control.Monad.Trans
import Control.Monad.Trans.Error
import Data.Foldable
import Data.Traversable
@gelisam
gelisam / IdiomaticLens.hs
Last active September 7, 2017 13:07
Idiomatic Lens
-- in reply to http://www.reddit.com/r/haskell/comments/23uzpg/lens_is_unidiomatic_haskell/
--
-- What the lens library might look like if Getter, Setter, Fold, Traversal,
-- Lens, Review, and Prism were separate datatypes.
--
-- For each optic, I only define enough combinators to explore pairs/sums
-- and lists of integers. Whenever possible, I reimplement the same
-- combinators and the same examples with all optics.
module IdiomaticLens where
@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 / UnderstandingPipes.md
Last active May 16, 2019 13:48
Understanding the Pipes library

In response to this reddit post about failing to understand the Pipes library after a couple of hours. I wanted to show how an experienced haskeller does it, but I'm afraid it also took me quite a few hours, which is why the following list of steps goes on and on.

After all those hours, my opinion is that Pipes is not at all an easy library. I don't know if Conduit is any easier, but otherwise I side with your friend in advising to use something else (perhaps ordinary lazy IO?) instead of Pipes.

Anyway, here is the full brain dump of my steps as I tried to get your three snippets to fit together.

  • Since you say that you have a hard time combining the snippets, I assume that they must have complicated types. So my first goal is to figure out the type of your first snippet.
  • hoogle for parseUrl, withManager, etc. No results.
  • Google for haskell runEffect, find that it's a method from Pipes.Core,
@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
@gelisam
gelisam / PolymorphicStrictness.hs
Last active August 29, 2015 14:04
code polymorphic in strictness (proof of concept)
-- in response to http://www.reddit.com/r/haskell/comments/2chb2h/fantasy_world_haskell/cjfobm5
--
-- Here is a demonstration that it is possible to write code which is "polymorphic in strictness",
-- that is, which can be executed strictly or lazily depending on the type at which it is instantiated.
--
-- Note that such a polymorphic function has to ask for a custom implementation of fmap and any other
-- standard function it is using, because those functions are not themselves polymorphic in strictness.
import Control.DeepSeq
import Debug.Trace
@gelisam
gelisam / PolymorphicStrictness2.hs
Created August 5, 2014 04:41
datatypes polymorphic in strictness
-- a better version of https://gist.github.com/gelisam/58495f7b996f77e09d80
-- based on http://www.reddit.com/r/haskell/comments/2chb2h/fantasy_world_haskell/cjgepwj
--
-- I implement the same example three times: once with a lazy datatype,
-- once with a strict datatype, and once with a strict-polymorphic datatype.
{-# LANGUAGE BangPatterns, KindSignatures, TypeOperators #-}
import Debug.Trace
import Text.Printf