Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 1
Reading The Fortress Language Specification
Masato Takeichi IST, University of Tokyo
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 2
The Fortress Language Specification
• Version 1.0 Beta – March 6, 2007
• Available at http://research.sun.com/projects/plrg/fortress.pdf
Fortress Interpreter
• Available at http://fortress.sunsource.net
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 3
Chapter 1 Introduction (1)
• The Fortress Programming Language – General Purpose – Statically Typed – Component-based
• Designed for – Producing Robust High-performance Software – With Programmability
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 4
Chapter 1 Introduction (2)
• Fortress – Is a “Growable Language” – Supports state-of-the-art compiler optimization
techniques – Has an extensible component system – Supports modular and extensible parsing – Name derived from Fortran – Has little relation to Fortran other than its
intended application domain – Does not support for backward compatibility
with existing versions of Fortran
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 5
Chapter 1 Introduction (3)
• Aspects of Fortress – From object-oriented and functional
languages • Java, NextGen, Scala, Eiffel, Self • Standard ML, Objective Caml, Haskell, Scheme
– Employs cutting-edge features from programming language community
– Achieves an unprecedented combination of performance and programmability
• Fortress is an “Open Source Project”
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 6
1.1 Fortress in a Nutshell (1)
• Object – Consists of fields and methods specified in
definition
• Trait – Named program constructs that declare sets
of methods – A method declared by trait may be either
abstract or concrete • Abstract methods have headers only • Concrete methods also have definitions
– May extend other traits • Inherits the methods provided by the traits it extends
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 7
1.1 Fortress in a Nutshell (2)
traits
fields
object
method
object
Static checking of physical dimensions
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 8
1.1 Fortress in a Nutshell (3)
• Notations – Allows Unicode characters, subscripts and superscripts
in identifiers – Follows mathematical conventions
• Variable references in italics • Multiplication expressed by simple juxtaposition
– Supports operator overloading – Facilitates extension of syntax with domain-specific
languages
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 9
1.1 Fortress in a Nutshell (4)
• Types – Statically and nominally typed – Types not specified for all fields, nor all method
parameters and return values: – Type inference used wherever possible – Types can be parametric with respect to other types
and values
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 10
1.1 Fortress in a Nutshell (5)
• Functions – Allows top-level function definitions in addition to
objects and traits – Functions are first-class values:
• Functions can be passed to and returned from functions • Functions are assigned as values to fields and variables
– Functions and methods can be overloaded – Supports keyword parameters and variable size
argument lists
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 11
1.1 Fortress in a Nutshell (6)
• Components – Programs are organized into components – Exports and Imports APIs and can be linked together – Component “Shape” described by APIs by specifying
types in traits, objects and functions – External references are to APIs imported by component
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 12
1.1 Fortress in a Nutshell (7)
• Parallelism – Fortress supports a rich set of operations for defining
parallel execution and distribution of large data structures
– “For loops” are parallel by default
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 13
Chapter 2 Overview 2.1 The Fortress Programming Environment 2.2 Exports, Imports, and Linking Components 2.3 Automatic Generation of APIs 2.4 Rendering 2.5 Some Common Types in Fortress 2.6 Functions in Fortress 2.7 Some Common Expressions in Fortress 2.8 For Loops Are Parallel by Default 2.9 Atomic Expressions
2.10 Dimensions and Units 2.11 Aggregate Expressions 2.12 Comprehensions 2.13 Summations and Products 2.14 Tests and Properties 2.15 Objects and Traits 2.16 Features for Library Development
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 14
2.1 The Fortress Programming Environment (1)
• Fortress is platform independent • A typical programming model:
– Source code stored in files organized in directories
– A text-based shell for • Store environment variables • Issue commands to execute and compile programs
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 15
2.1 The Fortress Programming Environment (2)
• Running a Program as a script – Program stored in a file with suffix “.fsx”
– Program executed directly from a shell by calling “fortress script” command
HelloWorld.fsx
fortress script HelloWorld.fsx
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 16
2.1 The Fortress Programming Environment (3)
• Running a Program as a compiled code – Program stored in a file with suffix “.fss” – Program compiled into one or more components stored in a
database fortress
– Compile program by “fortress compile” command
– Execute program by “fortress run” command
HelloWorld.fss
fortress compile HelloWorld.fss
fortress run HelloWorld
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 17
2.1 The Fortress Programming Environment (4)
• Manipulating fortress – Components are stored in fortress by compilation – New component shadows the old one with the same
name in fortress – Components are removed from fortress by “fortress
remove” command
fortress
HelloWorld.fss compile
HelloWorld run
remove
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 18
2.2 Exports, Imports, and Linking Components (1)
• Exporting API – Components can include export statements
• Export statements list APIs that a components implement
• APIs are themselves program constructs
Components implements Executable
API
varargs parameter
void
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 19
2.2 Exports, Imports, and Linking Components (2)
• API – Defined in files with “.fsi”
– Compiled with “fortress compile” command
– API compilation does not shadow existing elements of a fortress
• error signaled if the same name exists. – API removed after all components referring to the API
have been removed with “fortress removeAPI” command
Blarf.fsi
fortress compile Blarf.fsi
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 20
2.2 Exports, Imports, and Linking Components (3)
• Exporting API – Component exporting API must provide definition for every
construct declared in that API
Component API
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 21
2.2 Exports, Imports, and Linking Components (4)
• Importing API (1) – Component importing API can use any constructs declared in
that API
export
import
qualified name
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 22
2.2 Exports, Imports, and Linking Components (5)
• Qualified Name – Import statement “import S from A” makes all names in set S
imported from API A – Imported names can be referred to as unqualified names in
that component
Set of names
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 23
2.2 Exports, Imports, and Linking Components (6)
• Component Reference – No component refers directly to another component – All external references go through APIs
• Component – Executable components
• contain no import statements • export the “API Executable”
– Executable components are compiled and executed as stand-alone
– Non-executable components must be compiled and linked with other components to form a new compound component
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 24
2.2 Exports, Imports, and Linking Components (7)
• Linking Components
Gary
Executable component
Execute with “fortress run Gary” command
Ralph.fss Blargh.fss
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 25
2.2 Exports, Imports, and Linking Components (8)
• Components are encapsulated – Compound components contain their own copies of
constituent components in the resident fortress • Compound components are upgradable
– With new components that export some of the APIs used by theirconstituents
New version compiled
Old version shadowed
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 26
2.3 Automatic Generation of API
• Components and APIs exist in separate namespaces – Component may have same name as API
Zeepf.fsi
component Zeepf export Zeepf foo(s)=() baz(s)=s end
Zeepf.fss
automatic genaration
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 27
2.4 Rendering (1)
• ASCII representation is rendered as …
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 28
2.4 Rendering (2)
• ASCII names for Unicode characters
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 29
2.5 Some Common Types in Fortress
• Standard type – String – Boolean – Numerics
(RR in ASCII) 64-bit precision float 32-bit precision float 64-bit integer
32-bit integer
Infinite precision integer
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 30
2.6 Functions in Fortress (1)
• Allows (mutually) recursive function definitions
Juxtaposition for multiplication
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 31
2.6 Functions in Fortress (2)
• 2.6.1 Juxtaposition and function application – Juxtaposed exprs of numeric type represent
multiplication • n factorial(n-1)
– Juxtaposition of expr of function type and another expr represents function application
• sin x
– Juxtaposition of expr of string type represents concatenation
• “Hi,” “ it’s” “ me” “ again.”
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 32
2.6 Functions in Fortress (3)
• 2.6.2 Keyword Parameters
Calling brings red=0 and blue=0
default value
throw expression chained boolean operator
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 33
2.6 Functions in Fortress (4)
• 2.6.3 Varargs Parameters – Functions with variable number of arguments allowed
varargs parameter
void
testing the number of arguments
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 34
2.6 Functions in Fortress (5)
• 2.6.4 Function Overloading – Functions can be overloaded by parameter types – Overloaded calls resolved based on runtime types of
arguments
Overloaded function
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 35
2.6 Functions in Fortress (6)
• 2.6.5 Function Contracts
require contract
ensure contract
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 36
2.7 Some Common Expressions in Fortress
while expression block
tuple expression
tuple elements evaluated in parallel
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 37
2.8 For Loops Are Parallel by Default
range expression
evaluated in parallel
Result may be 5 4 6 3 7 2 9 10 1 8
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 38
2.9 Atomic Expressions (1)
• Atomic expression is executed in … – All other threads observe that
• The computation has completed, or • The computation has not begun
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 39
2.9 Atomic Expressions (2)
This block observes that
both x and y have been updated, or
neither has
Possible values are 0 and 2
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 40
2.10 Dimensions and Units (1)
• Numeric types can be annotated with physical units and dimensions – Unit symbols are encoded with trailing underscores and
rendered in roman font
encoded as kg_ and rendered in
roman font
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 41
2.10 Dimensions and Units (2)
• Longhand and shorthand names provided – m, meter, and meters
• Synonymous unit names provided – N is synonymous with kg m/s2 – Force is synonymous with Mass Acceleration – Acceleration is synonymous with Velocity/Time
• Measurements in the same unit can be … – compared, added, subtracted, multiplied, divided
• Measurements in different units can be … – multiplied, divided
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 42
2.10 Dimensions and Units (3) Correct
static error
static error
unit conversion
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 43
2.11 Aggregate Expressions (1)
• Support for writing down collections by enumerating elements – Tuple, array, matrix, vector, map, set, list
• Elements are evaluated in parallel
arrays
Separate rows
by newlines or “;”
3rd dimensional slices separated by double “;”s
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 44
2.11 Aggregate Expressions (2)
• Vector written down like one-dim array, Matrix written down like two-dim array
• Array aggregate expr evaluates to array, vector, or matrix from context – Elements of vectors and matrices must be numbers
Extra parentheses required
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 45
2.11 Aggregate Expressions (3)
• Set written down by … – Elements in braces, separated by commas
• List written down by … – Elements in angle brackets (written in ASCII
as <|, |>)
• Map written down by … – Elements in braces with key/value pairs
joined by arrow (written in ASCII as |->)
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 46
2.12 Comprehensions (1)
• Describe elements of collection by providing a rule
How elements x are to be generated
Elements for every valid x
Multiple generators
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 47
2.12 Comprehensions (2)
• Comprehension can contain filtering expressions • Comprehension expression exists for aggregate
except tuple
Filtering expression
List comprehension
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 48
2.12 Comprehensions (3)
• Array comprehension – Element expr includes a tuple indexing the
elements of the array
[ 0 0 0 0 0 0 0 0 0 ]
Range expr 0..2 can be used
[ 1 0 0 0 1 0 0 0 1 ]
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 49
2.13 Summations and Products
• Syntactic support for “Big” operations
ASCII encoding
is written SUM in ASCII
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 50
2.14 Tests and Properties
• Support for automated program testing
• Property declaration for documenting conditions expected – No explicit finite collections – Property expected to hold all values of the type
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 51
2.15 Objects and Traits (1)
• Defining new types as well as objects belonging to types
• Multiple inheritance hierarchy rooted at trait Object
concrete
method
abstract method
s
Inherits all methods in these traits
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 52
2.15 Objects and Traits (2)
• Trait declarations can be extended by … – trait declaration – object declaration
• Singleton declaration declares a stand- alone singleton object
• Constructor declaration declares an object constructor
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 53
2.15 Objects and Traits (3)
• Singleton declaration – Object must provide concrete definitions for all
abstract methods it inherits
Definitions for abstract methods in
Moving
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 54
2.15 Objects and Traits (4)
• Fields can be declared in object definition • For every field …
– Implicit getter is defined – If a field includes modifier settable, implicit setter is
defined
getter expr assignment
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 55
2.15 Objects and Traits (5)
• Every method declared in an object or trait includes an implicit self parameter – self denotes the receiver of the method
self param provided explicitly self params appear in
nonstandard positions
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 56
2.15 Objects and Traits (6)
• Constructor declaration declares an object constructor – Declaration includes value param in header
• Every call to the constructor yields a new object
concrete definitions
Yields an object
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 57
2.15 Objects and Traits (7)
• Implicit getters and setters can be overridden
Print a message
and return
the field value
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 58
2.15 Objects and Traits (8)
• 2.15.1 Traits, Getters, and Setters – Traits do not include field declarations – Traits can include getter and setter
declarations
getter decl
getter notation can be used for variable v of type Moving
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 59
2.15 Objects and Traits (9)
• Getters can be declared using field declaration syntax
• Getter declaration can include modifiers allowed on field declaration – settable is used for implicit setter
Field decl syntax
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 60
2.16 Features for Library Development (1)
• Fortress designed to be a good language for library programming
• 2.16.1 Generic Types and Static Parameters – Allow types to be parametric such as Arrays and
Vectors – Programmer can define new traits, objects,
functions that include static parameters
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 61
2.16 Features for Library Development (2)
• 2.16.2 Specification of Locality and Data Distribution – Express programmer intent through data
structure distribution
• 2.16.3 Operator Overloading
Postfix operator for factorial
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 62
2.16 Features for Library Development (3)
• 2.16.4 Definition of New Syntax – Provides a facility for defining new syntax in
libraries
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 63
Example
Buffon’s needle: Estimates pi using Monte Carlo simulation
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 64
Example
Buffon’s needle (Rendered version)
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 65
Chapter 3 Programs (1)
• Program consists of Unicode 5.0 characters – May be rendered as subscripts, superscripts,
italicized, …
• Program is valid if it satisfies all static conditions – Only valid programs can be executed – Validity must be checked before execution
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 66
Chapter 3 Programs (2)
• Executing valid program consists of evaluating expressions – Evaluation may modify program state yielding
result – Result is a value, or an abrupt completion
• Characters of a valid program determine a sequence of input elements
• Input elements determine program constructs
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 67
Chapter 3 Programs (3)
• Program constructs may contain other program constructs – declaration and expression
• Semantics explained as … – Structure of input elements and program
constructs with static constraints – How outcome of program execution is
determined from sequence of constructs
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 68
Chapter 3 Programs (4)
• Programs are developed, compiled, and deployed as encapsulated upgradable components (Chapter 2.2)
• Fortress is … – block-structured
• Program consists of nested blocks of code • Entire program is a single block
– expression-oriented • “statements” are expression with type ( )
– whitespace-sensitive
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 69
Chapter 4 Evaluation (1)
• State of executing program consists of a set of threads and a memory
• Communication with outside world through input and output actions
• Program execution consists of evaluating … in parallel – Body expression of run function – Initial-value expression of top-level variables
and singleton object fields
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 70
Chapter 4 Evaluation (2)
• Threads evaluate expressions by taking steps – Step may complete the evaluation
• No more steps possible, or • May result in an intermediate expression
• Dynamic program order: partial order among expressions – See Chapter 13
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 71
Chapter 4 Evaluation (3)
• Intermediate exprs are generalizations of Fortress exprs – Some cannot be written in programs
• Expr is dynamically contained within another expr – All steps for the first are taken between the
beginning and completion of the second
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 72
4.1 Values (1)
• Value is the result of normal completion of an expr
• Value has … – type – environment – finite set of fields
• Every value is an object – value object – reference object – function
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 73
4.1 Values (2)
• Type specifies … – Names and types of its fields – Which names must be bound in its
environment – Methods of the object
• Only trait types have methods other than those inherited from type Any
• Fields … – In value object, each field is a value – In reference object, each field is a location – Functions and the value () have no fields
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 74
4.1 Values (3)
• Field has a name, which may be an identifier or an index – Only values of type LinearSequence or Heapsequence
have fields named by indices (Sections 40.1 & 40.3)
• Field in value object is immutable • Reference objects may have both mutable and
immutable fields
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 75
4.1 Values (4)
• Values are constructed by … – Top-level function declaration and singleton declaration – Evaluating …
• Object expression • Function expression • Local function declaration • Call to object constructor • Literal • Spawn expression • Aggregate expression • Comprehension
with constructed value as the result of normal completion of evaluation
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 76
4.2 Normal and Abrupt Completion of Evaluation (1)
• Expression is evaluated until it completes • Evaluation may …
– Complete normally resulting in a value, or – Complete abruptly
• Abrupt completion has an associate value – Exception value thrown and uncaught – Exit value of an exit expression
• Exception … – Programmer-defined; thrown by a throw expression – Predefined exception; thrown by Fortress standard
libraries, e.g., DivideByZeroException
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 77
4.2 Normal and Abrupt Completion of Evaluation (2)
• On abrupt completion… – Control passes to dynamically immediately
enclosing expression – Until it is handled either by …
• try expression if exception is being thrown, or • label expression if exit expr was evaluated
• If abrupt completion is not handled within a thread, thread itself completes abruptly
• If the main thread completes abruptly, program completes abruptly
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 78
4.3 Memory and Memory Operations (1)
• Memory: a set of abstract locations – Used to model sharing and mutation
• Location has an associated type and contains a value of that type – Type of value is a subtype of type of location
• Location can have non-object trait types; Value always has an object type
• Operations performed on memory … – Allocation – Read – Write
Memory behavior described in Chapter 21
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 79
4.3 Memory and Memory Operations (2)
• Allocation creates a new location of a given type
• Allocation occurs when … – A mutable variable is declared – A reference object is constructed
• A new location allocated for each field
• Locations are never reclaimed – In practice, reclaimed by garbage collection
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 80
4.3 Memory and Memory Operations (3)
• Allocated location afresh is uninitialized • Fortress guarantees …
– An initializing write performed if it is ever read – Initializing write occurs before any read
• Any location whose value … – can be written after initialization is mutable – cannot be written after initialization is immutable
• Mutable locations include – mutable variables – settable fields of a reference object
• Immutable locations include – Non-transient, non-settable fields
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 81
4.4 Threads and Parallelism (1)
• Two kinds of threads in Fortress … – Implicit threads – Spawned (explicit) threads
• Objects created by spawn construct
• Thread may be in one of five states – Not started – Executing – Suspended – Normally completed – Abruptly completed
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 82
4.4 Threads and Parallelism (2)
• Every thread has a body and an execution environment – Body is an intermediate expression – Thread evaluates it in the context of
execution environment – Both the body and the environment may
change when the thread takes a step
• Execution environment is used to look up names in scope – Environment of newly created thread is that of
the thread that created the new thread
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 83
4.4 Threads and Parallelism (3) - Implicit thread • A number of Fortress constructs are
implicitly parallel – An implicitly parallel construct creates a
group of implicit threads
• Implicitly parallel constructs … – Tuple expressions
• Each element evaluated in a separate implicit thread
– also do blocks • Each sub-block evaluated in a separate implicit
thread
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 84
4.4 Threads and Parallelism (4) - Implicit thread
• Implicitly parallel constructs … (Cont.) – Method invocations and function calls
• Receiver/function and each argument evaluated in a separate implicit thread
– for loops, comprehensions, sums, generated expressions, and big operators
• Parallelism in loops specified by generators • Generators other than sequential generator
execute each iteration in a separate implicit thread
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 85
4.4 Threads and Parallelism (5) - Implicit thread
• Implicitly parallel constructs … (Cont.) – Extremum expressions
• Each guarding expression evaluated in a separate implicit thread
– Tests • Each test evaluated in a separate implicit thread
Extremum expression
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 86
4.4 Threads and Parallelism (6) - Implicit thread • Implicit threads run “fork-join” style
– All threads in a group created together and must complete before the group completes
– Programmer cannot single out implicit thread and operate upon it
– Implicit threads need not be scheduled fairly
May loop forever
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 87
4.4 Threads and Parallelism (7) - Implicit thread
• If any implicit thread completes abruptly, the group completes abruptly – Result of the group is the result of constituent thread
that completes abruptly – Reduction variables should not be accessed after
abrupt completion(Section 4.4.1)
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 88
4.4 Threads and Parallelism (8) - Spawned thread • Spawned thread objects are reference objects
of …
• This trait has methods … – val returns value computed by spawn
• Invocation of val may block until thread completes
– wait waits for thread completion without return value – ready returns true if thread completes, false otherwise – stop attempts to terminate thread (Sec 32.6)
στατιχ τψπε of expr spawned
Type of thread object
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 89
4.4 Threads and Parallelism (9) - Spawned thread
• Spawned thread has been observed to complete after invoking val or wait methods, or when ready invocation returns true
• In case of resource shortage, attempt made to run subexpression of spawn before continuing – The rest evaluated after the parallel block spawned off
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 90
4.4 Threads and Parallelism (10)
• Thread can be suspended in … – Thread that creates thread group is suspended
until that group has completed – Thread that invokes val or wait is suspended
until the spawned thread completes – Invoking abort function within atomic
expression may cause thread to suspend
• Threads can perform operations simultaneously on shared objects – atomic expression synchronizes data access
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 91
4.4.1 Reduction Variables (1)
• Special treatment to reductions for loops • Reduction operator for type T is an
operator on T generating a monoid – The operator is an associative infix on T
Reduction operator
T is a subtype of Monoid
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 92
4.4.1 Reduction Variables (2)
• Reduction variable for thread group – Is of the form var op=expr using reduction
operator or its group inverse – Value not read otherwise in the thread group – Variable is not a free variable of a functional
Reduction variable
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 93
4.4.1 Reduction Variables (3)
• Other threads simultaneously reference a reduction variable see an arbitrary value
• Updates by those threads may be lost • Association of terms in the reduction guided by
loop generators (Sec 32.8) • Fortress libraries declare common math operator
to be monoid
Monoid operators
also allowed for
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 94
4.4.1 Reduction Variables (4)
• Implementation of reduction – Reduction var is assigned identity of reduction
operator at beginning of iteration – When all iteration are complete, initial value
and value of implicit thread are reduced and assigned to reduction var
Reduction variable
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 95
4.4.1 Reduction Variables (5)
• Parallel slack : (available work)/(number of threads)
– Slack in hundreds or more proves beneficial with support for lightweight threading
– Very slack computations easily adapt to differences in the number of processors
– Slack is a desirable property
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 96
4.5 Environments (1)
• Environments maps names to values or locations – Environment is immutable
• Program starts with with empty environment • Environments extended with mappings by
– Variable/function/object declarations – Function calls
• After initializing top-level variables and singleton objects, top-level environment is constructed
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 97
4.5 Environments (2)
• Environment of value determined by how it is constructed – For object/function expr and local function decl, env of
the constructed value is the lexical env in which expr/decl was evaluated
– For others, env of the constructed value is top-level env of component in which expr/decl occurs
• Env of spawned thread for body is distinct from env of associated thread object in which calls to thread method are evaluated
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 98
4.6 Input and Output Actions
• Certain functionals (functions or methods) perform primitive input/output actions
• Any functional which may perform I/O action directly/indirectly must be declared with io modifier
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 99
Chapter 5 Lexical Structure
• Program consists of Unicode 5.0 characters – Every character is part of an input element – Partitioning of char sequence into input
elements determined uniquely
• Standard ways to render (display) input elements described
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 100
5.1 Characters (1) Code point
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 101
5.1 Characters (2)
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 102
5.1 Characters (3)
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 103
5.1 Characters (4)
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 104
5.1 Characters (5)
• Forbidden and Restricted Characters – No control characters allowed except
whitespace characters and SUBSTITUTE (U+001A, “control-Z”)
– Control characters cause static error if appear outside comment
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 105
5.2 Words and Chunks
• Chunk is a nonempty contiguous subsequence of program
• Word is a maximal chunk consising only word characters – Letters, digits, connecting punctuation, prime
characters, apostrophe
• Restricted word is a maximal chunk with only restricted-word characters – ASCII letters, digits, underscore characters
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 106
5.3 Lines, Pages and Position
• Characters partitioned into lines and pages
• Line terminator – LINE FEED – CARRIAGE RETURN not immediately followed
by LINE FEED – LINE SEPARATOR – PARAGRAPH SEPARATOR
• Page terminator – FORM FEED
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 107
5.4 ASCII Conversion
• An equivalent program containing only ASCII characters exists for every Fortress program
• ASCII conversion in three steps – Pasting words across line breaks – Replacing restricted words, sequences of
operator/special characters with single Unicode characters
– Replacing apostrophes in numerals with digit-group separators
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 108
5.5 Input Elements and Scannning
• ASCII conversion is followed by scanning – Program partitioned into input elements
• Input elements … – Whitespace element (comments included) – Token
• Reserved word • Literal
– Boolean, character, string, void, numeral • Identifier • Operator token • Special token
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 109
5.6 Comments
• Opening and closing comment delimiters … (* , *)
• Characters between balanced comment delimiters comprise a comment – Comment delimiters may be included in
comments
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 110
5.7 Whitespace Elements • Maximal chunk consisting of …
– Comments – Whitespace characters not within string/
character literals, numerals – Ampersands not within string/character literals
• Line-breaking whitespace distinguished from non-line-breaking whitespace
• Static error if ampersand occurs unless … – Within character/string literal – Within comments – Immediately followed by line terminator or
line-terminating comment
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 111
5.8 Reserved Words
• Operators on units are also reserved – cubed, cubic, in, inverse, per, square, squared
• Reserved to avoid confusion (no special meaning in Fortress) – goto, idiom, public, pure, reciprocal, static
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 112
5.9 Character Literals
• Character literal consists of one or more characters enclosed in single quotation marks – ‘ … ’, ’… ’, ‘ … ’
• Enclosed characters may be … – Single character – Sequence of hexadecimal
digits for Unicode code point
– Official Unicode 5.0 name/alternative name
– ASCII characters converted to a Unicode character Character-literal escape sequence
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 113
5.10 String Literals
• String literal consists of sequence of characters enclosed in double quotation marks
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 114
5.11 Boolean Literals
• Boolean literals are false and true
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 115
5.12 The Void Literal
• Void literal is ( )
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 116
5.13 Numerals (1)
• Numeric literal ( numeral ) is a maximal chunk consisting one or more words satisfying … – Each word consists of only digits and letters
• The last word may have one underscore as part of a radix specifier
– Consecutive words separated by exactly one char, either a digit-group separator or ‘.’
– The first word begins with a digit or the last word has a radix specifier
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 117
5.13 Numerals (2)
• Radix specifier is a word suffix consisting of – An underscore followed by
• a sequence of one or more digits (interpreted in base 10), or
• English name in all uppercase ASCII letters of an integer from 2 to 16
Valid numerals
Invalid numerals
Both upper- and lowercase not allowed
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 118
5.14 Operator Tokens
• Operator word is … – Not reserved – Consists only of uppercase letters and underscores – Does not begin or end with underscore – Has at least two different letters
• Base operator is … – Ordinary operator character – Two-character sequence “**” – Sequence of two or more vertical-line char “|” – Multicharacter enclosing operator (Section 5.14.1)
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 119
5.15 Identifiers
• Word beginning with a letter and is not a reserved word, operator word, or all or part of a numeral
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 120
5.16 Special Tokens
• Every special character that is not part of a token
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 121
5.17 Rendering of Fortress Programs 5.17.1 Fonts
• Roman • Italic • Math • Script • Fraktur • Sans-serif • Italic sans-serif • Monospace • Double-struck
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 122
5.17.2 Numerals
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 123
5.17.3 Identifiers
• Complex rules for rendering identifiers …
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 124
Chapter 6 Declarations
• Declarations introduce named entities – Declaration declares an entity and a name – The declared name refers to the declared
entity
• Not a one-one correspondence between declarations and named entities
• Declaration may contain other decls – Trait decl may contain method decls – Function decl may contain parameter decls
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 125
6.1 Kinds of Declarations (1)
• Two kinds of declarations … – Top-level declaration – Local declaration
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 126
6.1 Kinds of Declarations (2)
• Top-level declarations … – Trait declarations (Chapter 9) – Object declarations (Chapter 10)
• singleton decl/ constructor decl
– Top-level variable declarations (Section 6.2) – Top-level function declarations (Chapter 12)/ top-level
operator declarations (Chapter 16) – Dimension declarations (Chapter 18) – Unit declarations (Chapter 18) – Top-level type aliases (Section 8.9) – Test declarations (Chapter 19) – Top-level property declarations (Chapter 19)
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 127
6.1 Kinds of Declarations (3)
• Local declarations occur in another declaration or in some expression … – Field declarations (Section 10.2)
• Occur in object decl and object expr • Include field decl in param list of constructor decl
– Method declarations (Section 9.2) • Occur in trait/object decl, object expr
– Coercion declarations (Chapter 17) • Occur in trait/object decl
– Local variable declarations (Section 6.3) • Occur in block expr
– Local function declarations (Section 6.4) • Occur in block expr
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 128
6.1 Kinds of Declarations (4) • Local declarations occur in another declaration
or in some expression … (cont’d) – Labeled blocks (Section 13.2) – Static-parameter declarations
• Declare type param, nat param, int param, bool param, dim param, unit param, opr param, ident param
• Occur in static-param lists of trait/object decl, top-level type aliases, top-level function decls, method decls
– Hidden-type-variable declarations • Occur in where clauses of trait/object decls, top-level
function decls, method decls – Type aliases in where clauses of trait/object decls, top-
level function decls, method decls – (Value) parameter declarations
• Occur in …
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 129
6.1 Kinds of Declarations (5)
• Some declarations are syntactic sugar for other decls – Apparent field decls in trait decls are method decls (Section
9.2) – Dimension/unit decl may desugar into several separate decls
(Section 35.3) – After desugaring, the kinds of decls listed are disjoint
• Implicitly declared names – self implicitly declared as param of dotted methods (Section
9.2) – result implicitly declared as variable for ensures clause of a
contract(Section 12.4)
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 130
6.1 Kinds of Declarations (6)
• Type declarations declare names that refer to types – Trait declarations – Object declarations – Top-level type aliases – Type-parameter declarations – Hidden-type-variable declarations
• Dimension declarations – Dimension declarations
– dim-parameter declarations • Unit declarations
– Unit declarations – unit-parameter declarations
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 131
6.1 Kinds of Declarations (7) • Functional declarations
– Constructor declarations – Top-level function declarations – Method declarations – Local function declarations
• Variable declarations – Singleton declarations – Top-level variable declarations – Field declarations – Local variable declarations; incl implicit decl of result – (value) parameter declarations; incl implicit decl of self
• Static-variable declarations – Static-parameter declarations – Hidden-type-variable declarations
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 132
6.1 Kinds of Declarations (8)
• Most declarations declare a single name given explicitly in the declaration
• One exception … – Wrapped field declarations (Section 9.3) in
object decl and object expr • Declare both the field name and names for methods
provided by the declared type of the field
• Method declarations in trait may be either abstract or concrete – Abstract decls do not have bodies – Concrete decls (called definitions) have bodies
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 133
6.2 Top-Level Variable Declarations (1)
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 134
6.2 Top-Level Variable Declarations (2)
Immutable variable
Mutable variable
Initial value
subtype of Type
Initial value option
al
for multiple declarations
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 135
6.2 Top-Level Variable Declarations (3)
Examples of variable declarations
Example of multiple variable declaration using tuple notation
Equivalent declaration
s
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 136
6.3 Local Variable Declarations (1)
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 137
6.3 Local Variable Declarations (2)
Variable decl without initial value
mutability
Type and definition are separated
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 138
6.4 Local Function Declarations
• Functions can be declared within block expressions – Via the same syntax as top-level func decls with
modifiers private and test excluded
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 139
6.5 Matrix Unpasting (1)
• Matrix unpasting is an extension of local variable declaration syntax as a shorthand for breaking a matrix into parts
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 140
6.5 Matrix Unpasting (2)
Cache-oblivious Matrix
Multiplication
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 141
6.5 Matrix Unpasting (3)
Bind the Upper left square matrix to squareShape
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 142
Chapter 7 Names
• Names used to refer to entities in Fortress program
• Names may be simple or qualified – Simple name: identifier or operator – Qualified name: API name followed by “.”
followed by an identifier – Operator cannot be qualified
• Simple names are introduced by declarations – Declaration may be implicit – Every declaration has a scope
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 143
7.1 Namespaces
• Fortress supports disjoint namespaces – Type namespace: Type declarations declare
names and … • Static-variable declarations • Dimension declarations
– Value namespace: Function and variable declarations declare names and …
• nat, int, bool, unit, opr, ident parameters
– Label namespace: names declared by labeled blocks
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 144
7.2 Reach and Scope of a Declaration (1)
• Reach of declaration – Reach of labeled block: the block itself – Reach of functional method declaration:
component containing that declaration – Reach of dotted method declaration in trait T:
declaration of T and any trait or object decl/expr that extends T
– Reach of other declaration: the smallest block strictly containing that declaration
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 145
7.2 Reach and Scope of a Declaration (2)
• If two declarations with overlapping reaches declare the same name in the same namespace, and the declarations are not overloaded, then one declaration shadows the other for that name in that namespace
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 146
7.2 Reach and Scope of a Declaration (3) • Name is in scope in a namespace within the
reach of any declaration that declares that name unless one of the conditions holds … – Declaration is shadowed at the program point for the
name in that namespace – Declaration is a type alias, a dimension declaration, or
unit declaration; program point is in the declaration – Declaration is a field, local variable or parameter
declaration; program point is in the declaration or lexically precedes the declaration
– Declaration is a parameter declaration of an object declaration; program point is in the body of a method declaration of that object declaration
– Declaration is a labeled block; program point is in a spawn expression in the labeled block
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 147
7.3 Qualified Names
• Fortress provides a component system – Entities declared in a component are
described by an API
• Component imports APIs – Allows to refer to entities declared by the
imported APIs – In some cases, references to these entities
must be qualified by the API name
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 148
Chapter 8 Types • Fortress provides …
– Trait types – Tuple types – Arrow types – BottomType – Other types provided in libraries
• Some types … – Have names – May be parameterized by types and values (generic types)
• Types are identical iff … – They are the same kind – Their names and arguments (if any) are identical
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 149
8.1 Relationships between Types (1) • Types may be related by …
– Subtyping relation – Exclusion relation – Coercion
• Subtyping relation is … – Reflexive, transitive, and antisymmetric – Defined by extends clause of trait and object decl and
object expr
T is a subtype of U
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 150
8.1 Relationships between Types (2)
• Every expr has a static type • Every value has a runtime type (dynamic type) • Programs checked before executed to …
– ensure the runtime type of the value is a subtype of the static type of the expr
• Fortress defines an exclusion relation between types which relates two disjoint types – No value can have a type that is a subtype of two types that
exclude each other
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 151
8.1 Relationships between Types (2)
• Exclusion relation is … – Irreflexive and symmetric – Defined by excludes and comprises clauses of trait
declarations – Implied from these by subtyping relation
S and T excludes each other
T excludes U All subtypes
excluded as well
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 152
8.1 Relationships between Types (3)
• Coercion between types – Coercion from T to U is defined in declaration of U
U defines a coercion from T
T can be coerced to U
• Fortress type hierarchy is acyclic wrt subtyping and coercion except … – There exists a bidirectional coercion between two tuple types
iff they have the same sorted form
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 153
8.2 Trait Types
• Traits are declared by trait declarations (Chapter 9)
• Trait has a trait type of the same name
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 154
8.3 Object Trait Types
• Named objects are declared by object declarations (Chapter 10) – Named object has an object
trait type of the same name
• Anonymous objects are declared by object expressions (Section 13.9) – Anonymous object has an
anonymous object trait type
• Object trait type is a special kind of trait type
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 155
8.4 Tuple Types (1)
• Tuple is an ordered sequence of keyword-value pairs (Section 13.27)
• Tuple type consists of a parenthesized comma-separated list of … – A plain type T – A vararg type T… – A keyword-type pair identifier=T
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 156
8.4 Tuple Types (2)
• Element type in tuple type corresponds to one in tuple type iff … – Both are plain types in the same position – Both are vararg types, or – Both are keyword-type pairs with the same keyword
• Every tuple type is a subtype of Tuple – Tuple types are not subtypes of Object – Tuple types cannot be extended by other trait types
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 157
8.4 Tuple Types (3)
• Tuple types are covariant; tuple type X is a subtype of tuple type Y iff … – Correspondence between their element types is
bijective – For each element type in X, the type in the element
type is a subtype in the corresponding element type in Y
– Keyword-type pairs in X and Y appear in the same order
• Sorted form X’ for tuple type X – Created by reordering keyword-type pairs – There is a coercion from tuple type X to tuple
type Y iff X and Y have the same sorted form
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 158
8.4 Tuple Types (4)
• Tuple type excludes any nontuple type other than Any
• Two tuple types exclude each other unless the correspondence between their element type is bijective – Two tuple types with bijective correspondence exclude
each other if either any type of one excludes the type of the other, or their keyword-type pair do not appear in the same order
• Intersection of nonexclusive tuple types are defined elementwise
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 159
8.5 Arrow Types (1)
• Arrow types: types of function values – Functions can be passed as arguments and
returned as values
• Every arrow type is a subtype of Object • Arrow types are not trait types
– Arrow types cannot be extended by other trait types
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 160
8.5 Arrow Types (2)
• Parameter types are contravariant; return types are covariant
Examples of arrow types Optional throw clause
is a subtype of iff • D is a subtype of A and • B is a subtype of E and • For all X in C, there exists Y in F such that X is a subtype of
Y
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 161
8.6 Bottom Type
• Fortress provides a special BottomType • No value in Fortress has the bottom type
– throw and exit expressions have the bottom type
• Bottom type is a subtype of every type • Intersection of any exclusive types is the
bottom type
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 162
8.7 Types in the Fortress Standard Libraries (1)
• Fortress standard libraries define simple standard types for literals (Section 13.1) – BooleanLiteral[b] – () (pronounced “void”) – Character – String – Numeral[n,m,r,v] – Several simple numeric types
• Simple standard types for literals are mutually exclusive
• Values of these types are immutable
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 163
• Numeric types share the common supertype Number – Arbitrary-precision integers Z – Unsigned arbitrary-precision integers N – Rational numbers Q – Fixed-size representation for integers
• Z8, Z16, Z32, Z64, Z128
– Fixed-size representation for unsigned integers • N8, N16, N32, N64, N128
– Floating-point numbers – Intervals Interval[X]
• X can be instantiated with any number type
– Imaginary and complex numbers in … • rectangular form Cn (n=16, 32, 64, 128, 256) • Polar form Polar[X] (X is instantiated with any real number
type)
8.7 Types in the Fortress Standard Libraries (2)
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 164
8.7 Types in the Fortress Standard Libraries (3)
• Floating-point numbers – R32, R64 to be 32 and 64-bit IEEE754 floating-
point numbers
• Two functions on types … – Double[F] is a floating-point type twice the
size of floating-point type F – Extended[F] is a floating-point type sufficiently
larger than floating-point type F to perform summations
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 165
8.7 Types in the Fortress Standard Libraries (4)
• Fortress standard libraries also define … – Any – Object – Exception – Boolean – BooleanInterval – LenearSequence – HeapSequence – BinaryWord – …
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 166
8.8 Intersection and Union Types (1)
• Intersection type of a set of types S is a subtype of every set T in S and of the intersection of every subset of S
• Union type of a set of types S is a supertype of every set T in S and of the union of every subset of S
• Neither intersection nor union are first-class types – Used solely for type inferences (Chapter 20) – Cannot be expressed directly in programs
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 167
8.8 Intersection and Union Types (2)
• Intersection of a set of types S is equal to a named type U when any subtype of T in S and of the intersection of every subset of S is a subtype of U
• Union of a set of types S is equal to a named type U when any supertype of T in S and of the union of every superset of S is a subtype of U
Any subtype of S and T must be a
subtype of V; V is an
intersection of S and T
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 168
8.8 Intersection and Union Types (3)
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 169
8.8 Intersection and Union Types (4)
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 170
8.9 Type Aliases
• Fortress allows names to serve as aliases for more complex type instantiations – All use of type aliases are expanded before type
checking – Type aliases do not define new types nor nominal
equivalence relations among types
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 171
Chapter 9 Traits
• Traits are declared by trait declaration • Traits define new named types • Trait specifies a collection of methods • Trait can extend others
– Trait inherits the methods from those traits – Type defined by that trait is a subtype of traits
it extends
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 172
9.1 Trait Declarations (1)
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 173
9.1 Trait Declarations (2)
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 174
9.1 Trait Declarations (3)
• Trait declaration: [modifier] trait trait_name static_params [extended traits] [excluded traits] [comprises on the trait] [ where clause] {abstract_fields, getter_methods, setter_ methods} method_declarations end
• extends, excludes, comprises {trait_references} – If clause contains only one trait, { } may be elided
• comprises clause may include “…” • Trait_references in comprises clause is a declared trait
identifier or an abbreviated type for aggregate expressions
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 175
9.1 Trait Declarations (4)
• Every trait extends the trait Object • extends clause every trait listed in its
clause – If T extends U, T is a subtrait of U; U is a
supertrait of T – Extension is transitive; if T extends U it also
extends all supertraits of U – Extension relation is the smallest relation
satisfying transitivity – Relation must form acyclic hierarchy rooted
at trait Object
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 176
9.1 Trait Declarations (5)
• Trait T strictly extends U iff T extends U and T is not U
• Trait T immediately extends U iff T strictly extends U and there is no trait V s.t. T strictly extends V and V strictly extends U – U is an immediate supertrait of T – T is an immediate subtrait of U
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 177
9.1 Trait Declarations (6)
• Trait with excludes clause excludes every trait listed in its clause – If T excludes U, T and U are mutually exclusive – No third trait can extend them both and neither can
extend the other
• If trait decl of T includes comprises clause – If comprises clause of T does not include “…”, the trait
must not be extended with immediate subtraits other than those listed in its comprises clause
– If comprises clause of T includes “…”, any subtrait of T is not exposed by API
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 178
9.1 Trait Declarations (7)
Return type ()
self explicitly declared as a param
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 179
9.1 Trait Declarations (8)
Molecule can be Immediately
extended by
OrganicMolecule or
InorganicMolecule OrganicMolecule and
InorganicMolecule May be exclusive
Parallelism Oblivious Programming 2008-5-2 The Fortress Language Specification 180
9.2 Method Declarations (1)