Skip to content
Snippets Groups Projects
Commit d8a072ad authored by Michael Leuschel's avatar Michael Leuschel
Browse files

add first version of manual

parent 04f87a4e
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# External Functions
## LibraryStrings
In pure B there are only two built-in operators on strings: equality $=$ and inequality $\neq$.
This library provides several string manipulation functions, and assumes that STRINGS are
sequences of unicode characters (in UTF-8 encoding).
You can obtain the definitions below by putting the following into your DEFINITIONS clause:
`DEFINITIONS "LibraryStrings.def"`
The file `LibraryStrings.def` is bundled with ProB and can be found in the `stdlib` folder.
You can also include the machine `LibraryStrings.mch` instead of the definition file;
the machine defines some of the functions below as proper B functions (i.e., functions
for which you can compute the domain and use constructs such as
relational image).
%% Cell type:code id: tags:
``` prob
::load
MACHINE Jupyter
DEFINITIONS // "LibraryStrings.def"
STRING_LENGTH(xxx) == length(xxx);
EXTERNAL_FUNCTION_STRING_LENGTH == STRING --> INTEGER;
/* This external function takes two strings and concatenates them. */
STRING_APPEND(xxx,yyy) == append(xxx,yyy);
EXTERNAL_FUNCTION_STRING_APPEND == (STRING*STRING) --> STRING;
/* This external function takes a sequence of strings and concatenates them. */
STRING_CONC(string_conc_list) == "";
EXTERNAL_FUNCTION_STRING_CONC == seq(STRING) --> STRING;
/* This external function takes two strings and separates the first string
according to the separator specified by the second string. */
STRING_SPLIT(xxx,yyy) == split(xxx,yyy);
EXTERNAL_FUNCTION_STRING_SPLIT == ((STRING*STRING) --> (INTEGER<->STRING));
/* This external function takes a sequence of strings and a separator string
and joins the strings together inserting the separators as often as needed.
It is the inverse of the STRING_SPLIT function. */
STRING_JOIN(xxx,yyy) == join(xxx,yyy);
EXTERNAL_FUNCTION_STRING_JOIN == (((INTEGER<->STRING)*STRING) --> STRING);
STRING_CHARS(xxx) == chars(xxx);
EXTERNAL_FUNCTION_STRING_CHARS == (STRING --> (INTEGER<->STRING));
STRING_CODES(xxx) == codes(xxx);
EXTERNAL_FUNCTION_STRING_CODES == (STRING --> (INTEGER<->INTEGER));
/* This external function takes a string and converts it into an integer.
An error is raised if this cannot be done.
It is safer to first check with {\tt STRING\_IS\_INT} whether the conversion can be done. */
STRING_TO_INT(sss) == 0;
EXTERNAL_FUNCTION_STRING_TO_INT == (STRING --> INTEGER);
/* This external predicate takes a string and is true if the string represents an integer. */
STRING_IS_INT(sss) == (1=1);
EXTERNAL_PREDICATE_STRING_IS_INT == (STRING);
/* This external function takes a decimal string (with optional decimal places)
and converts it to an integer with the given precision. */
EXTERNAL_FUNCTION_DEC_STRING_TO_INT == STRING * INTEGER --> INTEGER;
DEC_STRING_TO_INT(decimal_string,precision) == 0;
/* parametric function; cannot be represented as constant function : */
STRING_TO_ENUM(sss) ==({}(1)); /* Note: you have to include the DEFINITION into your B file */
EXTERNAL_FUNCTION_STRING_TO_ENUM(STRING_TO_ENUM_TYPE) == (STRING --> STRING_TO_ENUM_TYPE);
TYPED_STRING_TO_ENUM(t,sss) ==({}(1));
EXTERNAL_FUNCTION_TYPED_STRING_TO_ENUM(STRING_TO_ENUM_TYPE) ==
(POW(STRING_TO_ENUM_TYPE)*STRING --> STRING_TO_ENUM_TYPE);
/* This external function converts an integer to a string representation. */
INT_TO_STRING(sss) == "0";
EXTERNAL_FUNCTION_INT_TO_STRING == (INTEGER --> STRING);
/* This external function converts an integer to a decimal string representation
with the precision provided by the second argument. */
INT_TO_DEC_STRING(integer,precision) == "0.0";
EXTERNAL_FUNCTION_INT_TO_DEC_STRING == (INTEGER*INTEGER --> STRING);
/* This external function converts a B data value to a string representation. */
TO_STRING(sss) == "0";
EXTERNAL_FUNCTION_TO_STRING(TO_STRING_TYPE) == (TO_STRING_TYPE --> STRING);
/* This external function takes a format string and a B sequence of values and generates an output
string, where the values have been inserted into the format string in place of the ~w placeholders.
*/
FORMAT_TO_STRING(MyFormatString,ListOfValues) == "0";
EXTERNAL_FUNCTION_FORMAT_TO_STRING(FORMAT_TO_STRING_TYPE) == ((STRING*seq(FORMAT_TO_STRING_TYPE)) --> STRING);
/* This external function checks whether the second string occurs contiguously within the first string. */
EXTERNAL_FUNCTION_STRING_CONTAINS_STRING == (STRING*STRING)--> BOOL;
STRING_CONTAINS_STRING(arg1,arg2)==FALSE; // TRUE when arg2 occurs as contiguous substring in arg1
END
```
%% Output
[2018-05-11 13:21:44,730, T+246558] "Shell-0" de.prob.cli.PrologProcessProvider.makeProcess(PrologProcessProvider.java:64): [INFO] Starting ProB's Prolog Core. Path is /Users/leuschel/.prob/prob2-3.2.10-SNAPSHOT/probcli.sh
[2018-05-11 13:21:45,901, T+247729] "Shell-0" de.prob.cli.PortPattern.setValue(PortPattern.java:30): [INFO] Server has started and listens on port 62905
[2018-05-11 13:21:45,901, T+247729] "Shell-0" de.prob.cli.InterruptRefPattern.setValue(InterruptRefPattern.java:29): [INFO] Server can receive user interrupts via reference 40975
[2018-05-11 13:21:45,904, T+247732] "ProB Output Logger for instance 342c2add" de.prob.cli.ProBInstance.readAndLog(ConsoleListener.java:48): [INFO] -- starting command loop --
[2018-05-11 13:21:45,926, T+247754] "ProB Output Logger for instance 342c2add" de.prob.cli.ProBInstance.readAndLog(ConsoleListener.java:48): [INFO] Connected: 127.0.0.1
[2018-05-11 13:21:46,041, T+247869] "ProB Output Logger for instance 342c2add" de.prob.cli.ProBInstance.readAndLog(ConsoleListener.java:48): [INFO] loading_classical_b(parser_version(2018-04-11 12:07:37.302),Jupyter,[/Users/leuschel/git_root/JAVAPROB/prob2-jupyter-kernel/notebooks/tests])
Loaded machine: Jupyter : []
%% Cell type:markdown id: tags:
### STRING_APPEND
This external function takes two strings and concatenates them.
Type: $STRING \times STRING \rightarrow STRING $.
%% Cell type:code id: tags:
``` prob
STRING_APPEND("abc","abc")
```
%% Output
"abcabc"
%% Cell type:code id: tags:
``` prob
STRING_APPEND("abc","")
```
%% Output
"abc"
%% Cell type:markdown id: tags:
### STRING_LENGTH
This external function takes a string and returns the length.
Type: $STRING \rightarrow INTEGER$.
%% Cell type:code id: tags:
``` prob
STRING_LENGTH("abc")
```
%% Output
3
%% Cell type:code id: tags:
``` prob
STRING_LENGTH("")
```
%% Output
0
%% Cell type:markdown id: tags:
### STRING_SPLIT
This external function takes two strings and separates the first string
according to the separator specified by the second string.
Type: $STRING \times STRING \rightarrow \mathit{seq}(STRING) $.
%% Cell type:code id: tags:
``` prob
STRING_SPLIT("filename.ext",".")
```
%% Output
{(1↦"filename"),(2↦"ext")}
%% Cell type:code id: tags:
``` prob
STRING_SPLIT("filename.ext","/")
```
%% Output
{(1↦"filename.ext")}
%% Cell type:code id: tags:
``` prob
STRING_SPLIT("usr/local/lib","/")
```
%% Output
["usr","local","lib"]
%% Cell type:code id: tags:
``` prob
STRING_SPLIT("",".")
```
%% Output
{(1↦"")}
%% Cell type:markdown id: tags:
I am not sure the following result makes sense, maybe a sequence of all characters is more appropriate?
%% Cell type:code id: tags:
``` prob
STRING_SPLIT("usr/local/lib","")
```
%% Output
{(1↦"usr/local/lib")}
%% Cell type:code id: tags:
``` prob
STRING_SPLIT("usr/local/lib","cal")
```
%% Output
{(1↦"usr/lo"),(2↦"/lib")}
%% Cell type:markdown id: tags:
### STRING_JOIN
This external function takes a sequence of strings and a separator string
and joins the strings together inserting the separators as often as needed.
It is the inverse of the `STRING_SPLIT` function.
Type: $\mathit{seq}(STRING) \times STRING \rightarrow STRING $.
%% Cell type:code id: tags:
``` prob
STRING_JOIN(["usr","local","lib"],"/")
```
%% Output
"usr/local/lib"
%% Cell type:code id: tags:
``` prob
STRING_JOIN(["usr/lo","/lib"],"cal")
```
%% Output
"usr/local/lib"
%% Cell type:code id: tags:
``` prob
STRING_JOIN(["usr/local/lib"],"")
```
%% Output
"usr/local/lib"
%% Cell type:markdown id: tags:
### STRING_CHARS
This external function takes a strings splits it into a sequence
of the individual characters. Each character is represented by a string.
Type: $STRING \rightarrow \mathit{seq}(STRING) $.
%% Cell type:code id: tags:
``` prob
STRING_CHARS("")
```
%% Output
%% Cell type:code id: tags:
``` prob
STRING_CHARS("abc")
```
%% Output
["a","b","c"]
%% Cell type:code id: tags:
``` prob
STRING_JOIN(STRING_CHARS("abc"),".")
```
%% Output
"a.b.c"
%% Cell type:code id: tags:
``` prob
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment