Skip to content

Instantly share code, notes, and snippets.

@Blaisorblade
Last active December 18, 2015 03:18

Revisions

  1. Blaisorblade revised this gist Jun 5, 2013. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions DeltaInt.hs
    Original file line number Diff line number Diff line change
    @@ -15,5 +15,19 @@ instance Changing Integer where
    apply = (+)
    diff = (-)

    -- Fails:

    v :: Delta Integer
    v = diff 2 1

    {- Error:
    DeltaInt.hs:21:5:
    Couldn't match type `Delta a0' with `Integer'
    Expected type: Delta Integer
    Actual type: Delta a0
    In the return type of a call of `diff'
    In the expression: diff 2 1
    In an equation for `v': v = diff 2 1
    Failed, modules loaded: none.
    -}
  2. Blaisorblade created this gist Jun 5, 2013.
    19 changes: 19 additions & 0 deletions DeltaInt.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    {-# LANGUAGE TypeFamilies #-}

    module DeltaInt where

    class Changing a where
    -- Using type families here is a bit convenient, but means risking trouble later.
    type Delta a
    nil :: a -> Delta a
    apply :: a -> Delta a -> a
    diff :: a -> a -> Delta a

    instance Changing Integer where
    type Delta Integer = Integer
    nil = const 0
    apply = (+)
    diff = (-)

    v :: Delta Integer
    v = diff 2 1