# Higher Order Core This crate contains core structs and traits for programming with higher order data structures. ### Introduction to higher order data structures A higher order data structure is a generalization of an ordinary data structure. In ordinary data structures, the default way of programming is: - Use data structures for data - Use methods/functions for operations on data In a higher order data structure, data and functions become the same thing. The central idea of a higher order data structure, is that properties can be functions of the same type. For example, a `Point` has an `x`, `y` and `z` property. In ordinary programming, `x`, `y` and `z` might have the type `f64`. If `x`, `y` and `z` are functions from `T -> f64`, then the point type is `Point`. A higher order `Point` can be called, just like a function. When called as a function, `Point` returns `Point`. However, unlike functions, you can still access properties of `Point`. You can also define methods and overload operators for `Point`. This means that in a higher order data structure, data and functions become the same thing. ### Motivation of programming with higher order data structures The major application of higher order data structures is geometry. A typical usage is e.g. to create procedurally generated content for games. Higher order data structures is about finding the right balance between hiding implementation details and exposing them for various generic algorithms. For example, a circle can be thought of as having the type `Point`. The argument can be an angle in radians, or a value in the unit interval `[0, 1]`. Another example, a line can be thought of as having the type `Point`. The argument is a value in the unit interval `[0, 1]`. When called with `0`, you get the start point of the line. When called with `1`, you get the end point of the line. Instead of declaring a `Circle` type, a `Line` type and so on, one can use `Point` to represent both of them. Higher order data structures makes easier to write generic algorithms for geometry. Although it seems abstract at first, it is also practically useful in unexpected cases. For example, an animated point can be thought of as having the type `Point<(&[Frame], f64)>`. The first argument contains the animation data and the second argument is time in seconds. Properties `x`, `y` and `z` of an animated point determines how the animated point is computed. The details of the implementation can be hidden from the algorithm that uses animated points. Sometimes you need to work with complex geometry. In these cases, it is easier to work with higher order data structures. For example, a planet might have a center, equator, poles, surface etc. A planet orbits around a star, which orbits around the center of a galaxy. This means that the properties of a planet, viewed from different reference frames, are functions of the arguments that determine the reference frame. You can create a "higher order planet" to reason about a planet's properties under various reference frames. ### Design Here is an example of how to declare a new higher order data structure: ```rust use higher_order_core::{Ho, Call, Arg, Fun, Func}; use std::sync::Arc; /// Higher order 3D point. #[derive(Clone)] pub struct Point where f64: Ho { /// Function for x-coordinates. pub x: Fun, /// Function for y-coordinates. pub y: Fun, /// Function for z-coordinates. pub z: Fun, } // It is common to declare a type alias for functions, e.g: pub type PointFunc = Point>; // Implement `Ho>` to allow higher order data structures // using properties `Fun` (`>::Fun`). impl Ho> for Point { type Fun = PointFunc; } // Implement `Call` to allow higher order calls. impl Call for Point where f64: Ho> + Call { fn call(f: &Self::Fun, val: T) -> Point { Point::<()> { x: >::call(&f.x, val), y: >::call(&f.y, val), z: >::call(&f.z, val), } } } impl PointFunc { /// Helper method for calling value. pub fn call(&self, val: T) -> Point where T: Copy { >::call(self, val) } } // Operations are usually defined as simple traits. // They look exactly the same as for normal generic programming. /// Dot operator. pub trait Dot { /// The output type. type Output; /// Returns the dot product. fn dot(self, other: Rhs) -> Self::Output; } // Implement operator once for the ordinary case. impl Dot for Point { type Output = f64; fn dot(self, other: Self) -> f64 { self.x * other.x + self.y * other.y + self.z * other.z } } // Implement operator once for the higher order case. impl Dot for PointFunc { type Output = Func; fn dot(self, other: Self) -> Func { let ax = self.x; let ay = self.y; let az = self.z; let bx = other.x; let by = other.y; let bz = other.z; Arc::new(move |a| ax(a) * bx(a) + ay(a) * by(a) + az(a) * bz(a)) } } ``` To disambiguate impls of e.g. `Point<()>` from `Point`, an argument type `Arg` is used for point functions: `Point>`. For every higher order type `U` and and argument type `T`, there is an associated function type `T -> U`. For primitive types, e.g. `f64`, the function type is `Func`. For higher order structs, e.g. `X<()>`, the function type is `X>`. The code for operators on higher order data structures must be written twice: - Once for the ordinary case `X<()>` - Once for the higher order case `X>` ### Higher Order Maps Sometimes it is useful to construct arbitrary data of the kind: - Vectors of primitives - Vectors of vectors, etc. For example, if a higher order point maps from angles to a circle, then complex geometry primitives might be defined onto the circle using angles: - Edge, e.g. `[a, b]` - Triangle, e.g. `[a, b, c]` - Square, e.g. `[[a, b], [c, d]]` The `HMap::hmap` method can be used to work with such structures. For example, if `p` is a higher order point of type `Point>`, then the following code maps two points at the same time: ```ignore let q: [Point; 2] = [0.0, 1.0].hmap(&p); ``` For binary higher order maps of type `f : (T, T) -> U`, the `HPair::hpair` method can be used before using `HMap::hmap`. For example: ```ignore let in_between: Func = Arc::new(move |(a, b)| { if b < a {b += 1.0}; (a + (b - a) * 0.5) % 1.0 }); // Pair up. let args: [(f64, f64); 2] = ([0.7, 0.9], [0.9, 0.1]).hpair(); // `[0.8, 0.0]` let q: [f64; 2] = args.hmap(&in_between); ```