Statements

Statements are useful when we want to cause some side effects (other than computing a value).

Side effects in Keelung include:

  • Output some values (or nothing)

  • Ask for inputs

  • Impose constraints on variables with assertions

Statements are Monadic

You can skip this section if you are familiar with monadic programming

This means that the type of statements have the form of:

arg0 -> arg1 -> ... -> argN -> Comp result

Applying all arguments to a statement will result in a term of type Comp result. However, it's generally impossible to extract result from Comp result unless we can provide it with certain context.

Here, we will use inputList as an example to demonstrate how to extract values from a monadic function:

inputList :: Input Access -> Int -> Comp [t]

We can specify how much inputs we want by supplying the length of the array, say 4:

inputList Public 4 :: Comp [t]

To extract the result (of type [t]) and bind it to an identifier, we use do-notation:

someProgram = do 
  result <- inputList Public 4 
  ...

Here, the type of result will have type [t] instead of Comp [t]

return

We can think of return as the inverse of <- in do-notations.

Use it at the end of our program to output some value.

return :: a -> Comp a

It's a common practice for a statement to return Comp () when there's nothing to return, like void functions in C.

Last updated

Logo

Copyright © 2023 BTQ