Skip to content

Instantly share code, notes, and snippets.

@tomlokhorst
Forked from sebastiaanvisser/tyingtheknot.hs
Created December 27, 2010 18:46
Show Gist options
  • Select an option

  • Save tomlokhorst/756407 to your computer and use it in GitHub Desktop.

Select an option

Save tomlokhorst/756407 to your computer and use it in GitHub Desktop.

Revisions

  1. tomlokhorst revised this gist Dec 27, 2010. 1 changed file with 4 additions and 11 deletions.
    15 changes: 4 additions & 11 deletions tyingtheknot.hs
    Original file line number Diff line number Diff line change
    @@ -8,14 +8,6 @@ import Prelude hiding ((.), id)
    import Control.Category
    import Data.Record.Label

    -- Tying the knot for two values with two labels indicating the positions to tie.

    tie :: a -> b -> (a :-> b) -> (b :-> a) -> (a, b)
    tie a b f g =
    let a' = setL f b' a
    b' = setL g a' b
    in (a', b')

    -- Simple test.

    data Person = Person
    @@ -32,9 +24,10 @@ $(mkLabels [''Person])
    $(mkLabels [''Job])

    me :: Person
    myJob :: Job
    me = setL job myJob (Person "Tom" myJob)

    (me, myJob) = tie (Person "Sebas" undefined) (Job "Software Engineer" undefined) job who
    myJob :: Job
    myJob = setL who me (Job "Software Engineer" me)

    test :: String
    test = getL (title . job . who . job . who . job . who . job) me
    test = getL (title . job . who . job . who . job . who . job) me
  2. Sebastiaan Visser revised this gist Dec 27, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tyingtheknot.hs
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ import Prelude hiding ((.), id)
    import Control.Category
    import Data.Record.Label

    -- Tying the knot for to values with two labels indicating the positions to tie.
    -- Tying the knot for two values with two labels indicating the positions to tie.

    tie :: a -> b -> (a :-> b) -> (b :-> a) -> (a, b)
    tie a b f g =
  3. Sebastiaan Visser created this gist Dec 27, 2010.
    40 changes: 40 additions & 0 deletions tyingtheknot.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    {-# LANGUAGE
    TypeOperators
    , TemplateHaskell
    #-}
    module Tie where

    import Prelude hiding ((.), id)
    import Control.Category
    import Data.Record.Label

    -- Tying the knot for to values with two labels indicating the positions to tie.

    tie :: a -> b -> (a :-> b) -> (b :-> a) -> (a, b)
    tie a b f g =
    let a' = setL f b' a
    b' = setL g a' b
    in (a', b')

    -- Simple test.

    data Person = Person
    { _name :: String
    , _job :: Job
    }

    data Job = Job
    { _title :: String
    , _who :: Person
    }

    $(mkLabels [''Person])
    $(mkLabels [''Job])

    me :: Person
    myJob :: Job

    (me, myJob) = tie (Person "Sebas" undefined) (Job "Software Engineer" undefined) job who

    test :: String
    test = getL (title . job . who . job . who . job . who . job) me