summaryrefslogtreecommitdiff
path: root/testsuite/tests/ghc-regress/safeHaskell/safeLanguage/SafeLang04.hs
blob: a9ac3619f32bb302224df98f8e98c80ce56c4e48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{-# OPTIONS_GHC -fenable-rewrite-rules #-}
-- | Check rules work as normal without Safe
module Main where

data T = T1 | T2 | T3 deriving ( Eq, Ord, Show )

lookupx :: Ord key => Show val => [(key,val)] -> key -> Maybe val
lookupx [] _                      = Nothing
lookupx ((t,a):xs) t' | t == t'   = Just a
                      | otherwise = lookupx xs t'

{-# RULES "lookupx/T" lookupx = tLookup #-}
tLookup :: [(T,a)] -> T -> Maybe a
tLookup [] _                      = Nothing
tLookup ((t,a):xs) t' | t /= t'   = Just a
                      | otherwise = tLookup xs t'

space = [(T1,"a"),(T2,"b"),(T3,"c")]
key = T3

main = do
    putStrLn $ "looking for " ++ show key
    putStrLn $ "in space " ++ show space
    putStrLn $ "Found: " ++ show (fromMaybe "Not Found!" $ lookupx space key)
    let b | Just "c" <- lookupx space key = "YES"
          | otherwise                     = "NO"
    putStrLn $ "Rules Disabled: " ++ b

fromMaybe :: a -> Maybe a -> a
fromMaybe a Nothing  = a
fromMaybe _ (Just a) = a