Flex Standard Library
The Flex Standard Library Overview
This page of documentation is intended to provide an overview of functionality built into the Flex specification language. Included in this page is documentation on Flex's primitive types, any functionality a user needs to use these types, and an overview of included syntax.
Primitive Types
Flex has a collection of primitive types that are automatically available without any imports. These are as follows:
bit
-- eithertrue
orfalse
intN
-- a value from-2^(N-1)
to2^(N-1) - 1
inclusive, whereN
is a positive integerTranspiler supported widths: int8, int16, int32, int64
uintN
-- a value from0
to2^N - 1
inclusive, whereN
is a positive integerTranspiler supported widths: uint8, uint16, uint32, uint64
float32
-- a single precision floating point number as defined by the IEEE 754 standardfloat64
-- a double precision floating point number as defined by the IEEE 754 standardstring
-- a sequence of characters (e.g.,"foo"
)
Flex also has several primitive type constructors with special syntax. If T
, T0
, T1
, ..., Tn
are arbitrary types, then the following are also types:
T[]
-- an array of values of typeT
Optional<T>
-- eithernone
orsome(t)
wheret: T
(T0, T1, ..., Tn)
-- a tuple
For more details see the expressions section.
Built-in Functions
length
flex
function length<T>(x : T[]) -> int32
Returns the length (number of elements) of the array argument.
last
flex
function last<T>(x : T[]) -> T
Returns the last element of the array argument.
Note: fails if the argument array is empty.
floor
flex
function floor(x : float64) -> Optional<int64>
Returns the largest possible 64 bit integer value which is less than or equal to the argument.
float64to32
flex
function float64to32(x : float64) -> float32
Returns the 32 bit version of the argument .
Note: since
float64
is has two times the precision offloat32
this conversion is an approximation of the original argument.
sin
flex
function sin(x : float64) -> float64
Returns the sine of the argument.
Note: the argument is expected to be in radians.
cos
flex
function cos(x : float64) -> float64
Returns the cosine of the argument.
Note: the argument is expected to be in radians.
tan
flex
function tan(x : float64) -> float64
Returns the tangent of the argument.
Note: the argument is expected to be in radians.
atan2
flex
function atan2(y : float64, x: float64) -> float64
Returns the theta component of the point (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates.
Parameters
y: the abscissa coordinate
x: the ordinate coordinate
asin
flex
function asin(x : float64) -> float64
Returns the arc sine of the argument.
Note: the argument is expected to be in radians.
sqrt
flex
function sqrt(x : float64) -> float64
Returns the square root of the argument.
Note: If
sqrt
is given a negative argument a failure occurs.The transpiler is responsible for determining what a failure means in the output language.
pow
flex
function pow(x : float64, y : float64) -> float64
pow(x,y)
Returns x
raised to the power of y
.
getOrElse
flex
function getOrElse<T>(x : Optional<T>, y: T) -> T
Returns the v
if x
is some(v)
, or a default value y
when x
is none
. Used with optional data types, and value casting. For more details see Value Casting.
some
Constructor for an the Optional
type taking a single argument. For more details see Pattern Matching > Some and None
Written
some(x)
for anx
of any type.
none
Constructor for an optional value taking no arguments. For more details see Pattern Matching > Some and None
Written:
none
constructing a typeOptional<T>
const x : Optional<float64> = none;
Syntax Overview
- Flex Grammar Specification — Complete specification of the grammar of Flex in PEG format.
Keywords
const
Declare an immutable global value
Type ascription is required
flex
const x : int32 = 0;
struct
Declare a structured type composed of fields that each have an explicitly declared type
flex
struct Position {x : float32;y : float32;};
message
Declare a message type that can be used as a Message in Tangram ProTM, and used as input or output for a transform
Applies to struct declaration
Creates an ontologically significant term to be used in a Transform
flex
message struct Position {x : float32;y : float32;};
newtype
Declare a new struct like type containing a single field
Allows a user to get compile time guarantees about normally non distinct values
Has ontological significance in Flex and can be used as input or output for a
transform
flex
newtype Altitude {value: int32;};
variant
Declare a a type that can be one of several "variants"
flex
variant Angle {DMS(int32, int32, int32);Radians(float64);};
enum
Declare an enumerated type consisting of constants of some declared type
If the type is integral this will be equivalent to enums in C++
flex
enum SimulationStatus int32 {Stopped = 0;Running = 1;Paused = 2;Reset = 3;};
function
Declare a function
flex
function square(x : float64) -> float64 {x * x;};
transform
Declare a transform function that can be used as a Transform in Tangram ProTM over terms which are ontologically significant.
flex
transform Celsius2Fahrenheit(c : Celsius) -> Fahrenheit =Fahrenheit {temp = c.temp * 9.0 / 5.0 + 32.0;};
let
let
Bind a value to an identifierValues bound like this are immutable Type is optional when it can be inferred can be inferred
flex
let x = 1.0 : float32;x + 1.0;let x : float32 = 1.0;x + 1.0;
match
- Pattern matching control flow
flex
match(optional_value) {some(value) => value;none => default;}
assert
- Refinement of data and specification of correctness properties
flex
message struct Position {x : float32;y : float32;assert x > 0.0 and x < 10.0;assert y > 0.0;};let x : int32 = {assert x > 0;x + 1;};
for
- Used in the list comprehension syntax
Used to construct a new list using each value of a pre-existing list.
flex
const xs: uint8[] = [2,4,6,20];const xsPlusOne: = [x + 1 for x in xs]// ~> [3, 5, 7, 21]: uint8[]
flex
const ys: uint8[] = [2,4,6,20,21];const evens = [y + 1 for x in ys if y % 2 == 0]// ~> [2,4,6,20]
scan
- Used to define operations over a collection
An accumulation value is defined to the right of the
scan
keyword and it's initial value is set Returns a list of intermediate and final values of the accumulator
flex
const sums: uint8[] = [cumulSum for x in [2,4,6,8] scan cumulSum: uint8 = 0 in cumulSum + x]// ~> [2, 6, 12, 20]: uint8[]
Operators
- Arithmetic:
+
,-
,/
,*
,%
Defined for numeric types - Comparison:
<
,>
,<=
,>=
Defined for numeric types - Equality:
==
,!=
Defined for primitive types that are not type constructors - Boolean:
&&
,||
,!
Defined forbit
- Bitwise:
&
,|
,~
,^
(Xor) Defined forbit
,intN
,uintN
Literals
- Bit literals are
true
andfalse
- String literals are delimited by double quotes.
"This is a string"
- Floating literals contain a decimal point.
1.618
and0.05
are valid, whereas1
,1.
and.05
are invalid - Type ascription is allowed for any flex expression.
x : T
for some typeT
and valuex
- Array literals are delimited with square brackets.
[1,2,3] : int32[]
- Tuple literals are delimited with parentheses.
(1,false,"foo") : (int32,bit,string)
- Hexadecimal literals begin with
0x
- Binary literals begin with
0b
- Integers can be represented as
0
inintN
anduintN
Types must be able to be inferred for integer literals or explicitly ascribed
- Underscores
_
can be used in numeric literals as digit separators.1_000_000
is equivalent to1000000