Last active
September 6, 2016 00:22
-
-
Save schar/d5db618f201c53930d9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE FlexibleInstances #-} | |
module Main (main) where | |
main :: IO () | |
main = return () | |
type E = String | |
type T = String | |
data F a = F a [a] deriving (Show, Eq) | |
domain :: [E] | |
domain = ["Bill", "Mary", "Sue", "John"] | |
instance Functor F where | |
fmap k (F x xs) = F (k x) $ map k xs | |
pamf :: F a -> (a -> b) -> F b | |
pamf = flip fmap | |
foc :: E -> F E | |
foc x = F x domain | |
saw :: (E, E) -> T | |
saw (x, y) = x ++ " saw " ++ y | |
class Alts f where | |
alts :: f -> [T] | |
instance Alts (F T) where | |
alts (F x xs) = x : xs | |
instance {-# OVERLAPS #-} Alts a => Alts (F a) where | |
alts (F x xs) = alts x ++ concatMap alts xs | |
class Point f where | |
point :: f -> T | |
instance Point (F T) where | |
point (F x xs) = x | |
instance {-# OVERLAPS #-} Point a => Point (F a) where | |
point (F x xs) = point x | |
contrast :: (Point f, Alts f) => T -> f -> Bool | |
contrast c f = point f /= c && length [x | x <- alts f, x == c] == 1 | |
ant = saw ("John", "Mary") -- John saw Mary | |
s0 = foc "Bill" `pamf` | |
\x -> saw (x, "Mary") -- BILL saw Mary | |
s1 = foc "Mary" `pamf` | |
\x -> saw ("Bill", x) -- Bill saw MARY | |
s2 = foc "Bill" `pamf` | |
\x -> foc "Mary" `pamf` | |
\y -> saw (x, y) -- BILL saw MARY (SS) | |
s3 = foc "Mary" `pamf` | |
\y -> foc "Bill" `pamf` | |
\x -> saw (x, y) -- BILL saw MARY (IS) | |
s4 = foc "Bill" `pamf` | |
\x -> foc "Bill" `pamf` | |
\y -> saw (x, y) -- BILL saw BILL (SS) | |
s5 = foc "Bill" `pamf` | |
\y -> foc "Bill" `pamf` | |
\x -> saw (x, y) -- BILL saw BILL (IS) | |
s6 = foc "Bill" `pamf` | |
\x -> foc "Sue" `pamf` | |
\y -> saw (x, y) -- BILL saw SUE (SS) | |
s7 = foc "Sue" `pamf` | |
\y -> foc "Bill" `pamf` | |
\x -> saw (x, y) -- BILL saw SUE (SS) | |
(<~>) :: (Point f, Alts f) => T -> f -> T | |
c <~> f = if contrast c f then point f else "you failed to contrast" | |
-- some test cases | |
-- ant <~> s0 | |
-- ant <~> s1 | |
-- ant <~> s2 | |
-- ant <~> s3 | |
-- ant <~> s4 | |
-- ant <~> s5 | |
-- ant <~> s6 | |
-- ant <~> s7 |
dylnb
commented
Dec 22, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment