2022/10/8 20:16 Assignment 2 2022 Specifications
Published using Google Docs Learn more Repo Abuse
Assignment 2 2022 Speci cations
Updated automatically every 5 minutes
Copyright By PowCoder代写 加微信 powcoder
FIT2102 Programming Paradigms 2022
Assignment 2: Lambda Calculus Parser
Due Date: 23:55, October 21st, 2022
Weighting: 30% of your final mark for the unit
Overview: Implement an interpreter that translates input strings into lambda calculus expressions using parser combinators in Haskell. This should highlight your understanding of Haskell, functional programming, and its application; as well as lambda calculus. You will also need to write a report to demonstrate your knowledge and understanding.
Building and using the
code: The code bundle is packaged the same way as tutorial code. To compile the code, run: `stack build`. To run the main function, run: `stack run`
Submission Instructions
file:///Users/yichensong/Desktop/FIT2102 Assignment 1/Assignment 2 2022 Specifications.html 1/26
2022/10/8 20:16 Assignment 2 2022 Specifications
Published using Google Docs
Learn more Repo Abuse
extracts to a folder named
¡ñ It must contain all the Assignment 2 2022 Speci cations
Updated automatically every 5 minutes
Submit a zipped file named
code that will be
marked including the
report and submission/LambdaParser.hs
¡ñ Your assignment code and your report in PDF format go in the submission/ folder of the code bundle. To submit you will zip up just the contents of this submission folder
¡ñ It should include sufficient documentation so that we can appreciate everything you have done (readme.txt or other supplementary documentation)
¡ñ You also need to include a report describing your design decisions. The report must be named
¡ñ Only Haskell built in libraries should be used (i.e. no additional installation should be required)
¡ñ Before zipping, run `stack clean –full` (to ensure a small bundle)
¡ñ Make sure the code you submit executes properly.
The marking process will look something like this:
1. Extract
2. Copy the
submission folder contents into the assignment code bundle submission folder
3. Execute stack build, stack test, stack run
Please ensure that you test this process before submitting. Any issues during this process will make your marker unhappy.
file:///Users/yichensong/Desktop/FIT2102 Assignment 1/Assignment 2 2022 Specifications.html 2/26
2022/10/8 20:16 Assignment 2 2022 Specifications
Failure to follow these instructions may result in
Published using Google Docs Learn more Repo Abuse deductions.
Assignment 2 2022 Speci cations
Table of Contents
Updated automatically every 5 minutes
Assignment 2: Lambda Calculus Parser 1
Submission Instructions
Table of Contents 2
Introduction 3
Goals / Learning Outcomes 3
Scope of assignment Lambda Calculus syntax Getting started 4 What is provided
lam :: Char -> Builder -> Builder 5
term :: Char -> Builder
build :: Builder -> Lambda 5
ap :: Builder -> Builder -> Builder 6
normal :: Lambda -> Lambda: 6
lamToBool :: Lambda -> Maybe Bool: 6
lamToInt :: Lambda -> Maybe Int: 7
Additional functions and types 7
Part 1 (10 marks) 9
Exercise 1 (2 marks): Construct a BNF Grammar for lambda calculus expressions 9
Exercise 2 (4 marks): Construct a parser for long form lambda calculus expressions 9
Exercise 3 (4 marks): Construct a parser for short form lambda calculus expressions 10
Part 2 (8 marks) 10
Exercise 1 (2 marks): Construct a parser for logical statements 10
Exercise 2 (4 marks): Construct a parser for arithmetic expressions 11
Exercise 3 (2 marks): Construct a parser for
file:///Users/yichensong/Desktop/FIT2102 Assignment 1/Assignment 2 2022 Specifications.html 3/26
2022/10/8 20:16 Assignment 2 2022 Specifications
comparison
Published using Google Docs expressions 11 Learn more Repo Abuse
Exercise 1 (3 marks):
Construct a parser for basic
Assignment 2 2022 Speci cations
Exercise 2 (4 marks): Construct a parser for more advanced concepts 12
Updated automatically every 5 minutes
Part 3 (7 marks) 12
Haskell constructs 12
Report (5 marks) 14
Marking breakdown
Correctness Code quality
Additional information
Common Mistakes Writing reports
Revision History
Introduction
In this assignment, we will use Haskell to develop an interpreter that parses a string into the data types provided in Builder.hs and Lambda.hs. Using this we can create Lambda Calculus expressions which we are then able to normalise into a simplified but equivalent expression (evaluating the expression).
This assignment will be split into multiple parts, of increasing difficulty. While many lambda expressions for earlier tasks have been provided to you in the course notes and in this document, you may have to do your own independent research to find church encodings for more complicated constructs.
You are welcome to use any of the material covered in the previous weeks, including solutions for tutorial questions, to assist in the development of your parser. Please reference ideas and code constructs obtained from external sources, as well as anything else you might find in your independent research, for this assignment.
file:///Users/yichensong/Desktop/FIT2102 Assignment 1/Assignment 2 2022 Specifications.html 4/26
2022/10/8 20:16 Assignment 2 2022 Specifications
Goals / Learning
Published using Google Docs Learn more Repo Abuse Outcomes
Assignment 2 2022 Speci cations
Updated automatically every 5 The purpose of this assignment is minutes
to highlight lambda calculus as a
method of computation and apply the skills you have learned to a practical exercise (parsing).
¡ñ Use functional programming and parsing effectively
¡ñ Understand and be able to use key functional programming principles (HOF, pure functions, immutable data structures, abstractions)
¡ñ Apply Haskell and lambda calculus to implement non- trivial programs
Scope of assignment
It is important to note that you will not need to implement code to evaluate lambda calculus
expressions. Functions to evaluate lambda calculus expressions are provided.
Rather, you are only required to parse an expression into the data types provided in
Builder.hs and then use the given functions to evaluate the expression.
You will need to have some understanding of lambda calculus, particularly in the latter parts of the assignment. Revise the notes on Lambda
Calculus and come to consultations early if you have any uncertainty.
file:///Users/yichensong/Desktop/FIT2102 Assignment 1/Assignment 2 2022 Specifications.html 5/26
2022/10/8 20:16 Assignment 2 2022 Specifications
Published using Google Docs Learn more Repo Abuse Lambda Calculus syntax
Assignment 2 2022 Speci cations
Updated automatically every 5 Lambda Calculus can be written minutes
in an explicit way (long form),
using brackets to show ordering of all operations, or in an implicit way (short form), by removing unnecessary syntax.
For example, consider the expression for a Church Encoded IF in short form
¦Ëbtf.b t f
We introduced this short syntax for lambda expressions as it¡¯s a little easier to read. However, let¡¯s also consider the long form version. First we need to include all lambdas
¦Ëb.¦Ët.¦Ëf.b t f
Then, we need to include brackets around every expression (note that applying the function ¡°b¡± to two parameters ¡°t¡± and ¡°f¡± is one expression)
(¦Ëb.(¦Ët.(¦Ëf.b t f)))
It may be simpler to parse the long-form syntax of lambda calculus compared to the short form.
Getting started
The first step which we recommend is to play around with the code base, and see how you can build Lambda Expressions.
¡ñ Step 1. Try to use the builder and GHCI to construct and normalise the following expressions:
¡ð (¦Ëb.(¦Ët.(¦Ëf.f t b)))
(¦Ëy.xx(¦Ëz.z)x))
¡ð For extra practice build the lambda expressions in the
file:///Users/yichensong/Desktop/FIT2102 Assignment 1/Assignment 2 2022 Specifications.html 6/26
2022/10/8 20:16 Assignment 2 2022 Specifications
Published using Google Docs
Assignment 2 2022 Speci cations
Week 5 Tutorial Sheet. See which ones successfully build and think about why!
Learn more Repo Abuse
Updated automatically every 5
¡ñ Step 2. Try to describe the
syntax for a verbose Lambda Calculus using a BNF Grammar.
¡ñ Step 3. Try to construct parsers for and test each part of this grammar separately.
¡ñ Step 4. Combine and test your code!
What is provided
Provided to you is an engine which can Beta/Eta reduce Lambda Calculus into beta- normal form.
This engine is built around the Builder type. This Builder type allows you to create Lambda Calculus expressions, which can then be normalised.
First, how do you build a lambda expression? Let’s consider the expression
Builder representation
build $ lam ‘x’
(term ‘x’)
¡ñ We use build to construct the lambda expression
¡ñ We use lam , similar to the way you use ¦Ë in lambda expressions.
¡ñ The first argument is the function input, in this case x
¡ñ The second argument is the return value of the
file:///Users/yichensong/Desktop/FIT2102 Assignment 1/Assignment 2 2022 Specifications.html 7/26
2022/10/8 20:16 Assignment 2 2022 Specifications
function. We use term to
Published using Google Docs identify this is a variable. Learn more Repo Abuse
Assignment 2 2022 Speci cations
Let’s look at the types of all these
expressions a little more.
Updated automatically every 5 minutes
lam :: Char ->
Builder -> Builder
¡ñ Takes a char, which represents the input variable
¡ñ Takes an expression of builder type, which represents the return value of the Lambda Expression
¡ñ Returns a value of Builder type.
term :: Char ->
¡ñ Takes a char, which represents the variable
¡ñ Returns a value of the Builder type.
build :: Builder ->
This should be the last step, which takes a builder and constructs the `Lambda` expression.
¡ñ Takes a Builder type
¡ñ Returns a Lambda type.
¡ñ Note: this fails if the
expression contains free variables.
ap :: Builder ->
Builder -> Builder
Combines two Builder expressions by applying one builder to another
file:///Users/yichensong/Desktop/FIT2102 Assignment 1/Assignment 2 2022 Specifications.html 8/26
2022/10/8 20:16 Assignment 2 2022 Specifications
Published using Google Docs
Learn more Repo Abuse
Updated automatically every 5 minutes
Takes a builder
Takes another builder
Returns a builder, where
the second builder will be
applied to the first builder
Assignment 2 2022 Speci cations
For example,
(¦Ëx.x)(¦Ëx.x)
let id = lam ‘x’
(term ‘x’) —
expression
build $ id `ap`
>>> (\x.x)\x.x
A more complex example:
(¦Ëb.(¦Ët.(¦Ëf.b t f)))
build $ lam ‘b’ $
lam ‘t’ $ lam ‘f’
((term ‘b’) `ap`
(term ‘t’) `ap`
(term ‘f’))
>>> \btf.btf
normal :: Lambda ->
Normalises a Lambda expression by reducing it to Beta-Normal Form.
NOTE: This will cause an infinite loop if you try to normalise a divergent expression.
let k = lam ‘x’ $
lam ‘y’ (term
let i = lam ‘x’
(term ‘x’)
normal $ build $
While you are not required to evaluate your lambda calculus expressions, we also provide you
file:///Users/yichensong/Desktop/FIT2102 Assignment 1/Assignment 2 2022 Specifications.html 9/26
2022/10/8 20:16 Assignment 2 2022 Specifications
with some evaluator functions to
aid in testing:
Published using Google Docs Learn more Repo Abuse
Assignment 2 2022 Speci cations
> Maybe Bool:
lamToBool :: Lambda – Updated automatically every 5
Normalises a Lambda expression and then returns its Boolean evaluation (if it has one).
lamToInt :: Lambda ->
Maybe Int:
Normalises a Lambda expression and then returns its numeric evaluation (if it has one).
Additional functions and types
Feel free to have a look at the ‘Builder.hs’ file, which will provide some tests showing more usage of this type. Note that it is not important that you understand how these functions work, just that you understand how to
There are many other well- documented functions that you can look at and use. Remember that it is not necessary to understand the implementation, only their usage (think of it like a library that you use).
These exercises provide a structured approach for creating an interpreter.
¡ñ Part 1: parsing lambda expressions
¡ñ Part 2: simple arithmetic and boolean operations
file:///Users/yichensong/Desktop/FIT2102 Assignment 1/Assignment 2 2022 Specifications.html 10/26
2022/10/8 20:16 Assignment 2 2022 Specifications
¡ñ Part 3: extending the
Published using Google Docs interpreter to handle moreLearn more Repo Abuse
programmatic operations
Assignment 2 2022 Speci cIaMtPioOnRsTANT: In each of the exercises, there will be
Updated automatically every 5 minutes
¡ñ Deliverables: Functions/parsers that you must implement or documentation you have to complete to successfully complete the exercise
¡ð The functions/parsers must be named and have the same type signature as specified in the exercise otherwise they will break our tests
¡ð These functions must be
implemented in
submission/LambdaParser.hs as per the code bundle
otherwise they will
break out tests
¡ñ Recommended steps: How to get started on the exercise. These are suggestions and you may wish to use a different approach
Basic tests will be provided, however it is important to construct your own unit
tests and add to the existing tests for each task to aid in your development. Similarly, the tests provided will not be a proof of correctness as they may not be exhaustive, so it¡¯s important you ensure that your code is provably correct.
Marks for the tasks will come from
¡ñ Correct implementations (i.e. passes the tests provided and our own tests)
file:///Users/yichensong/Desktop/FIT2102 Assignment 1/Assignment 2 2022 Specifications.html 11/26
2022/10/8 20:16 Assignment 2 2022 Specifications
¡ñ Effective usage of
Published using Google Docs course content (HOF, Learn more Repo Abuse
Functor, Applicative, Monad, etc.)
¡ñ Good code
Updated automatically every 5 minutes
Assignment 2 2022 Speci cations
quality (functional/declarative style, readability, structure, documentation etc.).
Please refer to the Marking rubric section for more information.
Part 1 (10 marks)
By the end of this section, we will have a parser for lambda calculus expressions.
Exercise 1 (2 marks): Construct a BNF Grammar for lambda calculus expressions
Deliverables
At the end of this exercise, we should have the following:
¡ñ A BNF grammar to demonstrate the structure of the lambda expression parser, representing both short and long form in one grammar (as your parser should also handle both short and long form).
Recommended steps
1. Construct parsers for lambda calculus expression components (¡°¦Ë¡±, ¡°.¡±, ¡°(¡°, ¡°)¡±, ¡°x¡±, ¡°y¡±, ¡°z¡±, etc.)
2. Use the component parsers to create parsers for simple combinators to get familiar with parsing
file:///Users/yichensong/Desktop/FIT2102 Assignment 1/Assignment 2 2022 Specifications.html 12/26
2022/10/8 20:16 Assignment 2 2022 Specifications
lambda expressions and
Published using Google Docs their structure Learn more Repo Abuse 3. Construct a BNF grammar
for short form and long
form lambda expressions Updated automatically every 5
Assignment 2 2022 Speci cations
Exercise 2 (4 marks): Construct a parser for long form lambda calculus expressions
Deliverables
At the end of this exercise, we should have at least the following parser:
¡ñ longLambdaP :: Parser Lambda
¡ð Parses a long form lambda calculus expression
Your BNF grammar must match your parsers. It is highly recommended to use the same name for your parsers and non- terminals (i.e. a non-terminal like
lambdaChar parser) so your marker can easily validate the grammar.
Recommended steps
1. Build a general purpose lambda calculus parser combinator which:
a. Parses general multi variable
lambda expressions/function bodies
Note default
associativity, e.g.
(xx)y a. Parses general
multi variable lambda expressions/function
file:///Users/yichensong/Desktop/FIT2102 Assignment 1/Assignment 2 2022 Specifications.html 13/26
2022/10/8 20:16 Assignment 2 2022 Specifications
Published using Google Docs
bodies with brackets
Learn more Repo Abuse
Assignment 2 2022 Speci cations
Parses any valid lambda calculus expression using long-form syntax
– E.g. (¦Ëb.
(¦Ët. (¦Ëf.b t f)))
Updated automatically every 5 minutes
– E.g ¦Ëxy.x(xy)
Exercise 3 (4 marks): Construct a parser for short form lambda calculus expressions
Deliverables
At the end of this exercise, we should have at least the following parsers:
¡ñ shortLambdaP :: Parser Lambda
¡ð Parses a short form lambda calculus expression
¡ñ lambdaP :: Parser Lambda
¡ð Parses both long form and short form lambda calculus expressions
Similar to Exercise 2, your parser must match your BNF grammar.
Recommended steps
1. Build a general purpose lambda calculus parser combinator which:
a. Parses any valid lambda expression using short-form syntax
– E.g. ¦Ëbtf.b t
file:///Users/yichensong/Desktop/FIT2102 Assignment 1/Assignment 2 2022 Specifications.html 14/26
2022/10/8 20:16 Assignment 2 2022 Specifications
Published using Google Docs Learn more Repo Abuse Part 2 (8 marks)
By the end of this section, we should Updated automatically every 5 Assignment 2 2022 Speci cations
be able to parse arithmetic and minutes logical expressions into their
equivalent lambda calculus expressions.
Exercise 1 (2 marks): Construct a parser for logical statements
Deliverables
At the end of this exercise, you should have the following parsers:
¡ñ logicP :: Parser Lambda
¡ð Parse simple to complex logical clauses
Recommended steps
1. Construct a parser for logical literals (¡°true¡±, ¡°false¡±) and operators (¡°and¡±, ¡°or¡±, ¡°not¡±, ¡°if¡±) into their church encoding
2. Use the logical component parsers to build a general logical parser combinator into the equivalent church encoding, which:
a. Correctly negates a given expression
i. E.g. not not True
b. Parses complex clauses with nested expressions
i. E.g. not True and False or
c. Parses expressions
with the correct order of operations (¡°()¡± -> ¡°not¡± -> ¡°and¡± -> ¡°or¡±)
Exercise 2 (4 marks): Construct a parser for
file:///Users/yichensong/Desktop/FIT2102 Assignment 1/Assignment 2 2022 Specifications.html 15/26
2022/10/8 20:16 Assignment 2 2022 Specifications
arithmetic expressions
Published using Google Docs Learn more Repo Abuse Requirements
At the end of this exercise, you Assignment 2 2022 Speci csahtoiounldshave the following
Updated automatically every 5 minutes
¡ñ basicArithmeticP :: Parser Lambda
¡ð Parses simple arithmetic
expressions (+, -) ¡ñ arithmeticP ::
Parser Lambda
¡ð Parses complex arithmetic
expressions (+, -, *, **, ()) with correct order of operations
Recommended steps
1. Construct a parser for natural numbers into their church encoding (¡°1¡±, ¡°2¡±, …)
2. Construct a parser for simple arithmetic operators with natural numbers into equivalent lambda expressions. (¡°+¡±, ¡°-¡±)
¡ð See the Parser combinators section of the notes for some examples
3. Construct a parser for complex mathematical expressions with natural numbers into their equivalent lambda expressions. (¡°*¡±, ¡°**¡±, ¡°()¡±)
¡ñ It may be useful to write a BNF for this
4. Using the component parsers built previously to build a parser combinator for complex arithmetic expressions.
¡ñ Note: the correct order of operations, e.g. 5 + 2 * 3 – 1 = 5 + (2 * 3) – 1
Exercise 3 (2 marks): Construct a parser for comparison expressions
file:///Users/yichensong/Desktop/FIT2102 Assignment 1/Assignment 2 2022 Specifications.html 16/26
2022/10/8 20:16 Assignment 2 2022 Specifications
Published using Google DRoecqsuirements Learn more Repo Abuse At the end of this exer
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com