Using PureScript's psci with pulp

Vincent Orr
InstructorVincent Orr

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 4 years ago

PureScript has a REPL (a console you can type expressions into) called pulp. In this lesson, we will show how you can use pulp to test your modules and functions. We will use code as we would in a file, like importing modules. We will also use REPL specific syntax like :type (or :t for short) to view type definitions and :reset to re-compile changes to our files.

Technologies talked about in this lesson:

Pulp by @bodil Psci Purescript

[00:01] To use PureScript's PSCi, we must make sure that we have the Bower dependency installed, which is PureScript PSCi support. I'm going to demonstrate PSCi using Pulp by typing in pulp psci from within your project directory. PSCi is simply PureScript's REPL, and is a great playground to explore your code.

[00:17] Let's have a quick look at the help section by typing in :?. I'll zoom out a little to make this clearer. As you can see, all the tools that come with PSCi start with colon, and then the expression name. Let's explore two of these tools.

[00:30] Here, we have :type, which will show the type of your function. Here is :reset. This will reset your imported modules. I'm going to clear my terminal, and now try some simple arithmetics. One plus one. That's weird. PSCi has just returned an error.

[00:46] It looks it has an unknown operator of plus. Why is that then? Well, the PureScript call is, in fact, really small. Most of its functionality needs to be imported via dependencies. One of these is called Prelude, and it's an important core dependency which you'll most likely use in every module.

[01:01] To do this, you simply type import Prelude, with a capital P. What you're doing here is exactly the same as you were inside a PureScript file. Let's have a quick look at one of these. We'll go into our src folder, and open up our Main.purs. Here, you can see import Prelude.

[01:16] Back to PSCi. Now, doing one plus one will return two.

[01:21] Now, let's demonstrate how we can use PSCi to become a playground for a module that you're working on. Here, we've named our module Main. We'll go back into PSCi, and we'll import that by typing import Main, remembering to have a capital M, as we do in our file.

[01:38] Now, looking at the type definition of our function main, what you can do is use PSCi to display that function definition. You simply type in :type main, and there you have. It's exactly the same as within our file, though the layout is a little different.

[01:52] You'll find yourself using this often. What you can do is just use :t. Now, we've seen the type of our function. Let's run it. All you've got to do is just type in the name of the function, which is main. That'll log, "Goodbye my poor sailor!"

[02:06] I'm going to make a change in the file to demonstrate how we can still use PSCi to test that change. Let's change the string to that log to, "You will make it back safe sailor!" This is turning into a right love story.

[02:18] Let's save our file, and go back into PSCi. We'll use one of our helpers, which is :reset. This will reset all of our imported modules. It spotted a change, and recompiled Main. You can reimport it, then run the function main.

[02:32] There you go. It's outputted the change that we made inside our file. Now, you can see that PSCi is a great playground to experiment with your code.