RascalTutor

This is a test synopsis.

Description

See examples below!!!

Examples

rascal>import Content;
ok
rascal>html("hello", "this is some \<strong\>HTML\</strong\> output")
|prompt:///|(14,47,<1,14>,<1,61>): CallFailed(["hello","this is some \<strong\>HTML\</strong\> output"])
	at $shell$(|prompt:///|(0,15,<1,0>,<1,15>))
ok
rascal>file("image", |https://www.rascal-mpl.org/assets/ico/favicon.png|)
|prompt:///|(14,51,<1,14>,<1,65>): CallFailed([
    "image",
    |https://www.rascal-mpl.org/assets/ico/favicon.png|
  ])
	at $shell$(|prompt:///|(0,62,<1,0>,<1,62>))
ok
rascal>1 + 1 == 2
bool: true
rascal>int count = 1;
int: 1
rascal>content("counter", Response (Request _) { count += 1; return response("count: <count>"); })
ok
rascal>count;
int: 1
rascal>count = 66;
int: 66
rascal>content("counter", Response (Request _) { count += 1; return response("count: <count>"); })
ok
rascal>count;
int: 66

WARNING: unexpected errors in the above SHELL example. Documentation author please fix! * emphasis * bold * Rascal Web site * [CallAnalysis] * Table:

| Module | LOC |
|--------|-----|
| A      | 10 |
| B      | 20 |

| Operator | Description | |------------|------------| | $A$ \| $B$ | alternative | | \|\| | or |

Horizontal rule:


  • code

  • in code: italics

<toc Test 2>

Benefits

Pitfalls

1. Synopsis

Analyzing the call structure of an application.

2. Description

Suppose a mystery box ends up on your desk. When you open it, it contains a huge software system with several questions attached to it:

  • How many procedure calls occur in this system?

  • How many procedures does it contains?

  • What are the entry points for this system, i.e., procedures that call others but are not called themselves?

  • What are the leaves of this application, i.e., procedures that are called but do not make any calls themselves?

  • Which procedures call each other indirectly?

  • Which procedures are called directly or indirectly from each entry point?

  • Which procedures are called from all entry points?

Let’s see how these questions can be answered using Rascal.

Examples: Consider the following call graph (a box represents a procedure and an arrow represents a call from one procedure to another procedure):

![calls](calls.png)

Rascal supports basic data types like integers and strings which are sufficient to formulate and answer the questions at hand. However, we can gain readability by introducing separately named types for the items we are describing. First, we introduce therefore a new type Proc (an alias for strings) to denote procedures:

rascal>alias Proc = str;
ok

Next, we have to represent the call relation as a Rascal datatype, and the relation is the most appropriate for it. As preparation, we also import the libraries [$Rascal:Prelude/Set], [$Rascal:Prelude/Relation] and [$Rascal:Prelude/Graph] that will come in handy.

rascal>import Set;
ok
rascal>import Relation;
ok
rascal>import analysis::graphs::Graph;
ok
rascal>rel[Proc, Proc] Calls = {<"a", "b">, <"b", "c">, <"b", "d">, <"d", "c">, <"d", "e">, <"f", "e">, <"f", "g">, <"g", "e">};
rel[str,str]: {
  <"a","b">,
  <"g","e">,
  <"b","c">,
  <"b","d">,
  <"d","c">,
  <"d","e">,
  <"f","e">,
  <"f","g">
}

Now we are in a good position to start asking some questions.

How many calls occur in this system? We use the function [Rascal:Set/size] to determine the number of elements in a set or relation. Since each tuple in the Calls relation represents a call between procedures, the number of tuples is equal to the number of calls.

rascal>size(Calls);
int: 8

How many procedures occur in this system? This question is more subtle, since a procedure may call (or be called) by several others and the number of tuples is therefore not indicative. What we need are the set of procedures that occur (as first or second element) in any tuple. This is precisely what the function [$Rascal:carrier] gives us:

rascal>carrier(Calls)
set[str]: {"a","b","c","d","e","f","g"}

and computing the number of procedures is now easy:

rascal>size(carrier(Calls));
int: 7

As an aside, functions [$Rascal:Prelude/Relation/domain] and [$Rascal:Prelude/Relation/range] do the same for the first, respectively, second element of the pairs in a relation:

rascal>domain(Calls);
set[str]: {"a","b","d","f","g"}
rascal>range(Calls);
set[str]: {"b","c","d","e","g"}

What are the entry points for this system? The next step in the analysis is to determine which entry points this application has, i.e., procedures which call others but are not called themselves. Entry points are useful since they define the external interface of a system and may also be used as guidance to split a system in parts. The top of a relation contains those left-hand sides of tuples in a relation that do not occur in any right-hand side. When a relation is viewed as a graph, its top corresponds to the root nodes of that graph. Similarly, the bottom of a relation corresponds to the leaf nodes of the graph. See the section called ?Graph? for more details. Using this knowledge, the entry points can be computed by determining the top of the Calls relation:

rascal>top(Calls);
set[str]: {"a","f"}

What are the leaves of this application?

In a similar spirit, we can determine the leaves of this application, i.e., procedures that are being called but do not make any calls themselves:

rascal>bottom(Calls);
set[str]: {"c","e"}

Which procedures call each other indirectly?

We can also determine the indirect calls between procedures, by taking the transitive closure of the Calls relation, written as Calls+. Observe that the transitive closure will contain both the direct and the indirect calls.

rascal>rel[Proc,Proc] closureCalls = Calls+;
rel[str,str]: {
  <"g","e">,
  <"a","b">,
  <"a","c">,
  <"a","d">,
  <"a","e">,
  <"b","c">,
  <"b","d">,
  <"b","e">,
  <"d","c">,
  <"d","e">,
  <"f","e">,
  <"f","g">
}

Which procedures are called directly or indirectly from each entry point?

We now know the entry points for this application ("a" and "f") and the indirect call relations. Combining this information, we can determine which procedures are called from each entry point. This is done by indexing closureCalls with appropriate procedure name. The index operator yields all right-hand sides of tuples that have a given value as left-hand side. This gives the following:

rascal>set[Proc] calledFromA = closureCalls["a"]
>>>>>>>set[Proc] calledFromF = closureCalls["f"]
           ^ Parse error here
ok

WARNING: unexpected errors in the above SHELL example. Documentation author please fix! Which procedures are called from all entry points? Finally, we can determine which procedures are called from both entry points by taking the intersection of the two sets calledFromA and calledFromF:

rascal>calledFromA & calledFromF
|prompt:///|(0,11,<1,0>,<1,11>): Undeclared variable: calledFromA
Advice: |http://tutor.rascal-mpl.org/Errors/Static/UndeclaredVariable/UndeclaredVariable.html|
ok

WARNING: unexpected errors in the above SHELL example. Documentation author please fix! or if your prefer to write all of the above as a one-liner using a [$Rascal:Expressions/Reducer] expression:

rascal>(carrier(Calls) | it & (Calls+)[p] | p <- top(Calls));
set[str]: {"e"}

The reducer is initialized with all procedures (carrier(Calls)) and iterates over all entry points (p ← top(Calls)). At each iteration the current value of the reducer (it) is intersected (&) with the procedures called directly or indirectly from that entry point ((Calls+)[p]).

3. Benefits

  • In small examples, the above results can be easily obtained by a visual inspection of the call graph. Such a visual inspection does not scale very well to large graphs and this makes the above form of analysis particularly suited for studying large systems.

4. Pitfalls

  • We discuss call analysis in a, intentionally, simplistic fashion that does not take into account how the call relation is extracted from actual source code. The above principles are, however, applicable to real cases as well.

    # Questions

4.1. If

Synopsis

Conditional statement.

Syntax
  • if ( Exp ) Statement;

  • if ( Exp ) Statement1 else Statement2;

Types

Exp

if ( Exp ) Statement;

bool

void

Exp

Statement1

Statement2

if ( Exp ) Statement1 else Statement2;

bool

T1

T2

lub(T1, T2)

Description

The test Exp is evaluated and its outcome determines the statement to be executed: Statement1 if Exp yields true and Statement2 otherwise. The value of an if-then statement is equal to Statement when its test is true. Otherwise it is void. The value of an if-then-else statement is the value of the statement that was executed.

Examples
rascal>if( 3 > 2 ) 30; else 40;
int: 30
rascal>x = if( 3 > 2 ) 30; else 40;
int: 30
rascal>if( 3 > 2 ) 30;
int: 30

An if-then statement yields void when its test is false (demonstrated by the ok that is printed by the Rascal system):

rascal>if( 2 > 3 ) 30;
ok

4.2. Synopsis

Library functions

4.3. Boolean

Usage

import Boolean;

Synopsis

Library functions for Booleans.

Description

For operators on Boolean values see Boolean in the Rascal Language Reference.

The following functions are defined for Booleans:

  • arbBool: Return an arbitrary Boolean value.

  • fromString: Convert the strings "true" or "false" to a bool

  • toInt: Convert a Boolean value to integer.

  • toReal: Convert Boolean value to real.

  • toString: Convert Boolean value to string.

4.3.1. arbBool

Function

bool arbBool()

Synopsis

Return an arbitrary Boolean value.

Examples
rascal>import Boolean;
ok
rascal>arbBool();
bool: true
rascal>arbBool();
bool: false
rascal>arbBool();
bool: false
Benefits

arbInt is a convenient generator for arbitrary binary choices.

4.3.2. fromString

Function

bool fromString(str s)

Synopsis

Convert the strings "true" or "false" to a bool.

4.3.3. toInt

Function

int toInt(bool b)

Synopsis

Convert a Boolean value to integer.

Description

Maps true to 1 and false to 0.

Examples
rascal>import Boolean;
ok
rascal>toInt(true);
int: 1
rascal>toInt(false);
int: 0

4.3.4. toReal

Function

real toReal(bool b)

Synopsis

Convert Boolean value to real.

Description

Maps true to 1.0 and false to 0.0.

Examples
rascal>import Boolean;
ok
rascal>toReal(true);
real: 1.0
rascal>toReal(false);
real: 0.0

4.3.5. toString

Function

str toString(bool b)

Synopsis

Convert Boolean value to string.

Description

Maps true to "true" and false to "false".

Examples
rascal>import Boolean;
ok
rascal>toString(true);
str: "true"
rascal>toString(false);
str: "false"

5. Questions

Question 1

Which means of transportation is faster:

Apache Helicopter
High-speed train
Ferrari F430
Hovercraft
Question 2

Replace the text box by the result of the multiplication and make the test true:

module Question2

test bool multiplication() =
     6 * 5 == 
;
Question 3

Replace the text box by the result of the intersection and make the test true:

module Question3

test bool setIntersection() =
     {-45,88,89,-97,-28,49} & {-45,88,89,-97,-28,49} == 
;
Question 4

Replace the text box by a function name and make the test true:

module Question4
import List;

test bool listFunction() =
 
([79, 61, 51, 31, 67, -8, 68, -16, -30]) == 9;
Question 5

Create a function to print squares by placing all code fragments in the grey box in the right order with the right indentation:

println("Squares from 1 to " + n);
}
void squares(int n){
println(i + " squared = " + (i * i));
for(int i <- [1 .. n + 1])
Question 6

Create a function to print squares by placing all code fragments in the grey box in the right order with the right indentation (and avoid decoys!):

i += 1;
println("Squares from 1 to " + n);
println(i + " squared = " + (i + i));
println(i + " squared = " + (i * i));
for(int i <- [1 .. n + 1])
i = 0;
void squares(int n){
}
Question 7

Click on all identifiers in this code fragment:

x = 1;
if(x){
   y = x + 2;
}
Question 8

Reorder the following items and make all statements true:

  • {1,2,3}
  • [1,2,3]
  • 123
  • "abc"
  • is a set[int]
  • is an int
  • is a str
  • is a list[int]