1.1Import
1.3Print
1.4Check
1.5Comment
8.18
top
← prev up next →

Recursive LanguageπŸ”— i

Sorawee Porncharoenwase <sorawee.pwase@gmail.com>

A language for writing recursively computable functions (or simply recursive functions), which are functions in recursion theory that consume natural numbers and produce a natural number. See Chapter 6 of Computability and Logic (George Boolos, John P. Burgess, and Richard Jeffrey) for details.

1Language FeaturesπŸ”— i

1.1ImportπŸ”— i

To make it clear what constructs and functions are used as a basis to define recursive functions, we need to explicitly import them with the import statement:

import<construct-1>,<construct-2>,...,<construct-n>;

Following are core constructs:

  • z and s functions (zero constant function and successor function, with arity 1)

  • id_<place>^<arity> where <place> and <arity> are natural numbers (identity/projection functions, with arity <arity>)

  • Cn[<f>, <g-1>, ..., <g-m>] (composition)

  • Pr[<f>, <g>] (primitive recursion)

  • Mn[<f>] (minimization)

Furthermore, following derived functions (presented in the book) are also provided for convenience.

  • const_<n> where <n> is a natural number (constant function, with arity 1)

  • sum and prod (addition and multiplication, with arity 2)

  • pred (predecessor function, with arity 1)

  • monus (subtraction on natural numbers, with arity 2)

  • sgn and ~sgn (zero predicate and its negated form, with arity 1)

1.2DefinitionπŸ”— i

We can define recursive functions using = where the LHS is an identifier name and the RHS is an expression that builds a recursive function. For example:

sum=Pr[id_1^1,Cn[s,id_3^3]];
prod=Pr[z,Cn[sum,id_1^3,id_3^3]];

defines sum to be the (primitive) recursive function Pr[id_1^1, Cn[s, id_3^3]] and prod to be the (primitive) recursive function Pr[z, Cn[sum, id_1^3, id_3^3]]

1.3PrintπŸ”— i

We can print results from function invocation by using the print statement.

printsum(10,23);

Running the program will result in 33.

1.4CheckπŸ”— i

We can use the check construct to test that our recursive functions are correct on given inputs. For example:

checksum(10,23)=33;

checks that sum(10, 23) should have the result 33.

1.5CommentπŸ”— i

Comment can be written by using #....

2ExampleπŸ”— i

 
importPr,Cn,s,id,z,const;
 
sum=Pr[id_1^1,Cn[s,id_3^3]];
printsum(2,23);#shouldbe25
 
prod=Pr[z,Cn[sum,id_1^3,id_3^3]];
printprod(3,3);#shouldbe9
 
fact=
Cn[Pr[const_1,Cn[prod,Cn[s,id_2^3],id_3^3]],
id_1^1,id_1^1];
 
printfact(fact(3));#shouldbe720
printfact(Cn[s,s](4));#shouldbe720
 
checkfact(3)=6;

3AcknowledgmentsπŸ”— i

Thanks to Matthias Felleisen for giving me advice on Racket macrology back in 2018.

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /