-
Notifications
You must be signed in to change notification settings - Fork 77
Scilla Built Ins
Here we enumerate the built-in data types and their operations, as well as the standard library.
For built-in primitive data types and operations on them, check the following implementation.
-
eq s1 s1
: IsString s1
equal toString s2
. ReturnsBool
. -
concat s1 s2
: ConcatenateString s1
withString s2
. ReturnsString
. -
substr s1 i2 i2
: Extract sub-string ofString s2
starting from positionUint32 i1
with lengthUint32 i2
. ReturnsString
. -
to_string x1
: Convertx1
to a string literal. Valid types ofx1
areIntX
,UintX
,ByStrX
andByStr
.
Hash is represented by the data type ByStr32
. In the below description, Any
can be IntX
, UintX
, String
, ByStrX
or ByStr
-
eq h1 h2
: IsByStr32 h1
equal toByStr32 h2
. ReturnsBool
. -
to_uint256 b
: Convertb : ByStrX when X <= 32
intouint256
. -
sha256hash x
: The SHA256 hash of valueAny x
. ReturnsByStr32
. -
schnorr_gen_key_pair
: Generate a private key / public key pair. ReturnsPair{ByStr32 ByStr33}
. -
schnorr_sign privK pubK m
: Signs a messageByStr m
using the private and public keysByStr32 privK
andByStr33 pubK
. Returns theByStr64
signature. -
schnorr_verify pubK m sig
: Verifies that messageByStr m
was signed byByStr32 pubK
with signatureByStr64 sig
. ReturnsBool
. -
concat x1 x2
: Concatenatex1 : ByStrX1
andx2 : ByStrX2
to result in aByStr(X1+X2)
value.
Keys can have types IntX
, UintX
, String
, ByStr32
or ByStr20
. Values can be of any type.
-
put m k v
: Insert keyk
and valuev
intoMap m
. Returns a newMap
. -
get m k
: InMap m
, for keyk
, return the associated value asOption v
. The returned value isNone
ifk
is not in the mapm
. -
remove k
: Remove keyk
and it's associated valuev
from the map. Returns a new updatedMap
. -
contains k
: Is keyk
and it's associated valuev
present in the map? ReturnsBool
. -
to_list m
: ConvertMap m
into aList (Pair ('A) ('B))
where'A
and'B
are key and value types.
For most IntX
and UintX
operations, there are two integer inputs.
-
eq i1 i2
: Isi1
equal toi2
. ReturnsBool
. -
add i1 i2
: Add integer valuesi1
andi2
. Returns an integer. -
sub i1 i2
: Subtracti2
fromi1
. Returns an integer. -
mul i1 i2
: Integer product ofi1
andi2
. Returns an integer. -
div i1 i2
: Integer division ofi1
divi2
. Returns an integer. -
rem i1 i2
: Integer remainder wheni1
is divided byi2
. Returns an integer. -
lt i1 i2
: Isi1
lesser thani2
. ReturnsBool
. -
pow i1 i2
:i1
raised to the poweri2
. Returns an integer ofi1
's type.i2
isUint32
. -
to_nat i1
: ConvertUint32 i1
value toNat
. -
to_(u)int(32/64/128/256)
: Convert aUintX/IntX
orString
(that represents a number) value to anotherUintX/IntX
value. ReturnsSome IntX/UintX
if conversion succeeded,None
otherwise.
Addition, subtraction, multiplication, pow, division and reminder operations may raise integer overflow, underflow and division_by_zero errors.
Addresses are represented by the data type ByStr20
.
-
eq a1 a2
: IsByStr20 a1
equal toByStr20 a2
. ReturnsBool
.
-
eq b1 b2
: IsBNum b1
equal toBNum b2
. ReturnsBool
. -
blt b1 b2
: IsBNum b1
less thanBNum b2
. ReturnsBool
. -
badd b1 i1
: AddUintX i1
toBNum b1
. ReturnsBNum
. -
bsub b1 b2
: SubtractBNum b2
fromBNum b1
. ReturnsInt256
.
At the moment, Scilla features the following folding principles (structural recursion primitives) for Nat
s and List
s:
nat_fold : forall 'T, ('T -> Nat -> 'T) -> 'T -> Nat -> 'T
list_foldl: forall 'A . forall 'B . ('B -> 'A -> 'B) -> 'B -> (List 'A) -> 'B
list_foldr: forall 'A . forall 'B . ('A -> 'B -> 'B) -> 'B -> (List 'A) -> 'B
Check the Tutorial for more intuition and examples.
Here we enumerate a series of functions, which are implemented as a part of the standard library via the built-in functions and recursion primitives. Those are going to be verified and made available for any Scilla program, so the clients wouldn't have to reinvent a wheel every time.
-
list_map
:('A -> 'B) -> List 'A -> : List 'B
.Apply
f : 'A -> 'B
to every element ofl : List 'A
. Linear complexity. -
list_filter
:('A -> Bool) -> List 'A -> List 'A
.Preserving the order of elements in
l : List 'A
, return new list containing only those elements that satisfy the predicatef : 'A -> Bool
. Linear complexity. -
list_head
:(List 'A) -> (Option 'A)
.Return the head element of a list
l : List 'A
asSome 'A
,None
ifl
isNil
(the empty list). -
list_tail
:(List 'A) -> (Option List 'A)
.For input list
l : List 'A
, returnsSome l'
, wherel'
isl
except for it's head; returnsSome Nil
ifl
has only one element; returnsNone
ifl
is empty. -
list_append
:(List 'A -> List 'A -> List 'A)
.Append the second list to the first one and return a new List. Linear complexity (on first list).
-
list_reverse
:(List 'A -> List 'A)
.Return the reverse of the input list. Linear complexity.
-
list_flatten
:(List List 'A) -> List 'A
.Concatenate a list of lists. Each element (List 'A) of the input (List List 'A) are all concatenated together (in the same order) to give the result. linear complexity over the total number of elements in all of the lists.
-
list_length
:List 'A -> Int32
Number of elements in list. Linear complexity.
-
list_eq
:('A -> 'A -> Bool) -> List 'A -> List 'A -> Bool
.Takes a function
f : 'A -> 'A -> Bool
to compare elements of listsl1 : List 'A
andl2 : List 'A
and returns True iff all elements of the lists compare equal. Linear complexity. -
list_mem
:('A -> 'A -> Bool) -> 'A -> List 'A -> Bool
.Checks whether an element
a : 'A
is in the listl : List'A
.f : 'A -> 'A -> Bool
should be provided for equality comparison. Linear complexity. -
list_forall
:('A -> Bool) -> List 'A -> Bool
.Return True iff all elements of list
l : List 'A
satisfy predicatef : 'A -> Bool
. Linear complexity. -
list_exists
:('A -> Bool) -> List 'A -> Bool
.Return True if at least one element of list
l : List 'A
satisfies predicatef : 'A -> Bool
. Linear complexity. Linear complexity. -
list_sort
:('A -> 'A -> Bool) -> List 'A -> List 'A
.Stable sort the input list
l : List 'A
. Functionflt : 'A -> 'A -> Bool
provided must return True iff its first argument is lesser-than its second argument. Linear complexity. -
list_find
:('A -> Bool) -> 'A -> 'A
.Return
Some a
, wherea
is the first element ofl : List 'A
that satisfies the predicatef : 'A -> Bool
. ReturnsNone
iff none of the elements inl
satisfyf
. Linear complexity. -
list_zip
:List 'A -> List 'B -> List (Pair 'A 'B)
.Combine corresponding elements of
m1 : List 'A
andm2 : List 'B
into aPair
and return the resulting list. In case of different number of elements in the lists, the extra elements are ignored. -
list_zip_with
:('A -> 'B -> 'C) -> List 'A -> List 'B -> List 'C *)
. Linear complexity.Combine corresponding elements of
m1 : List 'A
andm2 : List 'B
usingf : 'A -> 'B -> 'C
and return the resulting list of'C
. In case of different number of elements in the lists, the extra elements are ignored. -
list_unzip
:List (Pair 'A 'B) -> Pair (List 'A) (List 'B)
.Convert a list
l : Pair 'A 'B
ofPair
s into aPair
of lists. Linear complexity. -
list_nth
:Int32 -> List 'A -> Option 'A
.Returns
Some 'A
if n'th element exists in list.None
otherwise. Linear complexity. -
nat_eq
:Nat -> Nat -> Bool
.Compare two natural numbers (
Nat
) for equality.
Conversions:
-
list_to_map
:List (Pair 'A 'B) -> Map 'A 'B
Convert a list (
l : Pair 'A 'B
) of key-val pairs into a map (Map 'A 'B
), overwriting duplicates. -
nat_to_int
:Nat -> Uint32
.Convert a natural number
n : Nat
into an integer. -
to_uintX
andto_intX
:IntX/UintX -> Option IntX/UintX
. Conversion between integer types. All these utilities returnSome IntX/UintX
on success,None
otherwise. A conversion is said to succeed if the source value can be represented in the destination type. -
intX_to_nat
anduintX_to_nat
:IntX/UintX -> Option Nat
Convert signed and unsigned integers to
Nat
. ReturnsSome Nat
on successful conversion,None
otherwise. The built-into_nat
supports only convertingUint32
values toNat
. Hence these wrappers succeed only if the input value can be represented inUint32
.