Jump to content

Factor (programming language): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
LittleDan (talk | contribs)
deleting notability notice--factor is notable
LittleDan (talk | contribs)
Completely rewriting Factor article
Line 13: Line 13:
|website = [http://factorcode.org/ factorcode.org]
|website = [http://factorcode.org/ factorcode.org]
}}
}}
'''Factor''' is a [[high-level programming language]] created by [[Slava Pestov]]. Factor is dynamically typed and has automatic memory management, as well as powerful metaprogramming features. The language has a single implementation featuring a self-hosted optimizing compiler and an interactive development environment. The Factor distribution includes a large standard library.


==History==
'''Factor''' is a dynamically typed [[concatenative programming language|concatenative]] [[programming language]] whose design and implementation is led by [[Slava Pestov]]. Factor's main influences are [[Joy programming language|Joy]], [[Forth (programming language)|Forth]], [[Lisp programming language|Lisp]] and [[Self programming language|Self]]. Current versions of Factor exist as Continuous Builds for supported platforms while version 1.0 is under development<ref name="getting_factor"/>.
Slava Pestov created Factor in 2003 as a scripting language for a video game<ref>http://factorcode.org/slava/</ref>. The initial implementation, now referred to as JFactor, was implemented in [[Java (programming language)|Java]] and ran on the [[Java Virtual Machine]]. Though the early language resembled modern Factor superficially in terms of syntax, the modern language is very different in practical terms, and the current implementation is much faster.


The language has changed significantly over time. Originally, Factor programs centered around manipulating Java objects with the Java's reflection capabilities. From the beginning, the design philosophy has been to modify the language to suit programs written in it. As the Factor implementation and standard libraries grew more detailed, the need for certain language features became clear, and they were added. JFactor did not have an object system where you could define your own classes, and early versions of native Factor were the same; the language was similar to [[Scheme (programming language)|Scheme]] in this way. Today, the object system is a central part of Factor. Other important language features like tuple classes, combinator inlining, macros, user-defined parsing words and the modern vocabulary system were only added in a piecemeal fashion as their use became clear.
Like other [[Concatenative programming language|concatenative]] languages, Factor has a [[reverse Polish notation|postfix]] syntax, meaning that you write the arguments of a [[Subroutine|function]] before its name. As an example, [[Hello world]] in Factor is
"Hello world" print


The foreign function interface was present from very early versions to Factor, and an analogous system existed in JFactor. This was chosen over creating a plugin to the C part of the implementation for each external library that Factor should communicate with, and has the benefit of being more declarative, faster to compile and easier to write.
Factor is [[dynamic typing|dynamically typed]], and a unique [[Object-oriented programming|object]] system, inspired by [[CLOS]]<ref>[http://www.youtube.com/watch?v=f_0QlhYlS8g Factor: an extensible interactive language]</ref>, accompanies it. In Factor, there is a small group of [[primitive type|base types]], and users and the [[standard library]] can make their own classes using [[tuple]]s and other mechanisms. Factor supports [[Inheritance (computer science)|inheritance]]. Additionally, there are other ways to make [[Class (computer science)|class]]es other than types or tuples; Factor supports [[predicate class]]es and [[Union (computer science)|union classes]]. Factor's built-in compound datatypes include fixed and variable length [[Array data structure|vectors]] and [[hash table|hashtables]]. The language also supports [[floating point]], and [[Arbitrary-precision arithmetic|arbitrary precision integers]]. [[Linked list]]s, [[complex number]]s and [[fraction]]s are implemented in the standard library.


The Java implementation initially consisted of just an interpreter, but a compiler to Java bytecode was later added. This compiler only worked on certain procedures. The Java version of Factor was replaced by a version written in C and Factor. Initially, this consisted of just an interpreter, but the interpreter was replaced by two compilers, used in different situations. Over time, the Factor implementation has grown significantly faster <ref>http://concatenative.org/wiki/view/Factor/Implementation%20history</ref>.
Factor was originally only [[interpreting|interpreted]], but is now fully [[compiled]] (the non-optimizing compiler basically unrolls the interpreter loop<ref>[http://factor-language.blogspot.com/2007/09/two-tier-compilation-comes-to-factor.html Factor: a practical stack language: Two-tier compilation comes to Factor<!-- Bot generated title -->]</ref><ref>[http://factor-language.blogspot.com/2008/01/compiler-overhaul.html Factor: a practical stack language: Compiler overhaul<!-- Bot generated title -->]</ref>). The optimizing machine code compiler is written entirely in Factor. It does not output standalone executables, but rather generates [[machine code]] which is saved in the [[Smalltalk#Image-based_persistence|image]]. This image can then be deployed with the deploy-tool which produces a minimal [[Treeshaker|tree shaken]] image along with the VM.


==Programming paradigm==
When using the stack system does not suffice, [[lexical scope|lexical]] and [[dynamic scoping]] are supported alternatives. Factor has a growing library which supports [[Modularity (programming)|vocabularies]], [[continuation]]s, an [[Web server|HTTP server]] and accompanying [[web framework]], an [[OpenGL]] binding, a [[GUI]] library, an [[XML parser]], and several other utilities.
Factor is a dynamically typed, functional and object-oriented programming language. Code is structured around small procedures, called words. In typical code, these are 1-3 lines long, and a procedure more than 7 lines long is very rare. Something that would idiomatically be expressed with one procedure in another programming language would be written as several words in Factor.


Each word takes a fixed number of arguments and has a fixed number of return values. Arguments to words are passed on a data stack, using [[reverse Polish notation]]. The stack is used just to organize calls to words, and not as a datastructure. The stack in Factor is used in a similar way to the stack in [[Forth]]; for this, they are both considered [[stack-oriented programming language|stack languages]]. For example, below is a snippet of code that prints out "hello world" to the current output stream
One of Factor's main goals is to be useful for interactive and [[test-driven development]], which is why Factor is, at its core, a safe version of [[Forth (programming language)|Forth]]. Factor is dynamically typed, but the compiler assesses the stack depth of words (functions).
"hello world" print
<code>print</code> is a word in the <code>io</code> vocabulary that takes a string from the stack and returns nothing. It prints the string to the current output stream (by default, the terminal or the graphical listener).


Not all data has to be passed around only with the stack. [[Lexical scoping|Lexically scoped]] local variables let you store and access temporaries used within a procedure. [[Dynamic scoping|dynamically scoped]] variables are used to pass things between procedure calls without using the stack. For example, the current input and output streams are stored in dynamically scoped variables.
==Lisp-like features==
Factor shares many features with [[Lisp programming language|Lisp]], including a [[Homoiconicity|homoiconic]] syntax, [[garbage collection (computer science)|garbage collection]], [[higher-order function]]s, [[compile-time evaluation]], [[read-eval-print loop]], and [[Treeshaker (computer science)|treeshaker]].


Factor emphasizes flexibility and the ability to extend the language. There is a system for macros, as well as for arbitrary extension of Factor syntax. Factor's syntax is often extended to allow for new types of word definitions and new types of literals for data structures. It is also used in the XML library to provide literal syntax for generating XML. For example, the following word takes a string and produces an XML document object which is an HTML document emphasizing the string:
Unlike [[Lisp programming language|Lisp]], function composition proceeds left to right. In Lisp you might write<ref>[http://reddit.com/r/programming/info/657vx/comments/c02vk21 Slava Pestov, reddit comment]</ref>:
: make-html ( string -- xml )
(loop (print (eval (read))))<ref>[[Read-eval-print loop#Implementation | Read-eval-print loop, Implementation]]</ref>
dup
In Factor:
<XML
[ read eval print ] loop<ref>[http://www.reddit.com/info/6pq50/comments/c04jxfx pjdelport, reddit comment]</ref>
<html>
This is an inherent feature in [[Concatenative programming language|concatenative]] languages.
<head><title><-></title></head>
&lt;body>&lt;h1><->&lt;/h1>&lt;/body>
</html>
XML> ;
The word <code>dup</code> duplicates the top item on the stack. The <code>&lt;-></code> stands for filling in that part of the XML document with an item from the stack.


==Implementation and libraries==
==Concurrency support==
Factor includes a large standard library, written entirely in the language. These include
Factor's concurrency support was inspired by Erlang, Termite, Scheme48 and Java's java.util.concurrent library including basic [[Green threads|Lightweight co-operative threads]], to advanced libraries supporting [[Promise (programming)|Promises/Futures]], [[Mailbox (computing)|Mailboxes]] and [[Message passing|Message-passing concurrency.]]
* A cross-platform GUI toolkit, built on top of [[OpenGL]] and various windowing systems, used for the development environment
* Bindings to several database libraries, including [[PostgreSQL]] and [[SQLite]]
* An [[HTTP]] server and client, with the Furnace web framework
* Efficient homogeneous arrays of integers, floats or C structs
* A library implementing regular expressions, generating machine code to do the matching


There is a [[foreign function interface]] built in to Factor, allowing communication with [[C (programming language)|C]], [[Objective C]] and [[Fortran]] programs. There is also support for executing and communicating with shaders written in [[GLSL]].
There is no support for operating system [[Thread (computer science)|threads]] yet, but it's a planned feature.<ref>[http://www.mail-archive.com/factor-talk@lists.sourceforge.net/msg02540.html Support for Threads is planned]</ref>


Factor is implemented in Factor and [[C++]]. It was originally bootstrapped from an earlier Java implementation. Today, the parser and the optimizing compiler are written in the language. Certain basic parts of the language implemented are in C++, such as the garbage collector and certain primitives.
==Implementations==
So far, [[Java (programming language)|Java]], [[C (programming language)|C]], and [[C++ (programming language) | C++]] implementations have been constructed. The Java and C implementations are deprecated and no longer maintained; Factor's C version was ported to C++ for better maintainability.<ref>[http://factor-language.blogspot.com/2009/05/factor-vm-ported-to-c.html Factor VM Ported to C++]</ref> The native runtime of the C version has stopped shrinking (in favor of C++, which may shrink) while the [[self-hosting|self-hosted]] portion of Factor continues to grow.


Factor uses an image-based model, analogous to many [[Smalltalk]] implementations. Compiled code and data are stored in an image. To compile a program, the program is loaded into an image and the image is saved. A special tool assists in the process of creating a minimal image to run a particular program, packaging the result up into something that can be deployed as a standalone application.
There will likely always be a portion of Factor written in C++. Though Factor does not adhere to an external standard the way C++ does, the language is heavily documented.

==External links==
*[http://factorcode.org/ Factor website]
*[http://fun-factor.blogspot.com/2007/10/getting-started-with-factor-easy-ffi.html Getting started with Factor - Easy FFI]
*[http://tech.groups.yahoo.com/group/concatenative/message/1756 The birth of Factor]
*[https://lists.sourceforge.net/lists/listinfo/factor-talk Factor mailing list]
*[http://tunes.org/~nef/logs/concatenative Logs of #concatenative] on [[freenode]], an [[IRC]] channel which mainly discusses Factor
*[http://docs.factorcode.org/ Factor documentation]
*[http://planet.factorcode.org/ planet-factor] (a [[Aggregator|feed aggregator]])
*[http://gitweb.factorcode.org/gitweb.cgi?p=factor/.git;a=summary Git development repository]


==References==
==References==
{{reflist}}
{{reflist}}

==External links==
*[http://factorcode.org/ Factor website]


[[Category:Concatenative programming languages]]
[[Category:Concatenative programming languages]]

Revision as of 06:00, 6 October 2009

Factor
Paradigmmulti-paradigm: concatenative, stack-oriented
DeveloperSlava Pestov
First appeared2003
Stable release
Continuous Builds[1]
Typing disciplinestrong, dynamic
OSWindows, Mac OS, Linux, others
LicenseBSD license
Websitefactorcode.org
Influenced by
Joy, Forth, Lisp, Self
Influenced
Cat

Factor is a high-level programming language created by Slava Pestov. Factor is dynamically typed and has automatic memory management, as well as powerful metaprogramming features. The language has a single implementation featuring a self-hosted optimizing compiler and an interactive development environment. The Factor distribution includes a large standard library.

History

Slava Pestov created Factor in 2003 as a scripting language for a video game[2]. The initial implementation, now referred to as JFactor, was implemented in Java and ran on the Java Virtual Machine. Though the early language resembled modern Factor superficially in terms of syntax, the modern language is very different in practical terms, and the current implementation is much faster.

The language has changed significantly over time. Originally, Factor programs centered around manipulating Java objects with the Java's reflection capabilities. From the beginning, the design philosophy has been to modify the language to suit programs written in it. As the Factor implementation and standard libraries grew more detailed, the need for certain language features became clear, and they were added. JFactor did not have an object system where you could define your own classes, and early versions of native Factor were the same; the language was similar to Scheme in this way. Today, the object system is a central part of Factor. Other important language features like tuple classes, combinator inlining, macros, user-defined parsing words and the modern vocabulary system were only added in a piecemeal fashion as their use became clear.

The foreign function interface was present from very early versions to Factor, and an analogous system existed in JFactor. This was chosen over creating a plugin to the C part of the implementation for each external library that Factor should communicate with, and has the benefit of being more declarative, faster to compile and easier to write.

The Java implementation initially consisted of just an interpreter, but a compiler to Java bytecode was later added. This compiler only worked on certain procedures. The Java version of Factor was replaced by a version written in C and Factor. Initially, this consisted of just an interpreter, but the interpreter was replaced by two compilers, used in different situations. Over time, the Factor implementation has grown significantly faster [3].

Programming paradigm

Factor is a dynamically typed, functional and object-oriented programming language. Code is structured around small procedures, called words. In typical code, these are 1-3 lines long, and a procedure more than 7 lines long is very rare. Something that would idiomatically be expressed with one procedure in another programming language would be written as several words in Factor.

Each word takes a fixed number of arguments and has a fixed number of return values. Arguments to words are passed on a data stack, using reverse Polish notation. The stack is used just to organize calls to words, and not as a datastructure. The stack in Factor is used in a similar way to the stack in Forth; for this, they are both considered stack languages. For example, below is a snippet of code that prints out "hello world" to the current output stream

"hello world" print

print is a word in the io vocabulary that takes a string from the stack and returns nothing. It prints the string to the current output stream (by default, the terminal or the graphical listener).

Not all data has to be passed around only with the stack. Lexically scoped local variables let you store and access temporaries used within a procedure. dynamically scoped variables are used to pass things between procedure calls without using the stack. For example, the current input and output streams are stored in dynamically scoped variables.

Factor emphasizes flexibility and the ability to extend the language. There is a system for macros, as well as for arbitrary extension of Factor syntax. Factor's syntax is often extended to allow for new types of word definitions and new types of literals for data structures. It is also used in the XML library to provide literal syntax for generating XML. For example, the following word takes a string and produces an XML document object which is an HTML document emphasizing the string:

: make-html ( string -- xml )
   dup
   <XML
       <html>
           <head><title><-></title></head>
           <body><h1><-></h1></body>
       </html>
   XML> ;

The word dup duplicates the top item on the stack. The <-> stands for filling in that part of the XML document with an item from the stack.

Implementation and libraries

Factor includes a large standard library, written entirely in the language. These include

  • A cross-platform GUI toolkit, built on top of OpenGL and various windowing systems, used for the development environment
  • Bindings to several database libraries, including PostgreSQL and SQLite
  • An HTTP server and client, with the Furnace web framework
  • Efficient homogeneous arrays of integers, floats or C structs
  • A library implementing regular expressions, generating machine code to do the matching

There is a foreign function interface built in to Factor, allowing communication with C, Objective C and Fortran programs. There is also support for executing and communicating with shaders written in GLSL.

Factor is implemented in Factor and C++. It was originally bootstrapped from an earlier Java implementation. Today, the parser and the optimizing compiler are written in the language. Certain basic parts of the language implemented are in C++, such as the garbage collector and certain primitives.

Factor uses an image-based model, analogous to many Smalltalk implementations. Compiled code and data are stored in an image. To compile a program, the program is loaded into an image and the image is saved. A special tool assists in the process of creating a minimal image to run a particular program, packaging the result up into something that can be deployed as a standalone application.

References