Changes between Version 10 and Version 11 of PIL


Ignore:
Timestamp:
10/05/24 14:45:32 (19 months ago)
Author:
siegel
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PIL

    v10 v11  
    661. PIL programs can be easily translated to PFG.
    771. PIL must also have a totally well-defined semantics and syntax.
     81. Every operation should have a well-defined outcome, even division by zero or an illegal pointer dereference.  However, when analyzing a PIL program, a user can specify exactly which of these operations should be considered erroneous and reported.   A reasonable default might be to make all of them erroneous.
    891. PIL programs can have nested funtion definitions.
    9101. PIL programs can use preprocessor directives like in C
    10 1. There are no automatic conversions.  All must be done by explicit casts.
    11 1. `$input`/`$output` variables in global scope (like CIVL-C)
    12 1. Identifiers are like in C, e.g.,  `x`, `f10`, ...
    13 1. Keywords, functions, etc. not in C start with `$` (like CIVL-C)
    14 1. Libraries: similar to C, `#include <stdlib.h>`, but these name PIL libraries and there are additional PIL libraries
     111. There are no automatic conversions.  There is no "array-pointer pun".  All conversions must be done by explicit casts or other expressions.  To convert an array `a` to a pointer to element 0 of `a`, write `&a[0]`.
     121. PIL supports `$input`/`$output` variables in global scope (like CIVL-C)
     131. PIL identifiers are the same as C's, e.g., `x`, `f10`, ...
     141. Keywords, built-in or library functions, constants, and types not in C start with `$` (like CIVL-C)
     151. PIL supports libraries: similar to C, `#include <stdlib.h>`, but these name PIL libraries and there are additional standard PIL libraries not in the C standard library
     161. Program order:
     17 1. A program is a set of variable, function, and type definitions.
     18 1. The order is totally irrelevant.
     19 1. A variable can be used anywhere it is in scope.
     20 1. A function can be called anywhere it is in scope.
    15211. PIL programs can be divided into multiple files (translation units)
    16  1.  One TU can refer to a variable, function, type, in another TU.
    17  1. the variable just needs to be declared somewhere in the TU
    18  1. the function just needs a prototype somewhere in the TU
    19  1. the use of "static" in a variable decl or function def makes it private to that TU (so two can have same name)
    20  1. no need for "extern"
    21  1. at most one of the TUs declaring the variable can have an initializer
    22  1. at most one of the TUs can have a definition for a function
    23 1. Program order:
    24  1. a program is a sequence of variable, function, and type definitions.
    25  1. the order is totally irrelevant.
    26  1. a variable can be used anywhere it is in scope
    27  1. a function can be called anywhere it is in scope
     22 1. One TU can refer to a variable, function, or type, in another TU.
     23 1. The variable just needs to be declared somewhere in the TU.
     24 1. The function just needs a prototype somewhere in the TU.
     25 1. The use of `static` in a variable declaration or function definition makes it private to that TU (so two can have same name), as in C.
     26 1. There is no need for `extern` so this is not in PIL.
     27 1. At most one of the TUs declaring the variable can have an initializer.
     28 1. At most one of the TUs can have a definition for a function.
    2829
    2930== Types
    30 - '''type names''' are used for all declarations (no C declarators).  Examples:
    31  * `$int[]`: array of integer
    32  * `$int*`: pointer to integer
    33  * `$int*[]`: array of pointer to integer
    34  * `$int[]*`: pointer to array of integer
    35  * `$int*[]($real)`: function from Real to array of pointer to integer
     31- '''type names''' are used for all declarations.  There are no C declarators.  Examples:
     32 * `$int[] a`: declares `a` to be an array of integer
     33 * `$int* p`: pointer to integer
     34 * `$int*[] b`: array of pointer to integer
     35 * `$int[]* q`: pointer to array of integer
     36 * `$int*[]($real) f`: function from Real to array of pointer to integer
    3637- basic types:
    37 - what are the limits of these types?  to be decided
    38 - `$int`, `$real` (mathematical types)
    39 - `struct`: as in C
    40 - `union`: as in C
    41 - `T[]`: sequence of `T`.  Note: no "T[n]".  sequences can be assigned, returned, passed as arguments, etc. 
    42 - `$set<T>` : finite set of `T`, ops to add, contains, remove, size, ...
    43 - `$map<T1,T2>` : finite map from `T1` to `T2`, ops to put, get, containsKey, ...
    44 - `$fun<T1,T2>` :  "logic functions":  deterministic, total, side-effect free functions from `T1` to `T2`
    45 - `$tuple<T,...>` : tuples of specified type (similar to struct)
    46 - `$rel<T,...>` : relations of specified type (set of tuples)
     38 * `$int`: the set of integers
     39 * `$real`: the set of Reals
     40 * `fint[lo,hi]`: set of integers between `lo` and `hi`, inclusive. `lo` and `hi` are constant expressions so are known at compile time.
     41 * `uint[hi]`: nonnegative integers less than the constant expression`hi`.  Arithmetic is performed modulo `hi` (like C's unsigned integer types).
     42 * `$float<p,emax>`: the set of IEEE binary floating point numbers with precision `p` and emax `emax`.  These are also constant expressions.
    4743- `T*`: pointer to `T`
    48  -   what can be pointed to: var, array element, struct element, union member,  ...
    49 - Type definitions: `typedef typename ID;`
     44- `struct TAG { T1 f1; ... Tn fn; }`: a C struct
     45- `union`: similar
     46- `T[]`: sequence of `T`.  Note: there is no "T[n]".  Sequences are first-class values: they can be assigned, returned, passed as arguments, etc. 
     47- `R(T1, ..., Tn)`: the type of a procedure which consumes values in `T1`, ..., `Tn` and returns a value in `R`.   This is basically C's function type.
     48- `$fun<T1,T2>` :  "logic functions":  deterministic, total, side-effect free functions from `T1` to `T2`.  Note however the function may depend on the state (i.e., the state should be considered a hidden input).   A `$pure` function is a function that does not depend on the state.
     49- `$set<T>` : finite set of `T`
     50- `$map<T1,T2>` : finite map from `T1` to `T2`.  A map is a set of ordered pairs `(x,y)` with the property that if `(x,y)` and `(x,z)` are in the map, then `y`=`z`.
     51- `$tuple<T,...>` : tuples of specified type.  This is similar to `struct`, but is anonymous and the fields do not have names.
     52- Type definitions have the form: `typedef typename ID;`
    5053
    5154== Functions
    5255
    5356There are two kinds of functions in PIL:
    54 1. Imperative functions = "procedures".  A procedure has a type of the form `R(T1,...,Tn)` where `R` is the return type and the Ti are the input types.
     571. Imperative functions = "procedures".  A procedure has a type of the form `R(T1,...,Tn)` where `R` is the return type and the `Ti` are the input types.
    55581. Logic functions.  One of these has a type of the form `$fun<R,T1,...,Tn>`.
    5659
     
    6366defines a procedure named `f` which consumes inputs of types `T1`, ..., `Tn` and returns a value of type `R`.    `R` can be `void` if the procedure does not return a value.
    6467
    65 The definition above defines a '''constant''' `f` of type '''`R(T1, ..., Tn)`'''.   Procedures are first-class values.    One may declare a variable of type `R(T1, ..., Tn)`, a procedure may return a value of that type, a procedure may consume a value of that type, a value of that type may be assigned to a variable, etc.   Hence the procedure type is just like any other type, and procedure definitions define new constants of that type, just as `1` is a constant of type `$int`.   Note this is different from C in that C uses function pointers; PIL dispenses with the need for function pointers.
     68The definition above defines a '''constant''' `f` of type '''`R(T1, ..., Tn)`'''.   Procedures are first-class values.    One may declare a variable of type `R(T1, ..., Tn)`, a procedure may return a value of that type, a procedure may consume a value of that type, a value of that type may be assigned to a variable, etc.   Hence the procedure type is just like any other type, and procedure definitions define new constants of that type, just as `1` is a constant of type `$int`.   Note this is different from C in that C uses function pointers; PIL dispenses with the need for function pointers and just uses functions.
    6669
    6770A '''procedure call expression'''  has the usual form `g(e1, ..., en)`.  This is an expression that can be used anywhere an expression with side-effects is allowed.  Here, `g` is an expression of functional type, say `R(T1, ..., Tn)`, and `ei` is an expression of type `Ti` (for i=1, ..., n).  The procedure call expression has type `R`. 
     
    7881}}}
    7982
    80 Procedure definitions can be nested.   It is an error to call a procedure `f` when `f` is not in scope.  (This is similar to Gnu C.)   In other words, if the call takes place in dyscope d, then the definition of `f` must be in d's static scope, or in the parent of d's static scope, or its parent, etc.
     83Procedure definitions can be nested.   It is an error to call a procedure `f` when `f` is not in scope.  (This is similar to GNU C.)   In other words, if the call takes place in dyscope d, then the definition of `f` must be in d's static scope, or in the parent of that scope, or its parent, etc.
    8184
    8285There is a second way to specify a procedure, using a lambda expression, which is described below.
     
    8487=== Logic functions
    8588
    86 Logic functions are a certain class of functions that have no side-effects, and are deterministic total functions of their arguments and the current state.  A logic function has a type of the form `$fun<R,T1,...,Tn>`, logical functions which consume inputs of type `T1`, ..., `Tn` and return a value of type `R`. 
     89Logic functions are a certain class of functions that have no side-effects, and are deterministic total functions of their arguments and the current state.  A logic function has a type of the form `$fun<R,T1,...,Tn>`, which signifies the set of logical functions which consume inputs of type `T1`, ..., `Tn` and return a value of type `R`. 
    8790
    8891Logic functions are also first-class objects in PIL.   An application of a logic function `f(x1,... ,xn)` is a side-effect-free expression that can be used anywhere an expression is allowed.  A logic function is not necessarily pure, i.e., the value of an application may depend on any part of the state, not just the arguments.
     
    103106       <T1,T2> $logic int g($map<T1,T2> f, T1 x) { ... }
    104107}}}   
    105 This defines one procedure for each assignment of types to the `Ti`.
     108This defines one procedure or logic function for each assignment of types to the `Ti`.
    106109
    107110Both kinds of functions can be declared without providing definitions, indicating that the definition can be found in a different translation unit:
     
    122125* the `Ti` and `Uj` are types
    123126* the `xi` and `vj` are variables
     127* the `initj` are expressions that can refer to any variables in scope
    124128* R is a type (the return type), which may be void
    125129* {S1; ...} is a block (same as in a procedure definition)
     
    140144* the `Ti` and `Uj` are types
    141145* the `xi` and `vj` are variables
     146* the `initj` are expressions that can refer to any variables in scope
    142147* R is a type (the return type), which cannot be void
    143148* `expr` is a side-effect-free expression of type R
     
    146151The type of this expression is `$fun<R, T1, ..., Tn>`.   As with procedural lambdas, this yields a logic function with a dynamic scope that persists, so can be called anywhere, even after the lambda expression goes out of scope.
    147152
    148 In both cases, the initializer expressions `initj` are expressions using any variables in scope.
    149 
    150153== Tuples
    151154
     
    154157- `t.i`
    155158- `($tuple<T1,...>){ x1, ... }`
    156 - `$logic $tuple<T1,...> $tuple_write($tuple<T1,...> t, $int i, Ti x);`
     159- `$pure $logic $tuple<T1,...> $tuple_write($tuple<T1,...> t, $int i, Ti x);`
    157160
    158161Mutating expressions:
    159 - `t.i = x;`
     162- `t.i = x`
    160163
    161164== Sets
     
    164167- `s1 == s2`
    165168- `($set<T>)$empty`   // empty set of type T
    166 - `$logic _Bool $set_in(T x, $set<T> s);`  // is x an element of s?
    167 - `$logic $set<T> $set_with($set<T> s, T x);` // s U {x}
    168 - `$logic $set<T> $set_without($set<T> s, T x);` // s - {x}
    169 - `$logic $set<T> $set_union($set<T> s1, $set<T> s2);` // s1 U s2
    170 - `$logic $set<T> $set_difference($set<T> s1, $set<T> s2);` // s1-s2
    171 - `$logic $set_intersection($set<T> s1, $set<T> s2);` // s1 \cap s2
    172 - `$logic T[] $set_elements($set<T> s);`
    173 - `$logic _Bool $set_isSubsetOf($set<T> s1, $set<T> s2);`
    174 - `$logic $set<U> $set_map($set<T> s, $fun<T,U> f);`
     169- `$pure $logic _Bool $set_in(T x, $set<T> s);`  // is x an element of s?
     170- `$pure $logic $set<T> $set_with($set<T> s, T x);` // s U {x}
     171- `$pure $logic $set<T> $set_without($set<T> s, T x);` // s - {x}
     172- `$pure $logic $set<T> $set_union($set<T> s1, $set<T> s2);` // s1 U s2
     173- `$pure $logic $set<T> $set_difference($set<T> s1, $set<T> s2);` // s1-s2
     174- `$pure $logic $set_intersection($set<T> s1, $set<T> s2);` // s1 \cap s2
     175- `$pure $logic T[] $set_elements($set<T> s);`
     176- `$pure $logic _Bool $set_isSubsetOf($set<T> s1, $set<T> s2);`
     177- `$pure $logic $set<U> $set_map($set<T> s, $fun<T,U> f);`
    175178
    176179Mutating procedures: