Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
prog3-coursework
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ik1g19
prog3-coursework
Commits
0c4c72c5
Commit
0c4c72c5
authored
4 years ago
by
ik1g19
Browse files
Options
Downloads
Patches
Plain Diff
progress on challenge 4
parent
6816f582
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
cw/src/Challenges.hs
+106
-9
106 additions, 9 deletions
cw/src/Challenges.hs
with
106 additions
and
9 deletions
cw/src/Challenges.hs
+
106
−
9
View file @
0c4c72c5
...
@@ -20,6 +20,8 @@ import Control.DeepSeq
...
@@ -20,6 +20,8 @@ import Control.DeepSeq
import
System.IO
import
System.IO
import
System.Random
import
System.Random
import
Control.Applicative
-- types for Part I
-- types for Part I
type
WordSearchGrid
=
[[
Char
]]
type
WordSearchGrid
=
[[
Char
]]
...
@@ -271,21 +273,21 @@ printGrid (w:ws) = do putStrLn w
...
@@ -271,21 +273,21 @@ printGrid (w:ws) = do putStrLn w
-- LamAbs Int LamExpr | LamVar Int deriving (Eq,Show,Read)
-- LamAbs Int LamExpr | LamVar Int deriving (Eq,Show,Read)
prettyPrint
::
LamMacroExpr
->
String
prettyPrint
::
LamMacroExpr
->
String
prettyPrint
(
LamDef
ms
e
xpr
)
=
exprBrackets
e
xpr
prettyPrint
(
LamDef
ms
e
)
=
exprBrackets
e
--applies brackets to expr if needed
--applies brackets to expr if needed
exprBrackets
::
LamExpr
->
String
exprBrackets
::
LamExpr
->
String
exprBrackets
e
xpr
|
parseE
xpr
str
==
e
xpr
=
str
--omit brackets
exprBrackets
e
|
e
xpr
str
==
e
=
str
--omit brackets
|
otherwise
=
"("
+
str
++
")"
--include brackets
|
otherwise
=
"("
+
str
++
")"
--include brackets
where
where
str
=
exprToStr
e
xpr
str
=
exprToStr
e
--converts expr to string
--converts expr to string
exprToStr
::
LamExpr
->
String
exprToStr
::
LamExpr
->
String
exprToStr
(
LamApp
e
xpr1
expr
2
)
=
exprBrackets
e
xpr
1
++
" "
++
exprBrackets
e
xpr
2
exprToStr
(
LamApp
e
1
e
2
)
=
exprBrackets
e1
++
" "
++
exprBrackets
e2
exprToStr
(
LamAbs
x
e
xpr
)
=
"
\\
x"
++
show
x
++
" -> "
++
exprBrackets
e
xpr
exprToStr
(
LamAbs
x
e
)
=
"
\\
x"
++
show
x
++
" -> "
++
exprBrackets
e
exprToStr
(
LamVar
x
)
=
"x"
++
show
x
exprToStr
(
LamVar
x
)
=
"x"
++
show
x
exprToStr
(
LamMacro
m
)
=
m
exprToStr
(
LamMacro
m
)
=
m
...
@@ -300,10 +302,105 @@ ex3'4 = LamDef [ ("F", LamAbs 1 (LamVar 1) ) ] (LamAbs 2 (LamApp (LamAbs 1 (LamV
...
@@ -300,10 +302,105 @@ ex3'4 = LamDef [ ("F", LamAbs 1 (LamVar 1) ) ] (LamAbs 2 (LamApp (LamAbs 1 (LamV
-- Challenge 4 --
-- 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
::
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
-- Challenge 5
cpsTransform
::
LamMacroExpr
->
LamMacroExpr
cpsTransform
::
LamMacroExpr
->
LamMacroExpr
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment