fbpx
Wikipedia

Refal

Refal ("Recursive functions algorithmic language"; Russian: РЕФАЛ) "is a functional programming language oriented toward symbolic computations", including "string processing, language translation, [and] artificial intelligence".[1] It is one of the oldest members of this family, first conceived of in 1966 as a theoretical tool, with the first implementation appearing in 1968. Refal was intended to combine mathematical simplicity with practicality for writing large and sophisticated programs.

Refal
ParadigmPattern-matching and term-rewriting
Designed byValentin Turchin
DeveloperValentin Turchin, S. Florentsev, V. Olyunin, et al.
First appeared1968 (1968)
Typing disciplinestrong, dynamic
Websitehttp://www.refal.net
Major implementations
Refal-2, Refal-5, Refal-6, Refal+

One of the first functional programming languages to do so, and unlike Lisp of its time, Refal is based on pattern matching. Its pattern matching works in conjunction with term rewriting.

The basic data structure of Lisp and Prolog is a linear list built by cons operation in a sequential manner, thus with O(n) access to list's nth element. Refal's lists are built and scanned from both ends, with pattern matching working for nested lists as well as the top-level one. In effect, the basic data structure of Refal is a tree rather than a list. This gives freedom and convenience in creating data structures while using only mathematically simple control mechanisms of pattern matching and substitution.

Refal also includes a feature called the freezer to support efficient partial evaluation.

Refal can be applied to the processing and transformation of tree structures, similarly to XSLT.[2]

Basics edit

A Refal Hello World example is shown below.

$ENTRY Go { = <Hello>;} Hello { = <Prout 'Hello world'>; } 

The program above includes two functions named Go and Hello. A function is written as the name of the function followed by the function body in curly braces. The Go function is marked as the entry point of the program using the $ENTRY directive.

One could think of expressions in the function bodies as function "calls" in Lisp-like syntax. For example, the Hello function appears to call the built-in Prout function with the string 'Hello world' as the argument. The meaning and the mechanism of the call, however, is quite different. To illustrate the difference, consider the following function that determines whether a string is a palindrome.

 Pal { = True; s.1 = True; s.1 e.2 s.1 = <Pal e.2>; e.1 = False; } 

This example shows a function with a more complex body, consisting of four sentences (clauses). A sentence begins with a pattern followed by an equal sign followed by a general expression on the right hand side. A sentence is terminated with a semicolon. For example, the pattern of the second sentence of the function is "s.1" and the expression is "True".

As the example shows, patterns include pattern variables that have the form of a character identifying the type of the variable (what the variable matches) followed by the variable identifier. The variables that begin with an "s" match a single symbol, those that begin with an "e" match an arbitrary expression. The variable identifier can be an arbitrary alphanumeric sequence optionally separated from the type identifier by a dot.

A function executes by comparing its argument with the patterns of its sentences in the order they appear in the definition, until the first pattern that matches. The function then replaces the argument with the expression on the right hand side of the matched sentence.

If the result of a function application includes a subexpression in angle brackets (as it will after the third sentence of our example is applied), the result is further processed by Refal by invoking the function identified by the first symbol in the brackets. Execution stops when the result has no more angle brackets to expand in this way.

The function Pal can thus be read informally as: "If the expression is empty, replace it with True. Otherwise if the expression is a single symbol, replace it with True. Otherwise if the expression is a symbol followed by an arbitrary expression e.2 followed by the same symbol, replace it with the expression <Pal e.2>. (In other words, throw away the two identical symbols at the beginning and the end and recurse). Otherwise replace the expression with False. (The pattern e.1 always matches)."

The following are three step-by-step execution traces annotated with the sentence numbers applied at each step to produce the next

 <Pal 'noon'> (#3) <Pal 'oo'> (#3) <Pal > (#1) True 
 <Pal 'wow'> (#3) <Pal 'o'> (#2) True 
 <Pal 'revolver'> (#3) <Pal 'evolve'> (#3) <Pal 'volv'> (#3) <Pal 'ol'> (#4) False 

We can now see that the Hello World example in fact executes as the sequence of the following expression transformations:

 Seed the machine with the initial expression marked by $ENTRY: <Go >  (apply the sentence in Go) <Hello >  (apply the sentence in Hello) <Prout 'Hello world'> (Prout is a built-in that prints and expands to nothing)  (nothing to apply; stop) 

Other examples edit

Factorial edit

 Fact { 0 = 1; s.N = <* s.N <Fact <- s.N 1>>>; } 

Here 0 matches 0 the number and produces 1. On any other symbol which is a number, multiply it with the result of (Fact (- s.N 1)) Note the prefix style of operators.

Factorial with loops edit

 Fact { s.n = <Loop s.n 1>; }; Loop { 0 s.f = s.f; s.n s.f = <Loop <- s.n 1> <* s.n s.f>>; } 

As can be seen s.n acts as the loop counter.

Equality edit

 Equal { (e.1)(e.1) = T; (e.1)(e.2) = F; } 

Here the function is defined as, if given two terms, and the terms are same then the first clause matches and produces True. else the second clause matches and produces False.

An important property of Refal is that all functions in refal are single argument. (But may be decomposed into terms in an expression as above.)

If edit

Defining control structures is easy

 If { T Then (e.1) Else (e.2) = e.1; F Then (e.1) Else (e.2) = e.2; } 

Here the e1 is evaluated only when the expression entered matches 'True' Then e1 Else e2 the same for e2.

Squeeze blanks edit

 Squeeze { e.1'__'e.2 = <Squeeze e.1'_'e.2>; e.1 = e.1; } 

(Using '_' in place of space char so as to make the function call clear.) The first clause matches whenever the function Squeeze encounters double blanks in its input expression, and replaces it with a single blank. The second clause matches only when the first one did not, and returns the resultant value which is the current expression.

Squeeze using explicit looping edit

Squeeze { '__'e.1 = <Squeeze '_'e.1>; s.A e.1 = s.A <Squeeze e.1>; = ; }; 

References edit

  • Turchin, Valentin F. (1989). "REFAL-5 Programming Guide and Reference Manual". The City College of New York, New England Publishing Co., Holyoke.
  1. ^ Turchin, Valentin F. (1989). . REFAL-5 programming guide & reference manual. Holyoke: New England Publishing Co. Archived from the original on 2008-07-03. Retrieved 2010-04-05.
  2. ^ . Archived from the original on 2007-12-06. Retrieved 2008-03-18.

External links edit

  • Refal homepage

refal, this, article, relies, excessively, references, primary, sources, please, improve, this, article, adding, secondary, tertiary, sources, find, sources, news, newspapers, books, scholar, jstor, august, 2020, learn, when, remove, this, message, recursive, . This article relies excessively on references to primary sources Please improve this article by adding secondary or tertiary sources Find sources Refal news newspapers books scholar JSTOR August 2020 Learn how and when to remove this message Refal Recursive functions algorithmic language Russian REFAL is a functional programming language oriented toward symbolic computations including string processing language translation and artificial intelligence 1 It is one of the oldest members of this family first conceived of in 1966 as a theoretical tool with the first implementation appearing in 1968 Refal was intended to combine mathematical simplicity with practicality for writing large and sophisticated programs RefalParadigmPattern matching and term rewritingDesigned byValentin TurchinDeveloperValentin Turchin S Florentsev V Olyunin et al First appeared1968 1968 Typing disciplinestrong dynamicWebsitehttp www refal netMajor implementationsRefal 2 Refal 5 Refal 6 Refal One of the first functional programming languages to do so and unlike Lisp of its time Refal is based on pattern matching Its pattern matching works in conjunction with term rewriting The basic data structure of Lisp and Prolog is a linear list built by cons operation in a sequential manner thus with O n access to list s nth element Refal s lists are built and scanned from both ends with pattern matching working for nested lists as well as the top level one In effect the basic data structure of Refal is a tree rather than a list This gives freedom and convenience in creating data structures while using only mathematically simple control mechanisms of pattern matching and substitution Refal also includes a feature called the freezer to support efficient partial evaluation Refal can be applied to the processing and transformation of tree structures similarly to XSLT 2 Contents 1 Basics 2 Other examples 2 1 Factorial 2 2 Factorial with loops 2 3 Equality 2 4 If 2 5 Squeeze blanks 2 6 Squeeze using explicit looping 3 References 4 External linksBasics editThis section reads like a textbook Please improve this article to make it neutral in tone and meet Wikipedia s quality standards August 2020 A Refal Hello World example is shown below ENTRY Go lt Hello gt Hello lt Prout Hello world gt The program above includes two functions named Go and Hello A function is written as the name of the function followed by the function body in curly braces The Go function is marked as the entry point of the program using the ENTRY directive One could think of expressions in the function bodies as function calls in Lisp like syntax For example the Hello function appears to call the built in Prout function with the string Hello world as the argument The meaning and the mechanism of the call however is quite different To illustrate the difference consider the following function that determines whether a string is a palindrome Pal True s 1 True s 1 e 2 s 1 lt Pal e 2 gt e 1 False This example shows a function with a more complex body consisting of four sentences clauses A sentence begins with a pattern followed by an equal sign followed by a general expression on the right hand side A sentence is terminated with a semicolon For example the pattern of the second sentence of the function is s 1 and the expression is True As the example shows patterns include pattern variables that have the form of a character identifying the type of the variable what the variable matches followed by the variable identifier The variables that begin with an s match a single symbol those that begin with an e match an arbitrary expression The variable identifier can be an arbitrary alphanumeric sequence optionally separated from the type identifier by a dot A function executes by comparing its argument with the patterns of its sentences in the order they appear in the definition until the first pattern that matches The function then replaces the argument with the expression on the right hand side of the matched sentence If the result of a function application includes a subexpression in angle brackets as it will after the third sentence of our example is applied the result is further processed by Refal by invoking the function identified by the first symbol in the brackets Execution stops when the result has no more angle brackets to expand in this way The function Pal can thus be read informally as If the expression is empty replace it with True Otherwise if the expression is a single symbol replace it with True Otherwise if the expression is a symbol followed by an arbitrary expression e 2 followed by the same symbol replace it with the expression lt Pal e 2 gt In other words throw away the two identical symbols at the beginning and the end and recurse Otherwise replace the expression with False The pattern e 1 always matches The following are three step by step execution traces annotated with the sentence numbers applied at each step to produce the next lt Pal noon gt 3 lt Pal oo gt 3 lt Pal gt 1 True lt Pal wow gt 3 lt Pal o gt 2 True lt Pal revolver gt 3 lt Pal evolve gt 3 lt Pal volv gt 3 lt Pal ol gt 4 False We can now see that the Hello World example in fact executes as the sequence of the following expression transformations Seed the machine with the initial expression marked by ENTRY lt Go gt apply the sentence in Go lt Hello gt apply the sentence in Hello lt Prout Hello world gt Prout is a built in that prints and expands to nothing nothing to apply stop Other examples editFactorial edit Fact 0 1 s N lt s N lt Fact lt s N 1 gt gt gt Here 0 matches 0 the number and produces 1 On any other symbol which is a number multiply it with the result of Fact s N 1 Note the prefix style of operators Factorial with loops edit Fact s n lt Loop s n 1 gt Loop 0 s f s f s n s f lt Loop lt s n 1 gt lt s n s f gt gt As can be seen s n acts as the loop counter Equality edit Equal e 1 e 1 T e 1 e 2 F Here the function is defined as if given two terms and the terms are same then the first clause matches and produces True else the second clause matches and produces False An important property of Refal is that all functions in refal are single argument But may be decomposed into terms in an expression as above If edit Defining control structures is easy If T Then e 1 Else e 2 e 1 F Then e 1 Else e 2 e 2 Here the e1 is evaluated only when the expression entered matches True Then e1 Else e2 the same for e2 Squeeze blanks edit Squeeze e 1 e 2 lt Squeeze e 1 e 2 gt e 1 e 1 Using in place of space char so as to make the function call clear The first clause matches whenever the function Squeeze encounters double blanks in its input expression and replaces it with a single blank The second clause matches only when the first one did not and returns the resultant value which is the current expression Squeeze using explicit looping edit Squeeze e 1 lt Squeeze e 1 gt s A e 1 s A lt Squeeze e 1 gt References editTurchin Valentin F 1989 REFAL 5 Programming Guide and Reference Manual The City College of New York New England Publishing Co Holyoke Turchin Valentin F 1989 Introduction to Refal REFAL 5 programming guide amp reference manual Holyoke New England Publishing Co Archived from the original on 2008 07 03 Retrieved 2010 04 05 Refal The Language for Processing XML Documents Archived from the original on 2007 12 06 Retrieved 2008 03 18 External links editRefal homepage Retrieved from https en wikipedia org w index php title Refal amp oldid 1217753157, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.