Skip to content

Hard To Understand Programs

Iain Moncrief edited this page Nov 8, 2022 · 6 revisions

Iain Moncrief

-- CS 581

nats :: [Int]
nats = 1 : map (+1) nats

undefined :: a
undefined = undefined


-- CS 581

class Castable a b where
  cast :: a -> b

instance Castable Int Float where
  cast n = (float)n -- lol

instance Castable Int Double where
  cast n = intToDouble n

{-- Oh no! Some weird error about "undecidability" --}

{--
There is another error message I can't replicate but I see a lot. 
It says something about: Cannot construct infinite type a ~ a' or something weird. 
I still don't know what this means, but once I annotate my function, it almost always goes away.
I have seen it a lot more recently in the recent GHCi versions.
--}

The error I get when trying to implement Functor for Set.

import qualified Data.Set as Set
import Data.Set (Set)


instance Functor Set where
  fmap f s = Set.fromList $ map f $ Set.toList s
f :: forall a b c. (a -> b -> c) -> b -> a -> c                           -- fine
f :: forall a. forall b. forall c. (a -> b -> c) -> b -> a -> c           -- fine
f :: Num a => forall a. forall b. forall c. (a -> b -> c) -> b -> a -> c  -- weird error
f g b a = g a b
f == flip -- Shouldn't it return true?
x = 1

g :: Int -> Int
g x = x + 1

ghci> g x
-- Error!
data RoseTree a = Node a [RoseTree a] deriving (Show)


h t | (Node x ts) <- t = x : map h ts
    | otherwise        = []
-- Error!
--    Expected: RoseTree a -> a
--      Actual: RoseTree a -> [a]



h :: RoseTree [a] -> [a]
h t | (Node x ts) <- t = x : map h ts
    | otherwise        = []
{--
HardProgs.hs:92:26: error:
    • Couldn't match expected type ‘a’ with actual type ‘[a]’
      ‘a’ is a rigid type variable bound by
        the type signature for:
          h :: forall a. RoseTree [a] -> [a]
        at HardProgs.hs:91:1-24
    • In the first argument of ‘(:)’, namely ‘x’
      In the expression: x : map h ts
      In an equation for ‘h’:
          h t
            | (Node x ts) <- t = x : map h ts
            | otherwise = []
    • Relevant bindings include
        ts :: [RoseTree [a]] (bound at HardProgs.hs:92:15)
        x :: [a] (bound at HardProgs.hs:92:13)
        t :: RoseTree [a] (bound at HardProgs.hs:92:3)
        h :: RoseTree [a] -> [a] (bound at HardProgs.hs:92:1)
--}

Add your examples by editing the page

Clone this wiki locally