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.agda
Created March 13, 2013 14:58
How Agda's --type-in-type can lead to a contradiction.
{-# OPTIONS --type-in-type #-}
module Main where
open import Data.Empty
data _≡_ (A : Set) : Set → Set where
refl : A ≡ A
subst : {P : Set → Set} → {x y : Set} → x ≡ y → P y → P x
@gelisam
gelisam / memoizing_fib.hs
Last active December 21, 2015 05:38
Polymorphism and memoization interact in strange ways.
import Debug.Trace
noisy0 :: () -> Int
noisy0 () = trace "eval" 0
main = do print (let x = ( noisy0 ()
, fst x)
in x) -- evals noisyMempty once
print (( noisy0 ()
@gelisam
gelisam / README.md
Last active December 22, 2015 09:19
To make the README very short and clear, we could delegate the details to a second, longer document.

Hawk

Transform text from the command-line using Haskell expressions. Similar to awk, but using Haskell as the text-processing language.

Examples

The Haskell expression (!! 1) takes the second element from a list. Using Hawk to --map this expression over all input lines, we can extract the second column of the output of ps aux.

@gelisam
gelisam / prelude.hs
Last active December 22, 2015 10:29
The ~/.hawk/prelude.hs you would need in order to run the tree command in the Hawk README.
{-# LANGUAGE FlexibleInstances, OverloadedStrings #-}
import Prelude
import qualified Data.ByteString.Lazy.Char8 as B
import qualified Data.List as L
import Data.Monoid
import Data.Tree hiding (Forest)
import System.Console.Hawk.Representable
newtype Forest a = Forest {unForest :: [Tree a]}
@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 / Main.hs
Last active August 22, 2022 18:18
IndexedMonad example
-- in reply to http://www.reddit.com/r/haskell/comments/21mja6/make_lllegal_state_transitions_unrepresentable/
--
-- We implement a tiny language with three commands: Open, Close, and Get.
-- The first Get after an Open returns 1, the second Get returns 2, and so on.
--
-- Get is only valid while the state is open, and
-- Open must always be matched by a Close.
-- We enforce both restrictions via the type system.
--
-- There are two valid states: Opened and Closed.