functor

Naive functor implementation using virtual packages

functor
monad
moon add CAIMEOX/functor@0.1.1
Download zip
Author
Version
0.1.1
License
Apache-2.0
Last updated
last year
Downloads
25
README

#Functor

A comprehensive example of implementing advanced functional programming abstractions in MoonBit using virtual packages. This library demonstrates how to define and implement Functors and Monads with multiple concrete instances.

#Overview

This library showcases MoonBit's virtual package system to implement module functor similar to OCaml. It provides:

  • Functor: A virtual package for types that can be mapped over
  • Monad: A virtual package for types that support sequential computation
  • Concrete Instances: Implementations for Array and List types

#Virtual Packages

Virtual packages in MoonBit allow you to define abstract interfaces that can be implemented by different concrete packages. This enables polymorphism and code reuse similar to module functor in other functional languages.

#Architecture Overview

┌─────────────────┐ ┌─────────────────┐ │ Virtual Package │ │ Virtual Package │ │ (Functor) │ │ (Monad) │ │ │ │ │ │ - pure() │ │ - pure() │ │ - map() │ │ - bind() │ │ - to_string() │ │ │ └─────────────────┘ └─────────────────┘ │ │ ▼ ▼ ┌─────────────────┐ ┌─────────────────┐ │ Implementation │ │ Implementation │ │ Packages │ │ Packages │ │ │ │ │ │ ┌─────────────┐ │ │ ┌─────────────┐ │ │ │Array Functor│ │ │ │ Array Monad │ │ │ └─────────────┘ │ │ └─────────────┘ │ │ ┌─────────────┐ │ │ │ │ │List Functor │ │ │ │ │ └─────────────┘ │ │ │ └─────────────────┘ └─────────────────┘ │ │ ▼ ▼ ┌─────────────────────────────────────────┐ │ Client Package │ │ │ │ Imports virtual packages with aliases │ │ Chooses concrete implementations │ │ Uses polymorphic code: @f.map(), etc. │ └─────────────────────────────────────────┘

#Library Structure

src/ ├── functor/ # Virtual package defining Functor interface ├── monad/ # Virtual package defining Monad interface ├── instances/ # Concrete implementations │ ├── array/ # Array Functor implementation │ ├── array_m/ # Array Monad implementation │ └── list/ # List Functor implementation └── lib/ # Usage examples and tests

#Functor

A Functor is a type that can be mapped over. It must provide:

  • pure: Wrap a value in the functor context
  • map: Apply a function to the wrapped value
  • to_string: Convert to string representation

#Functor Usage Example

// Create a functor value
let x : @f.F[Int] = @f.pure(1)

// Map a function over it
let y : @f.F[Int] = @f.map(x, x => x + 1)

// Convert to string
let result = @f.to_string(y) // "[2]" for Array implementation

#Monad

A Monad extends Functor with sequential computation capabilities:

  • pure: Wrap a value (same as Functor)
  • bind: Chain computations that may produce new monadic values

#Monad Usage Example

// Create a monadic value
let m : @m.M[Int] = @m.pure(1)

// Chain computations
let result = @m.bind(m, x => @m.pure(x + 1))

#Implementation Examples

#Array Functor

type F[A] Array[A]

pub fn[A] pure(value : A) -> F[A] {
[value] // Wrap in single-element array
}

pub fn[A, B] map(s : F[A], f : (A) -> B) -> F[B] {
s.inner().map(f) // Use built-in array map
}

#List Functor

type F[A] @immut/list.T[A]

pub fn[A] pure(value : A) -> F[A] {
@immut/list.Cons(value, Nil) // Single-element list
}

pub fn[A, B] map(s : F[A], f : (A) -> B) -> F[B] {
s.inner().map(f) // Use immutable list map
}

#Package Configuration

#Virtual Package Configuration

{ "virtual": { "has-default": false } }

#Implementation Package Configuration

{ "implement": "CAIMEOX/functor/functor" }

#Consumer Package Configuration

{ "import": [ { "alias": "f", "path": "CAIMEOX/functor/functor" } ], "overrides": [ "CAIMEOX/functor/instances/array" ] }

#Warning

The virtual package feature is highly experimental and may change in future versions of MoonBit (Language server support is very poor, and can even falsely report errors currently). Use with caution and be prepared for potential breaking changes.