Skip to content

Instantly share code, notes, and snippets.

@chansey97
Created November 27, 2019 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chansey97/62e45ad98fa413291986d9978ea642d1 to your computer and use it in GitHub Desktop.
Save chansey97/62e45ad98fa413291986d9978ea642d1 to your computer and use it in GitHub Desktop.
ctfp-14.3.6-correct-solution
{-# LANGUAGE DeriveFunctor, TypeFamilies #-}
module Main where
data Pair a = Pair a a
deriving (Eq)
getFst :: Pair a -> a
getFst (Pair x1 _) = x1
getSnd :: Pair a -> a
getSnd (Pair _ x2) = x2
class Representable f where
type Rep f :: *
tabulate :: (Rep f -> x) -> f x
index :: f x -> Rep f -> x
instance Representable Pair where
type Rep Pair = Bool
tabulate f = Pair (f True) (f False) -- fmap f Pair(Bool, False)
index (Pair x1 x2) = \bool -> if bool==True then x1 else x2
-- index (tabulate f)
f :: Bool -> String
f True = "A"
f False = "B"
tabulatef :: Pair String
tabulatef = tabulate f
indextabulatef :: Bool -> String
indextabulatef = index tabulatef
check1a = indextabulatef True == "A"
check1b = indextabulatef False == "B"
-- tabulate (index Pair x1 x2)
pair :: Pair String
pair = Pair "A" "B"
indexpair :: Bool->String
indexpair = index pair
tabulateindexpair :: Pair String
tabulateindexpair = tabulate indexpair
check2 = tabulateindexpair == Pair "A" "B"
main = do
putStrLn "test"
putStrLn (show check1a)
putStrLn (show check1b)
putStrLn (show check2)
putStrLn (show (getFst tabulateindexpair))
putStrLn (show (getSnd tabulateindexpair))
--GHCi, version 8.6.5
--test
--True
--True
--True
--"A"
--"B"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment