Type Declaration Keywords

These keywords are used in type declarations and type expressions.

Overview

Keyword Purpose
DECLARE Type definition
IS Type assertion / definition
HAS Record field declaration
ONE OF Enum variant declaration
OF Type application
WITH Record construction
LIST List type / literal
FUNCTION FROM TO Function type
TYPE Kind of types

DECLARE

Begins a new type definition.

DECLARE Person HAS
  name IS A STRING
  age IS A NUMBER

IS

Connects a name to its type or definition.

In Type Declarations

DECLARE Person HAS
  name IS A STRING
  age IS A NUMBER

In Assumptions

ASSUME x IS A NUMBER
ASSUME f IS A FUNCTION FROM NUMBER TO NUMBER

In Definitions

DECIDE answer IS 42
DECIDE greeting IS "Hello"

HAS

Declares fields in a record type.

Syntax

DECLARE TypeName HAS
  field1 IS A Type1
  field2 IS A Type2

Example

DECLARE Person HAS
  name IS A STRING
  age IS A NUMBER
  employed IS A BOOLEAN

DECLARE and

-- Example: Record type declaration
-- Demonstrates product types with named fields

DECLARE Person
  HAS `full name` IS A STRING
      age         IS A NUMBER

-- Example: Creating a person record
`the applicant` MEANS Person WITH
  `full name` IS "John Doe"
  age         IS 30

#EVAL `the applicant`'s `full name`
#EVAL `the applicant`'s age

ONE OF

Declares variants in an enum (sum) type.

Syntax

DECLARE TypeName IS ONE OF
  Variant1
  Variant2
  Variant3 HAS field IS A Type

Example

DECLARE Status IS ONE OF
  Active
  Inactive
  Pending HAS reason IS A STRING

DECLARE and

-- Example: Enum type declaration
-- Demonstrates sum types with named constructors

DECLARE Color IS ONE OF
  Red
  Green
  Blue

-- Example usage
`my favorite color` MEANS Red

`name of the color` MEANS
  CONSIDER `my favorite color`
  WHEN Red   THEN "red"
  WHEN Green THEN "green"
  WHEN Blue  THEN "blue"

#EVAL `name of the color`

OF

Used for type application (applying type constructors to arguments).

To create type instances

DECLARE Person HAS
    age IS A NUMBER
    name IS A STRING

person1 MEANS Person OF 20, "John"  -- Explicit form using OF
person2 MEANS Person 21, "Jill"     -- Short form

Within ASSUME

DECLARE Container a HAS value IS A a

ASSUME box IS A Container OF NUMBER

WITH

Constructs record values by specifying field values.

Syntax

TypeName WITH
  field1 IS value1
  field2 IS value2

Example

DECLARE Person HAS
  name IS A STRING
  age IS A NUMBER

DECIDE john IS Person WITH
  name IS "John"
  age IS 30

LIST

Both a type constructor and literal syntax.

As Type

ASSUME numbers IS A LIST OF NUMBER
ASSUME names IS A LIST OF STRING

As Literal

DECIDE myList IS LIST 1, 2, 3, 4, 5
DECIDE empty IS EMPTY
-- Example: List type usage
-- Demonstrates list literals and cons operator

-- List literal syntax
`first list` MEANS LIST 1, 2, 3

-- Cons operator (FOLLOWED BY)
`second list` MEANS 1 FOLLOWED BY 2 FOLLOWED BY 3 FOLLOWED BY EMPTY

#EVAL `first list`
#EVAL `second list`

FUNCTION FROM TO

Declares function types.

Syntax

FUNCTION FROM InputType TO OutputType
FUNCTION FROM Type1 AND Type2 TO ResultType

Examples

-- Single parameter
ASSUME f IS A FUNCTION FROM NUMBER TO NUMBER

-- Multiple parameters (using AND)
ASSUME g IS A FUNCTION FROM NUMBER AND STRING TO BOOLEAN
-- Example: Function type
-- Demonstrates function type signature

GIVEN x IS A NUMBER
      y IS A NUMBER
GIVETH A NUMBER
add x y MEANS x PLUS y

#EVAL add 5 3

TYPE

The kind of types (used in type-level programming).

Example

GIVEN a IS A TYPE
      x IS A a
identity x MEANS x

See Also