Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Tacit programming

From Wikipedia, the free encyclopedia
Programming paradigm

Tacit programming, also calledpoint-free style, is aprogramming paradigm in which function definitions do not identify thearguments (or "points") on which they operate. Instead the definitions merelycompose other functions, among which arecombinators that manipulate the arguments. Tacit programming is of theoretical interest, because the strict use of composition results in programs that are well adapted forequational reasoning.[1] It is also the natural style of someprogramming languages, includingAPL and its derivatives,[2] andconcatenative languages such asForth. The lack of argument naming gives point-free style a reputation of being needlessly obscure, hence theepithet "pointless style".[1]

Unixscripting uses the paradigm withpipes.

Examples

[edit]

Python

[edit]

Tacit programming can be illustrated with the followingPython code. A sequence of operations such as the following:

defexample(x):returnbaz(bar(foo(x)))

... can be written in point-free style as the composition of a sequence of functions, without parameters:[3]

fromfunctoolsimportpartial,reducedefcompose(*functions):returnpartial(reduce,lambdax,f:f(x),functions)example=compose(foo,bar,baz)

For a more complex example, theHaskell codep = ((.) f) . g can be translated as:

p = partial(compose, partial(compose, f), g)

Functional programming

[edit]

A simple example (inHaskell) is a program which computes the sum of a list of numbers. We can define the sum function recursively using apointed style (cf.value-level programming) as:

sum[]=0sum(x:xs)=x+sumxs

However, using afold, this can be replaced with:

sumxs=foldr(+)0xs

And then the argument is not needed, so this simplifies to

sum=foldr(+)0

which is point-free.

Another example usesfunction composition:

pxyz=f(gxy)z

The following Haskell-likepseudocode exposes how to reduce a function definition to its point-free equivalent:

p=\x->\y->\z->f(gxy)z=\x->\y->f(gxy)=\x->\y->(f.(gx))y=\x->f.(gx)(*Heretheinfixcomposeoperator"."isusedasacurriedfunction.*)=\x->((.)f)(gx)=\x->(((.)f).g)xp=((.)f).g

Finally, to see a complex example imagine a map filter program which takes a list, applies a function to it, and then filters the elements based on a criterion

mfcriteriaoperatorlist=filtercriteria(mapoperatorlist)

It can be expressed point-free[4] as

mf=(.map).(.).filter

As suggested already, the points inpoint-free refer to the arguments, not to the use of dots; a common misconception.[5]

A few programs have been written to automatically convert a Haskell expression to a point-free form.

APL family

[edit]

In the languageJ, the same sort of point-free code occurs in a function made to compute the average of a list (array) of numbers:

avg=:+/%#

+/ sums the items of the array by mapping (/) summation (+) to the array.% divides the sum by the number of elements (#) in the array.

Euler's formulaeix=cosx+isinx,{\displaystyle e^{ix}=\cos x+i\sin x,} expressed tacitly:

cos=:2o.]sin=:1o.]Euler=:^@j.=cosj.sin

(j. is a primitive function whose monadic definition is0j1 times x and whose dyadic definition isx+0j1×y.) The same tacit computations expressed inDyalog APL:

avg+÷cos2sin1EulerCalccos+0j1×sin⍝ 0j1 is what's usually written as iEulerDirect*0J1×⊢⍝ Same as ¯12○⊢⍝ Do the 2 methods produce the same result?EulerCheckEulerDirect=EulerCalcEulerCheck¯11231111⍝ Yes, so far so good!

Stack-based

[edit]

Instack-oriented programming languages (andconcatenative ones, most of which are stack based[citation needed]), point-free methods are commonly used. For example, a procedure to compute theFibonacci numbers might look like the following inPostScript:

/fib{dupdup1eqexch0eqornot{dup1subfibexch2subfibadd}if}def

Pipelines

[edit]

Unix pipeline

[edit]
Further information:Pipeline (Unix)

In Unix scripting the functions are computer programs which receive data fromstandard input and send the results tostandard output. For example,

sort | uniq -c | sort -rn

is a tacit or point-free composition which returns the counts of its arguments and the arguments, in the order of decreasing counts. Thesort anduniq are the functions, the-c and-rn control the functions, but the arguments are not mentioned. The pipe| is the composition operator.

Due to the way pipelines work, it is normally possible only to pass oneargument at a time in the form of a pair of standardinput/output stream. Although extrafile descriptors can be opened fromnamed pipes, this no longer constitutes a point-free style.

jq

[edit]

jq is aJSON-oriented programming language in which the| symbol is used to connect filters to form a pipeline in a familiar way. For example:

   [1,2] | add

evaluates to 3. (Yes, the JSON array is a jq filter that evaluates to an array.)

Although similar to Unix pipelines, jq pipelines allow the incoming data to be sent to more than one recipient on the RHS of the| as though in parallel. For example, the programadd/length will compute the average of the numbers in an array, so that:

   [1,2] | add/length

evaluates to 1.5

Similarly:

   [1,2] | [length, add, add/length]

evaluates to [2,3,1.5]

A dot (.) can be used to define an attachment point on the RHS, e.g.:

   1 | [., .]

evaluates to [1,1]

and similarly:

   2 | pow(.; .)

evaluates to 4 sincepow(x;y) is x to the power y.

Fibonacci sequence
[edit]

A tacit jq program for generating the Fibonacci sequence would be:

   [0,1] | recurse( [last, add] ) | first

Here,[0,1] is the initial pair to be taken as the first two itemsin the Fibonacci sequence. (The pair[1,1] could likewise be used forthe variant definition.)

The alphabetic tokens are built-in filters: `first` and `last`emit the first and last elements of their input arrays respectively;andrecurse(f) applies a filter, f, to its input recursively.

jq also allows new filters to be defined in a tacit style, e.g.:

   def fib: [0,1] | recurse( [last, add] ) | first;
Composition of unary functions
[edit]

In the section on Python in this article, the following Python definition is considered:

defexample(x):returnbaz(bar(foo(x)))

In point-free style, this can be written in Python as:

example = compose(foo, bar, baz)

In jq, the equivalent point-free definition would be:

   def example: foo | bar | baz;

See also

[edit]

References

[edit]
  1. ^abCunha, Manuel Alcino Pereira da (2005).Point-free Program Calculation (PhD thesis).University of Minho.
  2. ^Holmes, W. Neville, ed. (2006).Computers and People.
  3. ^"Name code not values". Concatenative.org. Retrieved13 September 2013.
  4. ^pipermail
  5. ^"Pointfree".Haskell.org Wiki. Retrieved2016-06-05.

External links

[edit]
Imperative
Structured
Object-oriented
Declarative
Functional
Dataflow
Logic
Domain-
specific
language

(DSL)
Concurrent,
parallel
Metaprogramming
Separation
of concerns
Comparisons/Lists
Retrieved from "https://en.wikipedia.org/w/index.php?title=Tacit_programming&oldid=1318832696"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp