tur/typeclass
built-in typeclasses: Eq, Ord, Show, Num.
Core typeclass definitions used across the standard library.
Since: Phase 15
Num
(defclass Num [a])
numeric types: the four arithmetic operations and negation.
| a | the numeric type |
(add [x : a y : a] : a)-
x+y (sub [x : a y : a] : a)-
x-y (mul [x : a y : a] : a)-
x*y (div [x : a y : a] : a)-
x/y, truncating for the integer instances (neg [x : a] : a)-
xnegated
(add 2 3) ; => 5 (Num [int])
(add 2.5 0.25) ; => 2.75 (Num [float])
Every method is closed over the instance type a: each takes and returns a, so a Num instance for a non-integer type (Rational, Complex, a user newtype) types correctly at call sites.
Complex, Rational, F64x2, F32x4, I64x2, I32x4, U16x8, U8x16, int, int8, int16, int32, uint8, uint16, uint32, uint64, float32, float
Since: Phase 15 (N0: de-:int-ified)
Num[int]
(definstance Num [int])
Num instance for int.
Num[int8]
(definstance Num [int8])
Num instance for int8.
Num[int16]
(definstance Num [int16])
Num instance for int16.
Num[int32]
(definstance Num [int32])
Num instance for int32.
Num[uint8]
(definstance Num [uint8])
Num instance for uint8.
Num[uint16]
(definstance Num [uint16])
Num instance for uint16.
Num[uint32]
(definstance Num [uint32])
Num instance for uint32.
Num[uint64]
(definstance Num [uint64])
Num instance for uint64.
Num[float32]
(definstance Num [float32])
Num instance for float32.
Num[float]
(definstance Num [float])
Num instance for float (double).
NumCast
(defclass NumCast [a])
numeric types that convert to and from int and float.
| a | the numeric type |
(to-float [x : a] : float)-
xas a float (double) (to-int [x : a] : int)-
xas an int, truncating a fraction toward zero (from-int [n : int] : a)-
the
anearest the intn (from-float [f : float] : a)-
the
anearest the floatf, truncating toward zero for the integer instances
(to-int (from-int n)) = n, for an n the type can hold
(to-float (from-float f)) = f, for the float instance
(to-float (:: 7 int32)) ; => 7
(to-int 7.75) ; => 7
(defn mean [A] [(Num A) (NumCast A)] [x : A y : A] : float
{(as float {x + y}) / 2.0})
(mean 3 4) ; => 3.5
What as and a literal cannot do in generic code. Inside [A] [(NumCast A)], (as float x) on an x : A reads as (to-float x) -- as itself is decided where the generic is written, where A has no numeric type -- and a constant of type A is (from-int 0) or (from-float 0.5), since a literal is not an A. Each method converts exactly as as does between the two concrete types.
Since: simd-and-vectorization-plan (generic numeric arrays)
NumCast[int]
(definstance NumCast [int])
NumCast instance for int.
NumCast[int8]
(definstance NumCast [int8])
NumCast instance for int8.
NumCast[int16]
(definstance NumCast [int16])
NumCast instance for int16.
NumCast[int32]
(definstance NumCast [int32])
NumCast instance for int32.
NumCast[uint8]
(definstance NumCast [uint8])
NumCast instance for uint8.
NumCast[uint16]
(definstance NumCast [uint16])
NumCast instance for uint16.
NumCast[uint32]
(definstance NumCast [uint32])
NumCast instance for uint32.
NumCast[uint64]
(definstance NumCast [uint64])
NumCast instance for uint64.
NumCast[float32]
(definstance NumCast [float32])
NumCast instance for float32.
NumCast[float]
(definstance NumCast [float])
NumCast instance for float.
Clone
(defclass Clone [a])
values that can be copied into an independently owned value.
| a | the type to clone |
(clone [x : a] : a)-
a copy of
xthe caller owns; the identity for a primitive.
(clone 42) ; => 42
The same class as the auto-loaded stdlib/typeclass-clone.tur declares; this module adds the primitive instances.
rc, Ref, String, int, ref, bool, cstr, int8, int16, int32, uint8, uint16, uint32, uint64, float32
Since: Phase B1
Clone[int]
(definstance Clone [int])
Clone instance for int (trivial copy).
Clone[bool]
(definstance Clone [bool])
Clone instance for bool (trivial copy).
Clone[cstr]
(definstance Clone [cstr])
Clone instance for cstr (trivial copy).
Clone[int8]
(definstance Clone [int8])
Clone instance for int8 (trivial copy).
Clone[int16]
(definstance Clone [int16])
Clone instance for int16 (trivial copy).
Clone[int32]
(definstance Clone [int32])
Clone instance for int32 (trivial copy).
Clone[uint8]
(definstance Clone [uint8])
Clone instance for uint8 (trivial copy).
Clone[uint16]
(definstance Clone [uint16])
Clone instance for uint16 (trivial copy).
Clone[uint32]
(definstance Clone [uint32])
Clone instance for uint32 (trivial copy).
Clone[uint64]
(definstance Clone [uint64])
Clone instance for uint64 (trivial copy).
Clone[float32]
(definstance Clone [float32])
Clone instance for float32 (trivial copy).
Functor
(defclass Functor [^f])
map a function over every element of a container, keeping its shape.
| ^f | the type constructor (kind * -> *) |
(fmap [container : (f a) g : (fn [a] b)] : (f b))-
containerwithgapplied to each element, same shape.
The same class the auto-loaded stdlib/typeclass-functor.tur declares (a redeclaration must match it); that page carries the laws and examples.
Backtrack, (Either E), Goal, Option, Parser, rc, (Result _ B), Schema
Since: Phase HKT H3
Applicative
(defclass Applicative [^f] [(Functor f)])
a Functor that can lift a plain value and apply a wrapped function.
| ^f | the type constructor (kind * -> *) |
(pure [x : a] : (f a))-
xin the minimal context (dispatches on the expected return type). (ap [ff : (f (fn [a] b)) fa : (f a)] : (f b))-
apply the function inside
ffto the value insidefa.
The same class the auto-loaded stdlib/typeclass-applicative.tur declares (a redeclaration must match it); that page carries the laws and examples.
Since: Phase HKT H3
Monad
(defclass Monad [^m] [(Applicative m)])
an Applicative whose next step can depend on the previous result.
| ^m | the type constructor (kind * -> *) |
(bind [ma : (m a) k : (fn [a] (m b))] : (m b))-
run
maand continue with the computationkbuilds from its result (flatMap).
The same class the auto-loaded stdlib/typeclass-monad.tur declares (a redeclaration must match it); that page carries the laws and examples.
Since: Phase HKT H3
Foldable
(defclass Foldable [^t])
containers that can be reduced to a single summary value.
| ^t | the container's type constructor (kind * -> *) |
(foldl [ta : (t a) init : b fn : (fn [b a] b)] : b)-
combine the elements of
tafirst to last: starting frominit, each step callsfnwith the accumulator and the next element. (foldr [ta : (t a) init : b fn : (fn [a b] b)] : b)-
combine the elements of
talast to first: starting frominit, each step callsfnwith the next element and the accumulator.
The one stdlib instance is rc's (stdlib/rc.tur, which declares the same class): an rc holds a single value x, so foldl is (fn init x) and foldr is (fn x init).
Since: Phase HKT H3
Traversable
(defclass Traversable [^t] [(Functor t) (Foldable t)])
containers you can map over with an effect, collecting the results.
| ^t | the container's type constructor (kind * -> *) |
(traverse [ta fn] : int)-
apply the effectful
fnto each element oftaand return the effect of the whole new container -- e.g. an Option of a list that is none if any element gave none.
The v1 signature is the simplified traverse [ta fn] : int (the Haskell one is Applicative f => (a -> f b) -> t a -> f (t b)), and the stdlib ships no instance yet.
Superclasses: Functor and Foldable, as in Haskell.
Since: Phase HKT H3
Alternative
(defclass Alternative [^f] [(Applicative f)])
an Applicative with a failure value and a first-success choice.
| ^f | the type constructor (kind * -> *) |
(empty [a] : (f a))-
the failing/empty value;
aonly picks the instance. (alt-or [x : (f a) y : (f a)] : (f a))-
xif it succeeded, elsey.
The same class the auto-loaded stdlib/typeclass-alternative.tur declares (a redeclaration must match it); that page carries the laws and examples.
Since: Phase HKT H7
Bifunctor
(defclass Bifunctor [^^f])
map over both type arguments of a two-parameter type at once.
| ^^f | the type constructor (kind * -> * -> *) |
(bimap [container : (f a b) fn-left : (fn [a] c) fn-right : (fn [b] d)] : (f c d))-
containerwithfn-leftapplied on one side andfn-righton the other.
The same class the auto-loaded stdlib/typeclass-bifunctor.tur declares (a redeclaration must match it); that page carries the laws and examples.
Since: Phase HKT H5
Display
(defclass Display [a])
values with a human-readable text form (Rust's Display).
| a | the type to display |
(display [x] : cstr)-
xas a :cstr for end-user output.
(display 42) ; => "42"
int, cstr, bool, int8, int16, int32, uint8, uint16, uint32, uint64, float, float32, ptr<void>
Since: Phase R0
Debug
(defclass Debug [a])
values with a diagnostic text form (Rust's Debug).
| a | the type to debug-print |
(debug [x] : cstr)-
xas a :cstr for diagnostic output; a string comes back quoted.
(debug "hi") ; => "\"hi\""
int, cstr, bool, int8, int16, int32, uint8, uint16, uint32, uint64, float, float32, ptr<void>
Since: Phase R0
Error
(defclass Error [a])
values that describe an error.
| a | the error type |
(error-message [x] : cstr)-
a :cstr description of the error
x. (error-cause [x] : ptr<void>)-
the underlying cause of
xas :ptr<void>, or NULL when there is none.
Since: Phase R0
Display[int]
(definstance Display [int])
Display instance for int.
Debug[int]
(definstance Debug [int])
Debug instance for int.
Display[cstr]
(definstance Display [cstr])
the string itself (delegates to Show [cstr]).
Debug[cstr]
(definstance Debug [cstr])
the string in double quotes.
Display[bool]
(definstance Display [bool])
"true" / "false" (delegates to Show [bool]).
Debug[bool]
(definstance Debug [bool])
"true" / "false".
Display[int8]
(definstance Display [int8])
decimal (delegates to Show [int8]).
Debug[int8]
(definstance Debug [int8])
"int8(v)".
Display[int16]
(definstance Display [int16])
decimal (delegates to Show [int16]).
Debug[int16]
(definstance Debug [int16])
"int16(v)".
Display[int32]
(definstance Display [int32])
decimal (delegates to Show [int32]).
Debug[int32]
(definstance Debug [int32])
"int32(v)".
Display[uint8]
(definstance Display [uint8])
decimal (delegates to Show [uint8]).
Debug[uint8]
(definstance Debug [uint8])
"uint8(v)".
Display[uint16]
(definstance Display [uint16])
decimal (delegates to Show [uint16]).
Debug[uint16]
(definstance Debug [uint16])
"uint16(v)".
Display[uint32]
(definstance Display [uint32])
decimal (delegates to Show [uint32]).
Debug[uint32]
(definstance Debug [uint32])
"uint32(v)".
Display[uint64]
(definstance Display [uint64])
decimal (delegates to Show [uint64]).
Debug[uint64]
(definstance Debug [uint64])
"uint64(v)".
Display[float]
(definstance Display [float])
"%g" (delegates to Show [float]).
Debug[float]
(definstance Debug [float])
"float(v)".
Display[float32]
(definstance Display [float32])
"%g" (delegates to Show [float32]).
Debug[float32]
(definstance Debug [float32])
"float32(v)".
Display[ptr<void>]
(definstance Display [ptr<void>])
Display instance for result<T,E> represented as ptr<void>.
Debug[ptr<void>]
(definstance Debug [ptr<void>])
Debug instance for result<T,E> represented as ptr<void>.
Error[ptr<void>]
(definstance Error [ptr<void>])
Error instance for result<T,E> represented as ptr<void>.
From
(defclass From [a b])
conversion of a value of type a into type b.
| a | the source type | |
| b | the target type |
(from [x] : int)-
xconverted to b, returned as the :int carrier (int64_t holds pointers in v1).
The stdlib declares no From or Into instance; both classes are for user conversions.
Since: Phase R0
Into
(defclass Into [a b])
conversion of a value of type a into type b; the counterpart of From.
| a | the source type | |
| b | the target type |
(into [x] : int)-
xconverted to b, returned as the :int carrier (int64_t holds pointers in v1).
Since: Phase R0