No matching definitions.

tur/typeclass

stdlib/typeclass.tur

built-in typeclasses: Eq, Ord, Show, Num.

Core typeclass definitions used across the standard library.

Since: Phase 15

defclass

Num

(defclass Num [a])

numeric types: the four arithmetic operations and negation.

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

x negated

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

definstance

Num[int]

(definstance Num [int])

Num instance for int.

definstance

Num[int8]

(definstance Num [int8])

Num instance for int8.

definstance

Num[int16]

(definstance Num [int16])

Num instance for int16.

definstance

Num[int32]

(definstance Num [int32])

Num instance for int32.

definstance

Num[uint8]

(definstance Num [uint8])

Num instance for uint8.

definstance

Num[uint16]

(definstance Num [uint16])

Num instance for uint16.

definstance

Num[uint32]

(definstance Num [uint32])

Num instance for uint32.

definstance

Num[uint64]

(definstance Num [uint64])

Num instance for uint64.

definstance

Num[float32]

(definstance Num [float32])

Num instance for float32.

definstance

Num[float]

(definstance Num [float])

Num instance for float (double).

defclass

Clone

(defclass Clone [a])

values that can be copied into an independently owned value.

athe type to clone
(clone [x :a] :a)

a copy of x the 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.

Since: Phase B1

definstance

Clone[int]

(definstance Clone [int])

Clone instance for int (trivial copy).

definstance

Clone[bool]

(definstance Clone [bool])

Clone instance for bool (trivial copy).

definstance

Clone[cstr]

(definstance Clone [cstr])

Clone instance for cstr (trivial copy).

definstance

Clone[int8]

(definstance Clone [int8])

Clone instance for int8 (trivial copy).

definstance

Clone[int16]

(definstance Clone [int16])

Clone instance for int16 (trivial copy).

definstance

Clone[int32]

(definstance Clone [int32])

Clone instance for int32 (trivial copy).

definstance

Clone[uint8]

(definstance Clone [uint8])

Clone instance for uint8 (trivial copy).

definstance

Clone[uint16]

(definstance Clone [uint16])

Clone instance for uint16 (trivial copy).

definstance

Clone[uint32]

(definstance Clone [uint32])

Clone instance for uint32 (trivial copy).

definstance

Clone[uint64]

(definstance Clone [uint64])

Clone instance for uint64 (trivial copy).

definstance

Clone[float32]

(definstance Clone [float32])

Clone instance for float32 (trivial copy).

defclass

Functor

(defclass Functor [^f])

map a function over every element of a container, keeping its shape.

^fthe type constructor (kind * -> *)
(fmap [container :(f a) g :(fn [a] b)] :(f b))

container with g applied 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.

Since: Phase HKT H3

defclass

Applicative

(defclass Applicative [^f] [(Functor f)])

a Functor that can lift a plain value and apply a wrapped function.

^fthe type constructor (kind * -> *)
(pure [x :a] :(f a))

x in the minimal context (dispatches on the expected return type).

(ap [ff :(f (fn [a] b)) fa :(f a)] :(f b))

apply the function inside ff to the value inside fa.

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

defclass

Monad

(defclass Monad [^m] [(Applicative m)])

an Applicative whose next step can depend on the previous result.

^mthe type constructor (kind * -> *)
(bind [ma :(m a) k :(fn [a] (m b))] :(m b))

run ma and continue with the computation k builds 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

defclass

Foldable

(defclass Foldable [^t])

containers that can be reduced to a single summary value.

^tthe container's type constructor (kind * -> *)
(foldl [ta :(t a) init :b fn :(fn [b a] b)] :b)

combine the elements of ta first to last: starting from init, each step calls fn with the accumulator and the next element.

(foldr [ta :(t a) init :b fn :(fn [a b] b)] :b)

combine the elements of ta last to first: starting from init, each step calls fn with 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).

rc

Since: Phase HKT H3

defclass

Traversable

(defclass Traversable [^t] [(Functor t) (Foldable t)])

containers you can map over with an effect, collecting the results.

^tthe container's type constructor (kind * -> *)
(traverse [ta fn] :int)

apply the effectful fn to each element of ta and 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

defclass

Alternative

(defclass Alternative [^f] [(Applicative f)])

an Applicative with a failure value and a first-success choice.

^fthe type constructor (kind * -> *)
(empty [a] :(f a))

the failing/empty value; a only picks the instance.

(alt-or [x :(f a) y :(f a)] :(f a))

x if it succeeded, else y.

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

defclass

Bifunctor

(defclass Bifunctor [^^f])

map over both type arguments of a two-parameter type at once.

^^fthe type constructor (kind * -> * -> *)
(bimap [container :(f a b) fn-left :(fn [a] c) fn-right :(fn [b] d)] :(f c d))

container with fn-left applied on one side and fn-right on 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.

Result

Since: Phase HKT H5

defclass

Display

(defclass Display [a])

values with a human-readable text form (Rust's Display).

athe type to display
(display [x] :cstr)

x as a :cstr for end-user output.

(display 42)   ; => "42"

Since: Phase R0

defclass

Debug

(defclass Debug [a])

values with a diagnostic text form (Rust's Debug).

athe type to debug-print
(debug [x] :cstr)

x as a :cstr for diagnostic output; a string comes back quoted.

(debug "hi")   ; => "\"hi\""

Since: Phase R0

defclass

Error

(defclass Error [a])

values that describe an error.

athe error type
(error-message [x] :cstr)

a :cstr description of the error x.

(error-cause [x] :ptr<void>)

the underlying cause of x as :ptr<void>, or NULL when there is none.

ptr<void>

Since: Phase R0

definstance

Display[int]

(definstance Display [int])

Display instance for int.

definstance

Debug[int]

(definstance Debug [int])

Debug instance for int.

definstance

Display[cstr]

(definstance Display [cstr])

the string itself (delegates to Show [cstr]).

definstance

Debug[cstr]

(definstance Debug [cstr])

the string in double quotes.

definstance

Display[bool]

(definstance Display [bool])

"true" / "false" (delegates to Show [bool]).

definstance

Debug[bool]

(definstance Debug [bool])

"true" / "false".

definstance

Display[int8]

(definstance Display [int8])

decimal (delegates to Show [int8]).

definstance

Debug[int8]

(definstance Debug [int8])

"int8(v)".

definstance

Display[int16]

(definstance Display [int16])

decimal (delegates to Show [int16]).

definstance

Debug[int16]

(definstance Debug [int16])

"int16(v)".

definstance

Display[int32]

(definstance Display [int32])

decimal (delegates to Show [int32]).

definstance

Debug[int32]

(definstance Debug [int32])

"int32(v)".

definstance

Display[uint8]

(definstance Display [uint8])

decimal (delegates to Show [uint8]).

definstance

Debug[uint8]

(definstance Debug [uint8])

"uint8(v)".

definstance

Display[uint16]

(definstance Display [uint16])

decimal (delegates to Show [uint16]).

definstance

Debug[uint16]

(definstance Debug [uint16])

"uint16(v)".

definstance

Display[uint32]

(definstance Display [uint32])

decimal (delegates to Show [uint32]).

definstance

Debug[uint32]

(definstance Debug [uint32])

"uint32(v)".

definstance

Display[uint64]

(definstance Display [uint64])

decimal (delegates to Show [uint64]).

definstance

Debug[uint64]

(definstance Debug [uint64])

"uint64(v)".

definstance

Display[float]

(definstance Display [float])

"%g" (delegates to Show [float]).

definstance

Debug[float]

(definstance Debug [float])

"float(v)".

definstance

Display[float32]

(definstance Display [float32])

"%g" (delegates to Show [float32]).

definstance

Debug[float32]

(definstance Debug [float32])

"float32(v)".

definstance

Display[ptr<void>]

(definstance Display [ptr<void>])

Display instance for result<T,E> represented as ptr<void>.

definstance

Debug[ptr<void>]

(definstance Debug [ptr<void>])

Debug instance for result<T,E> represented as ptr<void>.

definstance

Error[ptr<void>]

(definstance Error [ptr<void>])

Error instance for result<T,E> represented as ptr<void>.

defclass

From

(defclass From [a b])

conversion of a value of type a into type b.

athe source type
bthe target type
(from [x] :int)

x converted 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

defclass

Into

(defclass Into [a b])

conversion of a value of type a into type b; the counterpart of From.

athe source type
bthe target type
(into [x] :int)

x converted to b, returned as the :int carrier (int64_t holds pointers in v1).

Since: Phase R0