-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImperativeFuncs.hs
More file actions
91 lines (75 loc) · 2.54 KB
/
Copy pathImperativeFuncs.hs
File metadata and controls
91 lines (75 loc) · 2.54 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
-- Matt's Typechecker: Imperative Helper Functions
-- (C) Matt Teichman, 2019
module ImperativeFuncs where
-- standard modules
import Control.Exception
import System.Exit
-- my modules
import LambdaParse
import Latex
import Lexer
import LTerm
-- load DECLARATIONS file
getLexicon :: FilePath -> IO (Maybe [(String, TypedExpression)])
getLexicon filename = do
declarations <- catch (readFile filename) handleLexicon
let lexicon = map (makeDenotation . (lambdaParse declP [])) $
lines declarations
return $ sequence lexicon
-- load LAMBDATERM file
getInput :: FilePath -> IO (Maybe [String])
getInput filename = do
input <- catch (readFile filename) handleLambdas
return $ case lines input of
[] -> Nothing
_ -> Just $ lines input
-- error message for empty DECLARATIONS file
lexiconError :: IO ()
lexiconError = do
putStrLn "There is a parse error in your lexicon."
exitFailure
-- error message for empty LAMBDATERM file
termError :: IO ()
termError = do
putStrLn "Please populate your LAMBDATERM file with terms to be parsed."
exitFailure
-- message for a parse error
parseError :: IO ()
parseError = putStrLn "Parse Error!"
-- print a string representation of a typed expression to screen
displayMaybe Nothing = parseError
displayMaybe (Just parse) = displayEither parse
-- helper function for displayMaybe
displayEither (Left x) = putStrLn x
displayEither (Right y) = putStrLn $ show y
-- print a LaTeX representation of a typed expression to screen
latexMaybe Nothing = parseError
latexMaybe (Just parse) = latexEither parse
-- helper function for latexMaybe
latexEither (Left x) = putStrLn x
latexEither (Right y) = putStrLn $ "$" ++ latexTE y ++ "$"
-- handle missing DECLARATIONS file
handleLexicon :: SomeException -> IO String
handleLexicon (SomeException e) = do
putStrLn $
"\nPlease create a DECLARATIONS file with lexical entries.\n" ++
"\n For example:\n\n" ++
" dog:<ET>\n" ++
" fido:E\n" ++
" barks:<ET>\n" ++
" forall:<<ET>T>\n"
exitFailure
-- handle missing LAMBDATERM file
handleLambdas :: SomeException -> IO String
handleLambdas (SomeException e) = do
putStrLn $
"\nPlease create a LAMBDATERM file with some lambda terms in it.\n" ++
"\n For example:\n\n" ++
" fido\n" ++
" (barks fido)\n" ++
" (\\x:E . (barks x))\n"
exitFailure
-- create a LaTeX document displaying the lambda terms and their types
writeLatex :: String -> IO ()
writeLatex str = writeFile "lambdas.tex" $
prelude ++ "\n$" ++ str ++ "$\n" ++ closing