Functional programming: Algebraic data types

Based on this Wikipedia article.

Composite data types

Composite (or compound) data types consist of a few primitive data types or other composite types. Examples of composite data types are C/C++ structure and union.

Product data type

Let’s look at the C/C++ structure example:

struct account {
   int account_number;
   char *first_name;
   char *last_name;
   float balance;
};

This type contains account_number AND first_name AND last_name AND balance fields – the AND makes it a product type, where product is boolean AND.

Sum data type

Let’s now look at the following example of C/C++ union:

union number {
  int8_t i8;
  int16_t i16;
  int32_t i32;
  int64_t i64;
  float f32;
  double f64;
};

This type can accommodate an int8_t OR int16_t OR int32_t OR int64_t OR float OR double number – the OR makes the type a kind of sum type (not quite yet), where sum is boolean OR.

This is not the whole of the sum data type yet. The actual type of the data currently stored in a C/C++ union is only known at run time, and the program that uses the union should be able to handle that at run time.

The union can be extended to tagged union, which may look like this:

struct number {
  uint8_t tag;
  union {
    int8_t i8;
    int16_t i16;
    int32_t i32;
    int64_t i64;
    float f32;
    double f64;
  } value;
}

The tag specifies the type of the value at run time.

Such tagged union type is called sum data type.

Hybrid data types

Product and sum data types are the base algebraic data types. The data types composed of product and sum data types are called hybrid data types.

It probably makes not that much sense to use the struct number type for the balance field in the struct account, but here it is – a hybrid data type example:

struct account {
int account_number;
char *first_name;
char *last_name;
struct number balance;
};

Algebraic data types

The product, sum and hybrid data types are what is called algebraic data types.

See also

Functional Programming: Terminology

Raspberry Pi: C++ cross-compiling in Eclipse

Based on this GuruCoding page.

Environment
  • Ubuntu 16.04 LTS
  • Eclipse Neon.3 Release 4.6.3
  • arm-linux-gnueabihf (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
  • Raspberry Pi 3 Model B
ARM toolchain

See Raspberry Pi: cross-compiling blog.

Upgrade libstdc++ to 6.0.21 on Raspberry Pi

See Raspberry Pi: how to solve the “version `GLIBCXX_6.0.21′ not found” problem.

Install Eclipse IDE for C/C++ developers

Download, unpack from here.

Eclipse plugins

Eclipse:

  • Help > Install new software > Work with: –All Available Sites–
  • Check “Mobile and Device Development”
  • Next > Accept the terms of the license agreement” > Finish
  • Restart Eclipse
Create project

In Eclipse:

  • File > New > C++ Project
  • C++ Project
    • Project name: helloworld
    • Project type: Hello World C++ Project
    • Toolchains: Cross GCC
    • Next
  • Basic Settings
    • Hello world greeting: Hello world!
    • Next
  • Select Configurations
    • Next
  • Cross GCC Command
    • Cross compiler prefix: arm-linux-gnueabihf-
    • Cross compiler path: /usr/bin
    • Finish
  • Project > Properties > C/C++ Build > Settings
    • Tool Settings > Cross Settings
      • Prefix: arm-linux-gnueabihf-
      • Path: /usr/bin
Enable C++ 11
  • Project > Properties > C/C++ Build > Settings
    • Configuration: Debug/Release
    • Tool Settings > Cross G++ Compiler > Dialect: ISO C+11 (-std=c++0x)
Build
  • Project > Build All

Raspberry Pi: C++ cross-compiling

Environment
  • Ubuntu 16.04 LTS
  • arm-linux-gnueabihf (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
  • Raspberry Pi 3 Model B
Install build tools

Check/install build-essential:

# Check if build-essential is installed
$ dpkg -s build-essntial
# Install build-essential if needed
$ sudo apt-get install build-essential
Install ARM toolchain
# ARM build tools
$ sudo apt-get install g++-arm-linux-gnueabihf
# Check installation
$ arm-linux-gnueabihf-g++ -v
Upgrade libstdc++ to 6.0.21 on Raspberry Pi

See Raspberry Pi: how to solve the “version `GLIBCXX_6.0.21′ not found” problem.

Create helloworld.cpp
#include <iostream>

int main() {
    std::cout << "Hello World!" << std::endl;
    return 0;
}
Compile
$ arm-linux-gnueabihf-g++ -O3 -g3 -Wall -fPIC -c -o helloworld.o helloworld.cpp
$ arm-linux-gnueabihf-g++ -o helloworld helloworld.o
Copy helloworld to Raspberry Pi
$ scp ./helloworld pi@192.168.0.xxx:/home/pi/path
Login into Raspberry Pi and run helloworld
$ ssh pi@192.168.0.xxx
# On Raspberry Pi
$ cd path
$ ./helloworld
Hello World!
g++-arm-linux-gnueabihf pakage detais
$ dpkg -s g++-arm-linux-gnueabihf
Package: g++-arm-linux-gnueabihf
Status: install ok installed
Priority: optional
Section: devel
Installed-Size: 11
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Source: gcc-defaults (1.150ubuntu1)
Version: 4:5.3.1-1ubuntu1
Depends: cpp-arm-linux-gnueabihf (>= 4:5.3.1-1ubuntu1), gcc-arm-linux-gnueabihf (>= 4:5.3.1-1ubuntu1), g++-5-arm-linux-gnueabihf (>= 5.3.1-3~)
Description: GNU C++ compiler for the armhf architecture
 This is the GNU C++ compiler, a fairly portable optimizing compiler for C++.
 .
 This is a dependency package providing the default GNU C++ cross-compiler
 for the armhf architecture.
Original-Maintainer: Debian GCC Maintainers <debian-gcc@lists.debian.org>