Skip to content

Online REPL Walkthrough

Dierk König edited this page May 30, 2020 · 12 revisions

In the online REPL, you can use the editor pane on the left hand side to insert and edit Frege code.

You can then run it by clicking the 'Evaluate' button at the bottom or by pressing 'Ctrl + Enter'.

This wiki page should provide you with some ideas that you may want to try in the Repl.

Please note that at the moment any printing to stdout does not lead to visible output. Only the value of the last evaluated expression is visible.

Using the REPL

The Read-Evaluate-Print-Loop (REPL) reads one command at a time, evaluates it, prints the result, and then waits for the next input. For editing multi-line input, it is easier to use the editor in the left panel.

Special commands that modify the behavior of the REPL start with a colon :, most notably :help.

You can also use the Up/Down arrow keys on your keyboard to browse through the history of previously entered commands.

The online REPL is only one of many REPL applications that Frege supports. They all share the same basic behavior and the same underlying interpreter. The basic REPL is contained in the Frege distribution, which uses it with a command-line interface and a Swing-based GUI. There also is JavaFX based REPL that uses FregeFX, and the online REPL that we talk about here.

Let's go through some typical Frege features.

Immutability

Start with creating a list and assign it to the reference myList.

frege> myList = [1,2,3]
value myList :: [Int]

The REPL tells you the type of the last evaluated expression. Read this as myList is of type (::) List ([ ]) of Integer (Int).

Now try to assign a new value to myList:

frege> myList = [5,6,7]
2: E .fr:2: function binding without patterns must have only a single equation

Here E calls out an error at line 2. You are not allowed to assign new values to existing references. Never. (We explain the error message below.)

If you changed your mind, what myList should be, you either have to give it a new name or reset the evaluation context:

frege> :reset

frege> myList = [5,6,7]
value myList :: [Int]

After reset, your command history is still intact but all previously known references have gone. You can use :browse to see the currently known references:

frege> :browse
value myList :: [Int]

frege> :reset

frege> :browse

Explaining the error message

When we tried to assign a new value to 'myList' we got the error message function binding without patterns must have only a single equation. This needs a bit of explanation.

Since all references in Frege are immutable, we do not even get an error message like you cannot change an immutable value. That's supposed to be obvious.

But there are cases when it looks like as if you would have multiple assignments. They occur for example when defining a function like multiply m n = m * n and you would like to care for the special value 0, where we know the answer without calculating.

multiply m 0 = 0
multiply 0 n = 0
multiply m n = m * n

Here, we use so-called patterns in the parameter list of the multiply function to short-circuit the special cases.

If you view myList not as a constant but as a function with no parameters, then it can only be defined with one expression since no patterns can possibly apply. Hence the error message. It is actually more likely that you forgot to add a parameter.

BTW: in a pure language like Frege where functions have no visible side effects and return the same value if given the same arguments, any function without arguments must return a constant value.

Converting to JSON

Copy the following code in the editor and evaluate it.

This will show the :load editor command in the repl and some evaluation result. You can now run the run function by typing run in the repl.

module examples.JSONExample where

import Data.JSON

data Coordinate = Coordinate { lon :: Float, lat :: Float }
derive Show Coordinate

data Weather = Weather { loc :: Coordinate
                       , temp :: Int
                       , desc :: String
                       , alert :: Maybe String
                       }
derive Show Weather

instance ToJSON Coordinate where
  toJSON coord = Struct [ ("lon", toJSON coord.lon), ("lat", toJSON coord.lat) ]


instance ToJSON Weather where
  toJSON Weather{loc, temp, desc, alert}
    = Struct [ ("loc", toJSON loc)
             , ("temp", toJSON temp)
             , ("desc", toJSON desc)
             , ("alert", toJSON $ fromMaybe "null" alert)
             ]

run = toJSON $ Weather location 98 "overcast clouds" (Just "Excessive heat")
  where
    location = Coordinate { lon = -122.09, lat = 37.39 }
Clone this wiki locally