UP | HOME

Candle

We are building the simplest possible mathematical model.

A vector from physics

Intuitions from physics:

Force

An abstract vector is defined as a direction and a magnitude.

In the simplest model there are just two directions - up and down.

Momentum

A momentum is defined as a difference between two adjacent vectors.

Derivative

In the simplest model the \(x\) -change is always \(1\) , so the derivative \(\frac{dy}{dx}\) is just the difference in the \(y\) coordinate.

A product-type

A Candle is by definition a record. Conceptually - a Set of labeled slots. Each particular value corresponds to a “point” or a “vector”.

Abstractly, any product type covers a whole vector-space (each value ranges over its type).

A record-type provides position-independent access, which means the structure is expandable without breaking the existing code - an incremental addition of properties (new slots) is possible (but NOT changing - sort of immutability of what has been already defined).

Of course, one could refactor quickly and safely because it is an ADT.

Parameterized by an interval

Ideally, a Candle has to be a dependent type (parameterized by its size - an interval between two timestamps). \[t1 - t2, \mid \forall t1, t2, t1 \leq t2\]

Deriving Eq, Ord

It will have implicit ordering by timestamps.

The first field is an opening timestamp so it will be used for comparing whole records.

This implies min and max can be used directly on the values of the Candle type.

A Semigroup

WIth respect to the merging operator \(\bigoplus\).

instance Semigroup Candle where
  (<>) = ...

An abstract Empty Candle

Non-existent, abstract identity element \(e\) for the merging operation. \[e\bigoplus x = x = x \bigoplus e\]

A Monoid

With the identity element \(e\) (an Empty Candle) we will define an instance of a Monoid.

instance Monoid Candle where
  mempty = ...
  mappend = ...

Not an Endofunctor

Any product type looks like an implicit, trivial Endofunctor, provided that a function being mapped over it “knows” how not to break data consistency.

It is almost as if

instance Functor Candle where
  fmap = ($)

but there is no way to guarantee the associativity of composition and the whole abstraction falls apart. It is NOT a Functor.

There is a hint: It is not a parameterized type - not an abstract container with a common structure. (it is a “very concrete record type - a unique set of named slots”).

Thus any record type is just a classic Abstract Data Type and also an Algebraic Data Type (a product), but not a Functor.

Author: <schiptsov@gmail.com>

Email: lngnmn2@yahoo.com

Created: 2023-08-08 Tue 18:37

Emacs 29.1.50 (Org mode 9.7-pre)