COMP1100/1130 [2020 S1]:
PROGRAMMING AS PROBLEM SOLVING Research School of Computer Science
Menu
menu
» Labs » Week 2: Gitlab, VSCodium and More Haskell
This week’s lab will introduce you to more important parts of the computing environment of this course, particularly Gitlab, which will be used every time you submit work. We will then further introduce you to programming with Haskell.
Table of Contents
Pre-Lab Checklist
Gitlab
Generating a new SSH key and adding it to Gitlab (Recommended)
Generating a new SSH key pair
Adding your SSH key to your Gitlab account ConXguring Git
Crash Course on using Git with VSCodium Forking a project
Cloning a project
Pull changes from remote repository Git commit local changes
Git push changes to remote repository Git work[ow
Angle conversion with Haskell
Haskell integration in VSCodium
Area of a Triangle
GHCi commands
Merge con[icts (Optional)
Pre-lab Checklist
Have completed Lab 1 tasks
Have read lecture material prior to this lab
Be able to locate VSCodium and the Terminal on lab computers Be able to execute basic bash commands e.g. cd, pwd, etc
Be able to execute basic commands in GHCi
Gitlab
CECS Gitlab CECS operates a teaching focussed Gitlab service http://gitlab.cecs.anu.edu.au/, which we will be using throughout the semester and will be used in other courses in the University as well (COMP1110 etc.). In your own time, feel free to check out the CECS Gitlab Guide.
Gitlab As mentioned in the previous lab, GitLab is an online Git repository manager with a wiki, issue tracking, continuous integration, and continuous deployment.
Git A free and open source version control system (VCS), originally developed by Linus Torvalds in 2005, the famous creator of the Linux operating system kernel. Git is an example of a Distributed Version Control System (DVCS). Instead of having only one single place for the full version history of the software as is common in once-popular version control systems like CVS or SVN, in Git, every developer’s working copy of the code is also a repository that can contain the full history of all changes. (Source)
Generating a new SSH key and adding it to Gitlab (Recommended)
Before working with Gitlab and VSCodium, we highly recommend you complete following the next section.
The SSH protocol provides a secure communication channel for sharing information and allows the user to authenticate to the GitLab remote server without supplying your username or password each time.
Generating a new SSH key pair
To create a new SSH key pair:
1. Open Terminal app.
2. Generate a new RSA SSH key pair. Paste the following text, substituting in your
ANU email address.
The -C [ag adds a comment in the key in case you have multiple of them and want to tell which is which.
This creates a new SSH key, using the provided email as a label.
3. When you’re prompted to “Enter a Xle in which to save the key,” press Enter. This accepts the default Xle location.
[
You will need to repeat these steps if you wish to generate and add a SSH key for your own personal device.
ssh-keygen -t rsa -C “uXXXXXXX@anu.edu.au”
Generating public/private rsa key pair.
Enter a file in which to save the key (/students/uXXXXXXX/.ssh/id_rsa):
ssh-add -l
xclip -sel clip < ~/.ssh/id_rsa.pub
*AngleConversion> 30 – radiansToDegrees (degreesToRadians 30)
*AngleConversion> radiansToDegrees (degreesToRadians 30)
4. Once the path is decided, you will be prompted to input a password to secure your new SSH key pair. Skip this by pressing Enter twice.
5. Lastly, type the following and press Enter.
Adding your SSH key to your Gitlab account
Let us add the newly created public key to your ANU Gitlab account.
1. Copy your public SSH key to the clipboard by using the following command in terminal:
This copies the ssh key which is now ready to be pasted in Gitlab.
2. The Xnal step is to add your public SSH key to GitLab. Navigate to the SSH Keys tab
in your User Settings on the CECS Gitlab website.
Click your avatar in the upper right corner and select Settings.
Navigate to the SSH Keys tab in your User Settings.
Paste your key in the ‘Key’ section and give it a relevant ‘Title’. Use an identiXable title like ‘ANU CECS lab’.
This guide has been inspired by resources already available online. Please feel free to check out 1, 2.
ConXguring Git
If this is your Xrst time using Git on the lab machine, you must conXgure Git by running two commands in the Terminal, replacing your name and your email in the respective lines.
Crash Course on using Git with VSCodium
In this section we will learn to do the following:
Create our own copy of a Gitlab repository (a collection of commits, and branches and tags to identify commits)
Create a copy of the repository on our local machine
Sync a local repository with a remote repository
Commit and push changes to a remote repository
Forking a project
This creates your own fork (copy) of a repository. Forking a repository allows us to commit our own changes without modifying the original project.
1. Using a browser, go to the repository that you will wish to make a copy of. The url for this lab is https://gitlab.cecs.anu.edu.au/comp1100/2020s1studentXles/lab02.
2. Go to the project’s dashboard under the Project tab and click on the Fork button.
3. You will be asked where to fork the repository. Click on the user or group to where you’d like to add the forked project. You should see your own name here.
Once you have successfully forked a project, you will be redirected to a new webpage where you will notice your name before > project-name. This means you are now the owner of this repository. The url in your browser should re[ect the change: https://gitlab.cecs.anu.edu.au/uXXXXXXX/Lab02
Here we are dealing with remote repositories.
A remote repository stores versions of project Xles in the “cloud” at some server site. The CECS GitLab Web site (i.e., https://gitlab.cecs.anu.edu.au) is the server we will be using.
Cloning a project
We can only work on projects on a local repository.
Cloning creates a local repository, storing the Xle on your local machine (i.e., the lab machine you are currently working on or your personal computer).
1. Navigate to the Gitlab webpage containing the project you wish to clone – the remote repository that you own. Click the Clone button, which is next to the Fork button we used earlier, copy the SSH link by choosing Copy to Clipboard. This copies the URL of the remote repository.
2. On your lab machine, launch VSCodium.
3. Next, we are going to open the Command Palette by pressing Ctrl + Shift +
P.
1. IntheCommandPalette,typegitcloneandpressEnter.
2. Paste the remote repository URL that you copied in step 1 into the Command Palette (by pressing Ctrl + v) and then press Enter.
If you set a password for your SSH key, you’ll have to enter it in every time, which is what we’d like to avoid in the Xrst place.
Enter passphrase (empty for no passphrase): [Type a passphrase] Enter same passphrase again: [Type passphrase again]
git config –global user.name “Your Name”
git config –global user.email uid@anu.edu.au
Disclaimer: We are only going to go through the essentials of setting up and using Git with VSCodium. If you wish to explore more options with using Git with VSCodium, check out their guide. We will not be demonstrating using command line for Git. If you wish to learn this yourself, this guide by Gitlab could be a great starting point.
If you don’t fork the project, you’ll be trying to push your work to the same public repo everyone is forking from, which Gitlab will prevent you from doing. Pause for a minute, and consider why this might be – ask your peers or tutor if you have questions.
Remember this. The Command Palette will be how you interact with many of VSCodium’s features, including Git.
3. Navigate to the comp1100/labs folder that you created in Lab1 and then press the Select Repository Location button in the top right.
4. A popup will appear in the bottom right. Click Open.
Congratulations! If you see a new window with the project Xles, you have successfully cloned the project. Now you are ready to edit the Xles in the project.
Pull changes from remote repository
Pulling changes from the remote repository ensures that our local Xles are synced with our previous work, if any. Although there are multiple ways of achieving this, we will focus on only pulling in this guide.
1. Open the Command Palette again by pressing Ctrl + Shift + P.
2. Type git pull and select the option Git: Pull to fetch and apply changes (if any)
from the selected remote repository.
3. [Optional] If you have made changes to your local repository before pulling, you may have the unpleasant job of dealing with Merge Con[icts, which we will get into later.
Since there are no new changes to the remote repository (you just cloned an identical copy a few minutes ago!) nothing should happen after following step 1 and 2.
Angle conversion with Haskell Haskell integration in VSCodium
In this lab and the following weeks, we will be using ghcid to do realtime error display within the text editor of VSCodium, as well as a summary of warnings and errors down the bottom of the window.
1. Open the Command Palette again by pressing Ctrl + Shift + P.
2. Type start ghcid and select the option Start Ghcid. A box should appear in the bottom of your window which probably says “All good” in green text (if not, it should start displaying feedback after making any changes to a Xle). This is your ghcid feedback display.
3. Next, click on the Split Terminal button. You should now see the ghcid feedback display side-by-side with a Terminal emulator, which you can use to run your code in GHCi.
Exercise 1
In order to complete this activity, open the Xle AngleConversion.hs in the VSCodium text editor (double click it on the left hand side of the window).
In this Xle, you will Xnd that we have already deXned a function for you: degreesToRadians which, as the name suggests, converts values of angles from degrees to radians.
The equation used for the deXned function is: radians = degrees × π 180o
You task is to complete the function radiansToDegrees with appropriate:
short comment useful to describe its funtionality type signature declaration
deXnition of the function itself
Here is the mathematical equation required: degrees = radians × 180o π
This is not a test. If you are confused, please ask for assistance from your tutor or your classmates.
More details regarding radians can be found here. Floating point numbers – a word of caution
Floating point numbers are imprecise – a/b*b==a does not hold in general (ref). Irrational numbers. which are real numbers that cannot be written as a simple fraction, can lead to this imprecision e.g. pi, sqrt 2, etc.
Let us look at a simple yet universal example. In the following example, (==) is used to check equality, and returns true if the values of the arguments are equal, and false otherwise. e.g. 3 == 3 returns True
We all know 0.1 + 0.2 = 0.3 based on simple arithmetic, let us test this in Haskell. Enter GHCi by typing ghci in terminal.
What do you see? Is this the answer you expected? Now try
Let us also look at the functions in AngleConversion.hs. In terms of the mathematics involved, we can all agree that
30o = radiansToDegrees ( degreesToRadians 30o)
which means that
30o − radiansToDegrees ( degreesToRadians 30o) = 0
Let us try that in ghci (Don’t forget to load the AngleConversion module):
What did you get? How about the following:
As we mentioned above, [oating point representations for irrational numbers such as pi can be imprecise.
Long story short: Internally, computers use a format (binary [oating-point) that cannot accurately represent decimals such as 0.1, 0.2 or 0.3 at all. When the expression is interpreted, “0.1” is already rounded to the nearest number in that format, which results in a small rounding error before the calculation happens.
Check out this website for more details. There are also numerous websites that go into further details regarding Floating point rounding errors, these are only a Google search away.
As an analogy, imagine writing down the decimal expansion of 1/3 to only a few places ( 0.333) and then multiplying this by three. You end up with 0.999, not 1, which is essentially what causes the computer to make the error above.
Git commit local changes
1. Open the Command Palette again by pressing Ctrl + Shift + P.
2. Type git commit all and select the option Git: Commit All.
3. Write a useful description in the Message box that appears. A useful message in this case could be “Added deXnition of radiansToDegrees function” or “deXned radiansToDegrees function”. It is important to make the message useful.
If this is your Xrst time committing in VSCodium, it may ask you whether you want to automatically stage commits. Click Always if this appears.
If an error appears like the following, ensure you have followed the instructions at above.
Git push changes to the remote repository
1. Open the Command Palette again by pressing Ctrl + Shift + P.
2. Type git push and select the option Git: Push.
3. Check that your work has actually been pushed to the remote repository by visiting your copy of the Lab2 repository on the Gitlab website. Hint: it will probably be at the URL: https://gitlab.cecs.anu.edu.au/uXXXXXXX/lab02, but with your uID instead of uXXXXXXX.
Git work[ow
The following diagram provides a pictorial representation of the work[ow that we followed in the previous section. Please refer to this work[ow throughout the semester and follow the steps numerically from 1 through to 5 in the lab computer as well as your personal computer. Please ask your tutor if you are unsure about any of the following steps.
Area of a Triangle Exercise 2
In the same directory as AngleConversion.hs
Copy the Xle Area.hs from the previous lab.
Add the module information at the top of the Xle similar to AngleConversion.hs And then …
Write a function that takes in 3 [oating numbers, representing the three sides of a triangle, and outputs a [oating number as the answer, representing the area of such triangle. The area of a triangle with sides a, b and c can be deXned as
It is very important that you perform this step before making any changes to your local Xles every time. Otherwise your local and remote repositories may get out of sync in ways that are tricky to Xx.
This must be started manually, and you will need to do this (the following steps) every time you open or reopen a new project in VSCodium.
Prelude> 0.1 + 0.2 == 0.3 …
Prelude> 0.1 + 0.2 …
You will need to restart ghcid to enable the realtime error display for the new Xle. You can do so by repeating the ghcid instructions.
where
Again, again …
area△ =√s(s−a)(s−b)(s−c) s= a+b+c
2
What do you think is a useful function name in this case?
What do you think should be the type signature of this function?
What do you think should be on the second line of this function?
What could you include as a comment for the function that is useful to the reader?
You may Xnd where clause useful in this case:
In this example, cube is a helper function (function that performs part of the computation of another function) that cubes a given number and its functionality can be hidden away in a where clause. This avoids code repetition, which contributes towards code style, which helps with debugging the functions more easily when required. More details on this later.
Some test cases that you could compare your function with:
Write or run some more tests to help give you conXdence that your function is correct.
GHCi commands
GHCi commands all begin with : and consist of a single command name followed by zero or more parameters. The command name may be abbreviated, with ambiguities being resolved in favour of the more commonly used commands. A more comprehensive list of commands can be found here.
Adapted from S. Thompson, Haskell: the craft of functional programming. Some exercises to try in your own time using GHCi commands:
How do I list all the functions in scope for Area.hs? What is the type of areaTriangle?
How do I Xnd more information about the Double type?
Where can I search Haskell commands and keywords?
The Haskell API search engine can be very useful, or you can search the Prelude source
code.
Merge Con[icts [Optional]
These instructions have been attached here as reference. You do not need to use these instructions in this lab.
If you have not followed the Git work[ow, you may encounter merge con[icts. Merge con[icts occur when different changes are made to the same line of a Xle. VSCodium may inform you of a merge con[ict by giving a message “Can’t push refs to remote. Try running ‘Pull’ Xrst to integrate your changes”.
Opening the Git log will explain that the remote repo contains changes that you do not have locally.
To resolve merge con[icts:
1. Run Git: Pull from the Command Palette.
2. In the VSCodium text editor, you will see the con[icting changes, one from your local repo and one from the remote repo. They will be labelled as such: “Current Change” and “Incoming Change”, respectively.
3. To resolve the con[ict, you must choose one to keep. You can do so by either clicking Accept Current Change or Accept Incoming Change button.
4. Finalise your choice by continuing to commit and push your work through the Command Palette as described in the sections above.
References
[1] GHCi commands,
https://downloads.haskell.org/~ghc/7.4.1/docs/html/users_guide/ghci- commands.html
— add the cubes of two numbers
sumCubes :: Double -> Double -> Double sumCubes m n = cube m + cube n
where
cube x = x * x * x
*Area> areaTriangle 3 4 5 6.0
*Area> areaTriangle 10 5 7 16.24807680927192
*Area> areaTriangle 3 3 3 3.897114317029974
You are required to submit your attempts at the exercises above in order to receive full participation marks for this lab. You have until the start of your next lab to do this. You should submit by committing and pushing all your changes to your Gitlab repository. You should have the exercises from the previous lab in this submission as well, so that you can receive full participation marks for that lab.
Command
Abbreviations
Action
:load
:l
Load the Haskell module
:reload
:r
Reload the module loaded by the last :load command
:browse
:bro
List the functions deXned in the module you just loaded.
:cd directory
Changes directory just like in Bash.
:type
:t
Show the type of the expression e.g. :t True shows True :: Bool
:info
:i
Shows information about the speciXed “name”, it could be a function, type constructor, class etc.
:help
😕
Give a complete list of GHCi commands.
:! command
Execute a shell command. e.g. :! pwd.
:quit
:q
Exit from GHCi and return to shell.
↑, ↓
Move up and down in command history.
TAB
Completion of name or command from partial entry. e.g. Tr
let x = pi
Give x the value of pi in this GHCi session.
Updated: 06 Jun 2020
Responsible Oscer: Director, RSCS
Page Contact:
Contact ANU Copyright Disclaimer
Course Convenor
+61 2 6125 5111
The Australian National University, Canberra
Disclaimer
Privacy
Freedom of Information
University, Canberra CRICOS Provider : 00120C ABN : 52 234 063 906