SOE ex.11.3

A function f is strict if  f ⊥ = ⊥

(1)
reverse :: [a] -> [a] is strict
reverse ⊥ = ⊥

  Easy to test, just try:
  reverse (head []) ==> error

(2)
simple :: Integer -> Integer -> Integer
simple x y z = x * (y + z)
simple is strict on all arguments.
simple ⊥ y z = simple x ⊥ z = simple x y ⊥ = ⊥
( because (*) and (+) are strict )

  simple (head [] :: Integer) 2 3 ==> error
  simple 1 (head [] :: Integer) 3 ==> error
  simple 1 2 (head [] :: Integer) ==> error

(3)
map :: (a -> b) -> [a] -> [b]
map is strict only on second argument.

  map id (head []) ==> error
  map (head []) [] ==> []

(4)
tail :: [a] -> [a]
tail is a strict function.
tail ⊥ = ⊥

  tail (head []) ==> error

(5)
area :: Shape -> Float
Base on the definition of area function (page#29),
area is a strict function.
area ⊥ = ⊥

(6)
regionToGRegion :: Region -> G.Region  -- page#119
regionToGRegion r = regToGReg (0,0) (1,1) r
and
regToGReg :: Vector -> Vector -> Region -> G.Region
and
base on the definition of regToGReg page#120
if region is ⊥ the result will be ⊥.
pattern matching will fail for ⊥ case.

  regionToGRegion ⊥ = ⊥

(7)
(&&) :: Bool -> Bool -> Bool
False && x   = False  -- Prelude's definition
True  && x   = x

Base on its definition, (&&) is strict only on first argument.
  ⊥ && x = ⊥
  False    && ⊥ = False

(8)
(True &&) :: Bool -> Bool
is a strict function.
  (True &&) ⊥ = ⊥

(9)
(False &&) :: Bool -> Bool
is not a strict function.
  (False &&) ⊥ = False

(10)
ifFun :: Bool -> a -> a -> a
ifFun pred cons alt = if pred then cons else alt
ifFun is strict function on all arguments.

  ifFun (head [] :: Bool) 1 2 ==> error
  ifFun True (head []) 2 ==> error
  ifFun False 1 (head []) ==> error

Leave a Reply