Skip to content

2015 005 Addition of Fn module

John Reppy edited this page Aug 16, 2015 · 9 revisions

Proposal 2015-005

Addition of Fn module

Author: Andreas Rossberg
Last revised: August 16, 2015
Status: proposed
Discussion: issue #6


Synopsis

signature FN =
  sig
    val id :      'a -> 'a
    val const :   'a -> 'b -> 'a
    val apply :   ('a -> 'b) * 'a -> 'b
    val o :       ('b -> 'c) * ('a -> 'b) -> ('a -> 'c)
    val curry :   ('a * 'b -> 'c) -> ('a -> 'b -> 'c)
    val uncurry : ('a -> 'b -> 'c) -> ('a * 'b -> 'c)
    val flip :    ('a * 'b -> 'c) -> ('b * 'a -> 'c)
    val repeat :  int -> ('a -> 'a) -> ('a -> 'a)
  end

  structure Fn : FN

Description

  • val id : 'a -> 'a
    is the identity function. Thus, id a is equivalent to a.

  • val const : 'a -> 'b -> 'a
    const a is a constant function that always returns a. Thus, const a b is equivalent to a (except for possible side-effects in b).

  • val apply : ('a -> 'b) * 'a -> 'b
    apply (f,a) applies f to a. Thus, it is equivalent to f a.

  • val o : ('b -> 'c) * ('a -> 'b) -> ('a -> 'c)
    f o g is the function composition of f and g. Thus, (f o g) a is equivalent to f (g a). This is the same as the global o operator.

  • val curry : ('a * 'b -> 'c) -> ('a -> 'b -> 'c)
    curry f transforms the binary function f into curried form. Thus, curry f a b is equivalent to f (a,b).

  • val uncurry : ('a -> 'b -> 'c) -> ('a * 'b -> 'c)
    uncurry f transforms a curried function f into a binary function. Thus, uncurry f (a,b) is equivalent to f a b. This function is the inverse of curry.

  • val flip : ('a * 'b -> 'c) -> ('b * 'a -> 'c)
    flip f switches the argument order of the binary function f. Thus, flip f (a,b) is equivalent to f (b,a).

  • val repeat : int -> ('a -> 'a) -> ('a -> 'a)
    repeat n f is the n-fold composition of f. Thus, repeat n f a is equivalent to f (... (f a)...), where f occurs n times.

Discussion

Other combinators could be added. These are the once that I have needed most in practice.

Impact

Adopting this proposal should not affect existing programs.

Rationale

The need for basic higher-order functions like id and const arises all the time in combination with other functionals. So far, the SML Basis has lacked any of these functions, although they are standard in the libraries of most other functional programming languages.

This module also provides a more natural home for the existing o operator.


History

  • [2015-08-16] Proposed

Clone this wiki locally