2.0 Declarative computation model Flashcards
What is a computation model?
formal system that defines a language and how expressions and statements of the language are executed by an abstract machine.
What is a programming model?
Programming techniques and principles used to write programs in the language of the computation model.
What is declarative programming?
Stateless programming
Evaluating functions over partial data structures.
Programming with functions, complete values, deterministic logic.
Can be made concurrent.
What is imperative programming?
Stateful programming
What is language syntax?
What are legal programs, programs that can be executed.
What is language grammars?
Rules on how to make statements (‘sentences’) out of tokens (‘words’).
What is a statement?
Sequence of tokens
What is a token?
Sequence of characters
What is a tokenizer?
A lexical analyser.
Program that takes a sequence of characters and returns a sequence of tokens.
What is a parser?
A program that takes a sequence of tokens and returns a parse tree.
What is a parse tree?
Shows the structure of statements.
What is the Extended Backus-Naur form (EBNF)?
Notation for defining grammar.
Uses terminal- and non-terminal symbols.
What is a terminal symbol?
Tokens
What is a non-terminal symbol?
Sequence of tokens.
Defined by a grammar rule, that show how to expand it into tokens.
Give an example of representing the nonterminal <digit> in EBNF</digit>
<digit> ::= 1|2|3|4|5|6|7|8|9|0
The nonterminal <digit> can be any of the ten tokens.
|: means or
</digit></digit>
How can an integer be represented using EBNF?
<int> ::= <digit> {<digit>}
An integer is a digit followed by any number of digits.
<digit>: Nonterminal
{...}: Means repeat whatever is inside, any number of times, including none.
</digit></digit></digit></int>
What is a formal language?
Set of all possible statements generated by a grammar.
What are context free grammars?
The expansion of a nonterminal is always the same no matter where it is used.
Nonterminals can only use already-declared variables.
For example, context free grammars does not allow using variables that is declared before they are used - this is a context dependency.
Can be ambiguous: There can be several parse trees corresponding to a given token sequence. This can cause different results depending on how an expression is evaluated.
Example: 23+4 can give trees representing (23) + 4, and 2 * (3+4)
What is context-sensitive grammar?
Grammar containing nonterminals whose use depends on the context where it is used.
How are the grammars of most practival programming languages defined?
Two parts, as a context free grammar, supplemented with a set of extra conditions composed by the language.
What does it mean to disambiguate a grammar?
Add extra conditions to grammars, that restrict the parser so that only one parse tree is possible.
What is a precedence condition?
A condition for expressions with multiple operators, e.g. (2+3*4).
Each operator is given a precedence level. The higher the level, the deeper into the parse tree they are put (away from the root).
When an operator is deeper in the pipeline than another, we say it binds tighter than the other operator.
What is a Associativity condition?
A condition for expressions with multiples of the same operators, e.g. (2-3-4).
Because all the operator would get the same precedence level, we add an associativity condition to be able to disambiguate them.
An associativity condition defines whether the leftmost or the rightmost operator binds tighter.
(2-3-4)
left: (2-3) - 4
right: 2- (3-4)
How can partial definitions of nonterminals be written?
Using ‘…’
<expression> ::= <variable> | <int> | ...
</int></variable></expression>
Define the nonterminal <statement> using EBNF, a keyword and a string</statement>
<statement> ::= skip | <exp> '=' <exp>
skip: Keyword, does not need quotes as we know that it is a keyword
'=': add string in between nonterminals using quote
</exp></exp></statement>
What does [] mean in EBNF?
Whatever is within the brackets can occur zero or one time.
What does {…}+ mean in EBNF?
{} must contain minimum 1 expression
What is the semantics of a language?
What a program does when it executes the language.
What is the kernel language approach used for?
Defining the semantics of a program.
Define a kernel language, and combine with the data structures it manipulates forms the kernel computation model.
Define a translation scheme from a programming language to the kernel language. Each grammatical construct in the programming language is translated into kernel language. Translation is done by using linguistic abstraction and syntactic sugar.
NB: Each computation model has its own kernel language. These kernel languages builds on the kernel language of the models predecessor by adding concepts.
The declarative kernel language is an example of a kernel language.
What is the difference between a practical language and a kernel language?
Practical language provides abstractions for the programmer.
Kernel language contains minimalset of intuitive concepts.
Easy to reason in.
Has formal semantics
What is the four approaches to language semantics?
Operational
Axiomatic
Denotational
logical
What is an operational semantic?
Shows how statements execute in terms of an abstract machine
What is an axiomatic semantic?
Defines semantics as the relation between the input state and the output state, meaning, the situation before and after executing a statement.
What is an denotational semantic?
The semantics defines a statement as a function over an abstract domain.
What is an logical semantic?
Defines a statement as a model of a logical theory.
What is a linguistic abstraction?
Constructs that are both an abstraction and an addition to the language syntax.
Used when we need to extend a language (for example defining loops in a declarative language)
How are linguistic abstractions defined?
- Define a new grammatical construct
- Define its translation into the kernel language
Some examples:
fun, lazy, for, class
What is syntactic sugar?
Shortcut notation for frequently used idioms. This notation is part of the language syntax, and is defined by grammar.
What is the difference between syntactic sugar and linguistic abstraction?
Syntactic sugar does not extend the language, just shortens the program size.
How is syntactic sugar used when introducing local variables in e.g. if statements?
else
Local L in
…
end
end
else L in
…
end
What is an alternative to translation, when defining language semantics?
Interpreter approach.
An interpreter is written in language L1 and accepts programs written in language L2 and executes them.
The semantics is defined by giving an interpreter for the language.
In this case, new language features are created by extending the interpreter.
What are the datastructures used in declarative models?
A single assignment store: A set of variables initially unbound, and can be bound to one value.
Store:
{x1=1, x2=[2 4 5 6], x3}
x1 and x2 are bound, x3 is unbound.
What is a value store?
A store where all variables are bound to values.
What are declarative variables?
Variables in a single assignment store.
Also called dataflow variables.
Once bound, it stays bound throughout program execution, and is indistinguishable from its value.
This means that the expression a+b is the same as doing 11+23 if the store contains:
{a=11, b=23}
What are some benefits of using single assignment stores and not value stored?
Can store procedure returns in unbound input variables.
Declarative concurrency requires use of unbound variables.
Needed for relational- and constraint programming.
Efficiency - used in tail recursion.
What is a variable identifier?
Textual name that refers to a store-variable and can be used from outside the store.
Mapping from variable identifiers to store entities is called an environment.
store:
{x1: 100}
environment:
{X -> x1}
in statement: ‘X’