Calculemus


Fpf Module

Polymorphic Finite Partial functions via Patricia Trees.

Types

Type Description

func<'a, 'b>

Type of polymorphic finite partial functions represented as a patricia tree, 'a being the type of the arguments and 'b that of the values.

Functions and values

Function or value Description

(|->) argument value fpf

Full Usage: (|->) argument value fpf

Parameters:
    argument : 'a - The argument of the new mapping.
    value : 'b - The value of the new mapping.
    fpf : func<'a, 'b> - The fpf to update.

Returns: func<'a, 'b> The fpf with the new defined mapping.

Updates the fpf with a new mapping argument-value.

argument : 'a

The argument of the new mapping.

value : 'b

The value of the new mapping.

fpf : func<'a, 'b>

The fpf to update.

Returns: func<'a, 'b>

The fpf with the new defined mapping.

Example

 ("x" |-> 1)undefined |> graph
Evaluates to [("x", 1)].

a |=> b

Full Usage: a |=> b

Parameters:
    a : 'a - The argument of the only mapping.
    b : 'b - The value of the only mapping.

Returns: func<'a, 'b> The new point function with the defined mapping.

Creates a point function: a special case of fpf defined only for a single argument a mapped to a value b.

a : 'a

The argument of the only mapping.

b : 'b

The value of the only mapping.

Returns: func<'a, 'b>

The new point function with the defined mapping.

Example

 "x" |=> 1 |> graph
Evaluates to [("x", 1)].

apply fpf a

Full Usage: apply fpf a

Parameters:
    fpf : func<'a, 'b> - The fpf to be applied.
    a : 'a - The supposed fpf's argument.

Returns: 'b The fpf's value for a, if fpf is actually defined for it.

Applies fpf to a.

fpf : func<'a, 'b>

The fpf to be applied.

a : 'a

The supposed fpf's argument.

Returns: 'b

The fpf's value for a, if fpf is actually defined for it.

Exception Thrown with message apply, when fpf is not defined for a.
Example

 apply (("y" |-> 2)(("x" |-> 1)undefined)) "y"
Evaluates to 2.

Example

 apply (("y" |-> 2)(("x" |-> 1)undefined)) "z"
Throws System.Exception: apply.

applyd fpf d a

Full Usage: applyd fpf d a

Parameters:
    fpf : func<'a, 'b> - The fpf to be applied.
    d : 'a -> 'b - The function to apply to x, if it's not an argument of the fpf.
    a : 'a - The supposed fpf's argument.

Returns: 'b The fpf's value for a, if fpf is actually defined for it, otherwise applies d to x.

Applies fpf to a and returns the corresponding value, if fpf is actually defined for it, otherwise applies d to a.

fpf : func<'a, 'b>

The fpf to be applied.

d : 'a -> 'b

The function to apply to x, if it's not an argument of the fpf.

a : 'a

The supposed fpf's argument.

Returns: 'b

The fpf's value for a, if fpf is actually defined for it, otherwise applies d to x.

defined fpf a

Full Usage: defined fpf a

Parameters:
    fpf : func<'a, 'b> - The fpf to check.
    a : 'a - The argument to check.

Returns: bool true if fpf is defined for a.

Checks if fpf is defined for the argument a.

fpf : func<'a, 'b>

The fpf to check.

a : 'a

The argument to check.

Returns: bool

true if fpf is defined for a.

Example

 defined (("y" |-> 2)(("x" |-> 1)undefined)) "x"
Evaluates to true.

Example

 defined (("y" |-> 2)(("x" |-> 1)undefined)) "z"
Evaluates to false.

dom fpf

Full Usage: dom fpf

Parameters:
    fpf : func<'a, 'b> - The input fpf

Returns: 'a list The domain (the set of arguments) of the input fpf.

Returns the domain of the input fpf.

fpf : func<'a, 'b>

The input fpf

Returns: 'a list

The domain (the set of arguments) of the input fpf.

Example

 ("y" |-> 2)(("x" |-> 1)undefined)
 |> dom
Evaluates to ["x"; "y"].

foldl folder state fpf

Full Usage: foldl folder state fpf

Parameters:
    folder : 'State -> 'a -> 'b -> 'State - The normal F# function to update the state given the input fpf.
    state : 'State - The initial state.
    fpf : func<'a, 'b> - The input fpf

Returns: 'State The final state value.

Applies a function to the argument and value of an fpf, threading an accumulator argument through the computation. Take the second argument, and apply the function to it and the first argument and value of the fpf. Then feed this result into the function along with the second argument and value and so on. Return the final result. If the input function is f and the fpf's arguments and values are (i0,j0)...(iN,jN) then computes f (... (f s i0 j0) i1 j1 ...) iN jN.

It is, for finite partial functions, the same operation that ListModule.Fold is for list.

folder : 'State -> 'a -> 'b -> 'State

The normal F# function to update the state given the input fpf.

state : 'State

The initial state.

fpf : func<'a, 'b>

The input fpf

Returns: 'State

The final state value.

Example

 ("y" |-> 2)(("x" |-> 1)undefined)
 |> foldl (fun acc i j -> acc + j) 0
Evaluates to 3.

fpf xs ys

Full Usage: fpf xs ys

Parameters:
    xs : 'a list - The list of arguments of the new fpf.
    ys : 'b list - The list of values of the new fpf.

Returns: func<'a, 'b> The new fpf with the defined mappings.

Creates a new fpf from two lists xs and ys representing its domain and range. It associates argument to value based on the order of items in the two lists.

xs : 'a list

The list of arguments of the new fpf.

ys : 'b list

The list of values of the new fpf.

Returns: func<'a, 'b>

The new fpf with the defined mappings.

Example

 fpf [1;2;3] [1;4;9] |> graph
Evaluates to [(1, 1); (2, 4); (3, 9)].

graph fpf

Full Usage: graph fpf

Parameters:
    fpf : func<'a, 'b> - The input fpf

Returns: ('a * 'b) list The graph (the set of pairs argument-value) of the input fpf.

Returns the graph of the input fpf.

fpf : func<'a, 'b>

The input fpf

Returns: ('a * 'b) list

The graph (the set of pairs argument-value) of the input fpf.

Example

 ("y" |-> 2)(("x" |-> 1)undefined)
 |> graph
Evaluates to [("x", 1); ("y", 2)].

is_undefined _arg1

Full Usage: is_undefined _arg1

Parameters:
    _arg1 : func<'a, 'b> - The function to be checked.

Returns: bool True if the function is undefined.

Returns true if the function is completely undefined, false otherwise.

In case of equality comparison worries, better use this.

_arg1 : func<'a, 'b>

The function to be checked.

Returns: bool

True if the function is undefined.

Example

is_undefined undefined
Evaluates to true.

Example

is_undefined (("x" |-> 1)undefined)
Evaluates to false.

mapf mapping fpf

Full Usage: mapf mapping fpf

Parameters:
    mapping : 'a -> 'b - The function to transform values of the input fpf.
    fpf : func<'c, 'a> - The input fpf.

Returns: func<'c, 'b> The fpf with transformed values.

Builds a new fpf whose values are the results of applying the given function to the values of the input fpf.

It is, for finite partial functions, the same operation that ListModule.Map is for list.

mapping : 'a -> 'b

The function to transform values of the input fpf.

fpf : func<'c, 'a>

The input fpf.

Returns: func<'c, 'b>

The fpf with transformed values.

Example

 ("x" |-> 1)undefined
 |> mapf (fun x -> x * 10)
Evaluates to Leaf (..., [("x", 10)]).

ran fpf

Full Usage: ran fpf

Parameters:
    fpf : func<'a, 'b> - The input fpf

Returns: 'b list The range (the set of values) of the input fpf.

Returns the range of the input fpf.

fpf : func<'a, 'b>

The input fpf

Returns: 'b list

The range (the set of values) of the input fpf.

Example

 ("y" |-> 2)(("x" |-> 1)undefined)
 |> ran
Evaluates to [1; 2].

tryapplyd fpf a d

Full Usage: tryapplyd fpf a d

Parameters:
    fpf : func<'a, 'b> - The fpf to be applied.
    a : 'a - The supposed fpf's argument.
    d : 'b - The default value to return in case of failure.

Returns: 'b The fpf's value for a, if fpf is actually defined for it. Otherwise, the default value d.

Tries to apply fpf to an argument a, returning d as a default value if it fails.

fpf : func<'a, 'b>

The fpf to be applied.

a : 'a

The supposed fpf's argument.

d : 'b

The default value to return in case of failure.

Returns: 'b

The fpf's value for a, if fpf is actually defined for it. Otherwise, the default value d.

Example

 tryapplyd (("y" |-> 2)(("x" |-> 1)undefined)) "x" 9
Evaluates to 1.

Example

 tryapplyd (("y" |-> 2)(("x" |-> 1)undefined)) "x" 9
Evaluates to 9.

tryapplyl fpf a

Full Usage: tryapplyl fpf a

Parameters:
    fpf : func<'a, 'b list> - The input fpf to be applied.
    a : 'a - The supposed fpf's argument.

Returns: 'b list The fpf's value for a, if fpf is actually defined for it. Otherwise, the default value [].

Tries to apply an fpf whose values are lists to an argument a,returning [] as a default value if it fails.

fpf : func<'a, 'b list>

The input fpf to be applied.

a : 'a

The supposed fpf's argument.

Returns: 'b list

The fpf's value for a, if fpf is actually defined for it. Otherwise, the default value [].

Example

 tryapplyl (("y" |-> [4;5;6])(("x" |-> [1;2;3])undefined)) "x"
Evaluates to [1;2;3].

Example

 tryapplyl (("y" |-> [4;5;6])(("x" |-> [1;2;3])undefined)) "z"
Evaluates to [].

undef x

Full Usage: undef x

Parameters:
    x : 'a

Returns: 'b

A function undefined for any argument x and that always fails.

It is to be the same thing of what Fpf.undefined is in the context of the finite partial functions.

x : 'a
Returns: 'b
Exception Thrown with the message 'undefined function' when applied to any argument.
Note

In a non-functional world you can create a list of values and initialize the list signifying nothing: e.g. []. Then when you process the list it could return without exception or if you wanted the processing of the list to return with exception when there is nothing in the list, you would check the list for nothing and return an exception.

In a functional world you can create a list of functions and initialize the list with a function causing an exception given that the items is the list are evaluated as functions.

undef is that function which is used to initialize a list to cause an exception if the list is empty when evaluated.

Example

 ((undef 1):int)
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
Throws System.Exception: undefined function.

Example

 valmod 1 100 (undef) 1
Evaluates to 100.

undefine a fpf

Full Usage: undefine a fpf

Parameters:
    a : 'a - The argument to undefine.
    fpf : func<'a, 'b> - The fpf to undefine.

Returns: func<'a, 'b> The new fpf with the argument undefined, or the input fpf unchanged if it was already undefined for that argument.

Undefines the fpf for the given argument a.

a : 'a

The argument to undefine.

fpf : func<'a, 'b>

The fpf to undefine.

Returns: func<'a, 'b>

The new fpf with the argument undefined, or the input fpf unchanged if it was already undefined for that argument.

Example

 ("y" |-> 2)(("x" |-> 1)undefined)
 |> undefine "x"
Evaluates to Leaf (..., [("y", 2)]).

Example

 let input = ("y" |-> 2)(("x" |-> 1)undefined)

 input
 |> undefine "z"
 |> (=) input
val input: obj
Evaluates to true.

undefined

Full Usage: undefined

Returns: func<'a, 'b> Empty.

The empty, or everywhere undefined, fpf.

Returns: func<'a, 'b>

Empty.

valmod a b f x

Full Usage: valmod a b f x

Parameters:
    a : 'a - The argument to update.
    b : 'b - The value to assign to the argument.
    f : 'a -> 'b - The F# function to update.
    x : 'a - The argument to apply the modified function to.

Returns: 'b The new value, if applied to the updated argument. Otherwise, the value of the original function.

Updates to b the value of a normal F# function f for the argument a, creating, instead, a new mapping a-b if f it's not already defined for a. Then it applies this modified function to the argument x.

Corresponds to the mathematical notation \((a \mapsto b)f\) and it is the same thing of what (x |-> y) f is in the context of the finite partial functions.

a : 'a

The argument to update.

b : 'b

The value to assign to the argument.

f : 'a -> 'b

The F# function to update.

x : 'a

The argument to apply the modified function to.

Returns: 'b

The new value, if applied to the updated argument. Otherwise, the value of the original function.

Example

 valmod 1 100 id 1
val id: x: 'T -> 'T
Evaluates to 100.

Example

 valmod 1 100 id 2
val id: x: 'T -> 'T
Evaluates to 2.