diff --git a/cw/src/Challenges.hs b/cw/src/Challenges.hs index a551d7cbdf268055497c18b0e2d4ce74680fdb09..1264c1923f1c6631ed30c697c87c745b258ca2ea 100644 --- a/cw/src/Challenges.hs +++ b/cw/src/Challenges.hs @@ -20,6 +20,8 @@ import Control.DeepSeq import System.IO import System.Random +import Control.Applicative + -- types for Part I type WordSearchGrid = [[ Char ]] @@ -271,23 +273,23 @@ printGrid (w:ws) = do putStrLn w -- LamAbs Int LamExpr | LamVar Int deriving (Eq,Show,Read) prettyPrint :: LamMacroExpr -> String -prettyPrint (LamDef ms expr) = exprBrackets expr +prettyPrint (LamDef ms e) = exprBrackets e --applies brackets to expr if needed exprBrackets :: LamExpr -> String -exprBrackets expr | parseExpr str == expr = str --omit brackets - | otherwise = "(" + str ++ ")" --include brackets +exprBrackets e | expr str == e = str --omit brackets + | otherwise = "(" + str ++ ")" --include brackets where - str = exprToStr expr + str = exprToStr e --converts expr to string exprToStr :: LamExpr -> String -exprToStr (LamApp expr1 expr2) = exprBrackets expr1 ++ " " ++ exprBrackets expr2 -exprToStr (LamAbs x expr) = "\\x" ++ show x ++ " -> " ++ exprBrackets expr -exprToStr (LamVar x) = "x" ++ show x -exprToStr (LamMacro m) = m +exprToStr (LamApp e1 e2) = exprBrackets e1 ++ " " ++ exprBrackets e2 +exprToStr (LamAbs x e) = "\\x" ++ show x ++ " -> " ++ exprBrackets e +exprToStr (LamVar x) = "x" ++ show x +exprToStr (LamMacro m) = m @@ -300,8 +302,103 @@ ex3'4 = LamDef [ ("F", LamAbs 1 (LamVar 1) ) ] (LamAbs 2 (LamApp (LamAbs 1 (LamV -- Challenge 4 -- + +-- data LamMacroExpr = LamDef [ (String,LamExpr) ] LamExpr deriving (Eq,Show,Read) +-- data LamExpr = LamMacro String | LamApp LamExpr LamExpr | +-- LamAbs Int LamExpr | LamVar Int deriving (Eq,Show,Read) + + +--MacroExpr ::= "def" MacroName "=" Expr "in" MacroExpr | Expr +--Expr ::= Var | MacroName | Expr Expr | “\” Var “->” Expr | “(“ Expr “)” +--MacroName ::= UChar | UChar MacroName +--UChar ::= "A" | "B" | ... | "Z" +--Var ::= “x” Digits +--Digits ::= Digit | Digit Digits +--Digit ::= “0” | “1” | “2” | “3” | “4” | “5” | “6” | “7” | “8” | “9” + + parseLamMacro :: String -> Maybe LamMacroExpr -parseLamMacro _ = Nothing +parseLamMacro _ = Nothing + + +macroExpr :: Parse LamMacroExpr +macroExpr = do string "def" + space + name <- macroName + space + + + +expr :: Parser LamExpr +expr = do {x <- var; return $ LamVar x} <|> + do {name <- macroName;return $ LamMacro name} <|> + do e1 <- expr + space + e2 <- expr + return $ LamApp e1 e2 <|> + do char '\' + x <- var + symbol "->" + e <- expr + return $ LamAbs x e <|> + do char '(' + e <- identifier + char ')' + return e + + +macroName :: Parser String +macroName = do name <- some upper + return $ name + + +var :: Parser Int +var = do char 'x' + x <- some digit + return $ read x + + + +-- examples in the instructions +--Just (LamDef [] (LamApp (LamVar 1) (LamApp (LamVar 2) (LamVar 3)))) --"x1 (x2 x3)" +--Just (LamDef [] (LamApp (LamApp (LamVar 1) (LamVar 2)) (LamMacro"F"))) --"x1 x2 F" +--Just (LamDef [ ("F", LamAbs 1 (LamVar 1) ) ] (LamAbs 2 (LamApp (LamVar 2) (LamMacro "F")))) --"def F = \x1-> x1 in \x2 -> x2 F" +--Nothing -not in grammar --"def F = \x1 -> x1 (def G = \x1 -> x1 in x1)in \x2 -> x2" +--Nothing -repeated macro definition --"def F = \x1 -> x1 in def F = \x2 -> x2 x1 in x1" +--Nothing -macro body not closed --"def F = x1 in F" + + +--arithmetic expression examples + +-- expr ::= term '+' expr ⏐ term +-- term ::= factor '*' term ⏐ factor +-- factor ::= nat ⏐ '(' expr ')‘ +-- nat ::= digit | digit nat +-- digit ::= ’0’ ⏐ '1' ⏐ ... ⏐ '9' + +-- expr :: Parser AETree +-- expr = do t ← term +-- char ‘+’ +-- e ← expr +-- return (Add t e) +-- <|> term + +-- term :: Parser AETree +-- term = do f ← factor +-- char ‘*’ +-- t ← term +-- return (Mul f t) +-- <|> factor + +-- factor :: Parser AETree +-- factor = nat <|> do char '(' +-- e ← expr +-- char ')' +-- return e + +-- nat :: Parser AETree +-- nat = do ds ← some digit +-- return (Lit (read ds)) -- Challenge 5