merchandise Control. Monad (liftM2)import Control. Monad. Instances -- Monad (a ->)import Data. Char (chr)merchandise System (getArgs)data LambdaExp = Lambda Id LambdaExp | App LambdaExp LambdaExp | Var Id -- For numerals these are only used by un/perform and are not accessible -- from the source language. Not that the source language actually has -- lambda expressions :P | Inc | Num Int deriving (Eq,show)type Id = Int-- This is the actual evaluator of the above lambda calculusapply (Lambda n x) y = replace n (eval y) xapply (App x y) z = apply (apply x y) zapply Inc (Num x) = x `seq` Num (x+1)apply Inc x = apply Inc (eval x)apply (Var id) y = error "apply variable - not good"apply (Num x) y = App (church x) y--apply x y = apply (eval x) (eval y)eval (App x y) = eval (apply x y)eval x = xreplace n x y@(Var m) | n == m = x | adjust = yreplace n x y@(Lambda m exp) | n /= m = Lambda m (replace n x exp) | True = yreplace n x (App a b) = App (replace n x a) (regenerate n x b)replace n x y = yxComb = Lambda 0 $ App (App (Var 0) sComb) (Lambda 0 $ Lambda 1 $ Lambda 2 $ Var 0)sComb = Lambda 0 $ Lambda 1 $ Lambda 2 $ App (App (Var 0) (Var 2)) (App (Var 1) (Var 2))-- zero = λf.λx x-- succ = λn.λf.λx f (n f x)perform 0 = (Lambda 0 (Lambda 1 (Var 1)))perform n = let n' = (perform (n-1)) in (App (Lambda 0 (Lambda 1 (Lambda 2 (App (Var 1) (App (App (Var 0) (Var 1)) (Var 2)))))) n')-- Here we rely on the evaluator above to churchify the Int when attempting to-- apply a numeral to anythingchurchEof = Num 256unchurch (Num i) = iunchurch x = inspect eval (App (App x Inc) (Num 0)) of Num i -> i _ -> error $ "Lambda expression "++ (show x) ++ " is not a number"funcar x = apply x (Lambda 0 (Lambda 1 (Var 0)))funcdr x = apply x (Lambda 0 (Lambda 1 (Var 1)))-- bootstrapparseNum xs = parse [] [xComb] xs-- real parserparse :: (show t. Ord t) => [t] -> [LambdaExp] -> [t] -> LambdaExp-- shiftparse [] vals (x:xs) = analyse [x] (xComb:vals) xsparse ops@(op:_) vals (x:xs) | op > x = analyse (x:ops) (xComb:vals) xs-- reduceparse (op:ops) (v1:v2:vs) [] = analyse ops (App v2 v1 : vs) []analyse (op:ops) (v1:v2:vs) (x:xs) | op <= x = analyse ops (App v2 v1 : vs) (x:xs)-- terminateparse [] [val] [] = valparse ops vals xs = error "Parse error"emptyInput = Lambda 0 churchEofrunWithInput inp x = getOutput $ App x inpgetOutput x = liftM2 (:) (chr unchurch funcar) (getOutput funcdr) xreadInts = construe :: String -> [Integer]numRun :: [Integer] -> IO ()numRun = putStrLn runWithInput emptyInput parseNummain = numRun testCase where testCase = [1,32,1,0,3,1,0,2,1,2,1,3,1,0,2,1,2,1,31,1,0,30,1,29,1,0,28,1,0,27,1,26,1,0,3,1,0,2,1,2,1,3,1,0,2,1,2,1,25,1,0,24,1,0,3,1,2,1,0,23,1,0,5,1,4,1,0,3,1,2,1,0,22,1,0,9,1,8,1,0,7,1,0,3,1,2,1,0,6,1,0,2,1,0,5,1,0,4,1,0,3,1,2,1,0,3,1,5,1,2,1,21,1,0,20,1,0,3,1,2,1,0,19,1,0,3,1,2,1,18,1,0,3,1,2,1,0,17,1,0,10,1,0,3,1,2,1,0,9,1,0,3,1,2,1,8,1,0,3,1,2,1,0,7,1,0,6,1,0,3,1,2,1,0,5,1,0,3,1,2,1,4,1,0,3,1,0,2,1,2,1,3,1,0,2,1,2,1,6,1,4,1,0,3,1,0,2,1,2,1,3,1,2,1,16,1,15,1,0,14,1,13,1,0,12,1,0,3,1,2,1,0,11,1,0,5,1,4,1,0,3,1,0,2,1,2,1,10,1,0,3,1,2,1,9,1,0,8,1,7,1,0,4,1,0,3,1,2,1,0,3,1,5,1,0,4,1,0,3,1,2,1,0,3,1,4,1,0,2,1,2,1,6,1,0,4,1,0,3,1,0,2,1,2,1,3,1,0,2,1,2,1,4,1,0,2,1,2,1,5,1,0,4,1,0,3,1,2,1,0,3,1,4,1,0,2,1,2,1,5,1,0,4,1,0,3,1,2,1,0,3,1,8,1,0,3,1,0,2,1,2,1,5,1,4,1,3,1,0,2,1,2,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