-- Solutions for INFOFP Final 2026-2027 module Exam261112 where import Control.Monad.State (State, get, put, runState) import Data.Char (isUpper) import Data.List (tails) -- Subquestion labels follow the question text, not the answer-box labels. -- Question 1 data Op = Add | Sub deriving (Eq, Show) data Expr a = Lit Int | Var a | Bin Op (Expr a) (Expr a) deriving (Eq, Show) -- 1a substitute :: (a -> Expr b) -> Expr a -> Expr b -- 1b substitute _ (Lit n) = Lit n substitute replaceBy (Var x) = replaceBy x substitute replaceBy (Bin op l r) = Bin op (substitute replaceBy l) (substitute replaceBy r) -- 1c instance Functor Expr where fmap f = substitute (Var . f) data Instr a = Push Int | Load a | Apply Op deriving (Eq, Show) -- 1d compileOnto :: Expr a -> [Instr a] -> [Instr a] compileOnto (Lit n) rest = Push n : rest compileOnto (Var x) rest = Load x : rest compileOnto (Bin op l r) rest = compileOnto l (compileOnto r (Apply op : rest)) compile :: Expr a -> [Instr a] compile e = compileOnto e [] -- Question 2 -- 2a suffixTotals :: [Int] -> [Int] suffixTotals = foldr step [0] where step x totals@(s : _) = (x + s) : totals -- 2b largeSuffixes :: Int -> [Int] -> [Int] largeSuffixes limit = map fst . filter ((> limit) . snd) . zip [0..] . suffixTotals -- 2c specSuffixTotals :: ([Int] -> [Int]) -> [Int] -> Bool specSuffixTotals candidate xs = candidate xs == map sum (tails xs) -- Question 3 type NameTable = (Int, [(String, Int)]) -- 3a processName :: String -> State NameTable Int processName name = do (next, table) <- get case lookup name table of Just old -> return old Nothing -> do put (next + 1, (name, next) : table) return next -- 3b rememberPair :: String -> String -> State NameTable (String, Int) rememberPair first second = processName first >>= \i -> let labelled = (first, i) in processName second >> return labelled -- 3c labelNames :: String -> [(String, Int)] labelNames sentence = (snd . snd) (runState (mapM processName [w | w <- words sentence, startsWithCapital w]) (0, [])) startsWithCapital :: String -> Bool startsWithCapital (c:_) = isUpper c startsWithCapital _ = error "absurd" -- 3d processSentence :: IO Int processSentence = do sentence <- getLine let labels = labelNames sentence print labels return (length labels) -- Question 4 pre :: (a -> b) -> (b -> c) -> a -> c pre f h x = h (f x) -- (a) -- We use the standard Prelude definition of composition: -- (.) :: (b -> c) -> (a -> b) -> a -> c -- (f . g) x = f (g x) -- (b) total :: [Int] -> Int total [] = 0 -- (c) total (x:xs) = x + total xs -- (d) withSum :: [Int] -> (Int -> b) -> b withSum [] k = k 0 -- (e) withSum (x:xs) k = withSum xs (preAdd x k) -- (f) preAdd :: Int -> (Int -> b) -> Int -> b preAdd x k s = k (x + s) -- (g) -- 4a {- Claim: pre (f . g) = pre g . pre f, where g :: a -> b and f :: b -> c. By function extensionality, it suffices to prove that pre (f . g) h x = (pre g . pre f) h x for all h :: c -> d and x :: a. Let h and x be arbitrary. Then: pre (f . g) h x = -- (a) h ((f . g) x) = -- (b) h (f (g x)) = -- (a), right to left pre f h (g x) = -- (a), right to left pre g (pre f h) x = -- (b), right to left (pre g . pre f) h x Since h and x were arbitrary, we conclude by function extensionality that pre (f . g) = pre g . pre f. -} -- 4b {- Claim: for every finite list xs :: [Int] and every k :: Int -> b, withSum xs k = k (total xs). We prove the claim by structural induction on xs, keeping k universally quantified in the induction hypothesis. Base case: xs = []. Let k :: Int -> b be arbitrary. withSum [] k = -- (e) k 0 = -- (c), right to left k (total []) So the claim holds for xs = [] and every k :: Int -> b. Inductive case: xs = z:zs. Assume the induction hypothesis: I.H.: for every h :: Int -> b, withSum zs h = h (total zs). Let k :: Int -> b be arbitrary. We must show that withSum (z:zs) k = k (total (z:zs)). withSum (z:zs) k = -- (f) withSum zs (preAdd z k) = -- I.H., with h = preAdd z k :: Int -> b preAdd z k (total zs) = -- (g) k (z + total zs) = -- (d), right to left k (total (z:zs)) Thus the claim holds for z:zs and every k, assuming the induction hypothesis. By induction, it holds for every finite list xs and every k :: Int -> b. -} -- Question 5 pairUp :: Int -> (Int, Int) pairUp x = (x, x + 1) -- 5a -- Answer C: pairUp 4 : map pairUp [] -- The outer (:) is a constructor, so this is already WHNF. Neither its -- head nor its tail needs to be evaluated. The undefined element was dropped. makeBox :: Int -> Maybe Int makeBox x = Just (x + 1) -- 5b -- Just (undefined + 1) -- The outer Just is a constructor; its argument is not forced. -- 5c -- ((+) (undefined :: Int)) `seq` True -- True: the partially applied function is already in WHNF. -- -- foldl (&&) True [undefined, False] -- undefined: the result is (True && undefined) && False, which forces -- undefined before the final False can be considered. -- -- let k x rest = if x == 2 then True else rest in -- foldr k False (cycle [1..2]) -- True: the second element is 2, so the remaining list is not needed. -- -- let k acc x = acc || x == 2 in foldl k False (cycle [1..2]) -- undefined: foldl must reach the end of the list before returning its -- accumulator, but this list is infinite. -- 5d forcePair :: (a, b) -> (a, b) forcePair (x, y) = x `seq` y `seq` (x, y)