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.
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).
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