Skip to main content

Other Information

literal

In PureScript (and many programming languages), the term "literal" refers to syntax that allows you to directly write a value of a specific type in your code, without needing to construct it through functions or other runtime operations.

A string literal is text written directly between double quotes ("..."):

-- A string literal representing the value "hello"
str :: String
str = "hello"

An array literal is a list of values written between square brackets ([ ... ]):

-- An array literal with the values 1, 2, 3
arr :: Array Int
arr = [1, 2, 3]

Contrast with Non-Literal Constructs:

-- Non-literal (constructed at runtime):
dynamicArray = Array.cons 1 (Array.cons 2 (Array.cons 3 []))
dynamicString = String.fromCharArray ['h', 'e', 'l', 'l', 'o']

Why "Literal" Matters:

  • Compile-Time Known: Literals are fixed values embedded in the source code.
  • No Runtime Overhead: They don’t require functions like Array.from or new Object().
  • Pattern Matching: Can be used directly in patterns:

Smart constructors

Let's now look at a real-world functional design pattern in PureScript using:

Opaque types (hiding constructors),

Smart constructors (controlled creation),

Modules as APIs (interface boundaries). https://chatgpt.com/s/t_68a5dd7aa36481919b5ecef4738d0776

value has a type, type has a kind A data constructor is a function that constructs a value of a data type Type = describes what kind of data something is Constructor = makes values of that type

data Shape = Circle Number Number Number
| Rectangle Number Number Number Number

This defines a type named Shape, and two data constructors:

Summary

ConceptRoleExample
TypeCategory of valuesShape
ConstructorFunction to create valuesCircle 1.0 1.0 5.0
ValueActual instance of a typemyShape = Circle 1.0 1.0 5.0
Pattern matchUse constructors to handle datacase shape of Circle x y r -> ...
ValueTypeConstructor?Notes
7Int❌ NoneLiteral for a primitive type
trueBoolean❌ NoneSame
CircleShapeCircleUser-defined data constructor
Just 5MaybeJustBuilt-in algebraic data type

why Unknown data constructor

what :t is doing: based on value to find out the type, value is constructed with data/value constructor.

> :t Int
> Error found:
in module $PSCI
at :1:1 - 1:4 (line 1, column 1 - line 1, column 4)

Unknown data constructor Int

> :t Circle
Number -> Number -> Number -> Shape

> :t Circle 0.1 0.2 0.3
Shape

> :t Circle 0.1 0.2
Number -> Shape