Concept Programming The Art of Turning Ideas into Code Christophe - - PowerPoint PPT Presentation

concept programming
SMART_READER_LITE
LIVE PREVIEW

Concept Programming The Art of Turning Ideas into Code Christophe - - PowerPoint PPT Presentation

TM Concept Programming The Art of Turning Ideas into Code Christophe de Dinechin, christophe@dinechin.org Problem statement Dealing with Ever Increasing Software Complexity Exponential Growth Complexity Software complexity follows


slide-1
SLIDE 1

Concept Programming

The Art of Turning Ideas into Code

Christophe de Dinechin,

christophe@dinechin.org

TM

slide-2
SLIDE 2

Problem statement

Dealing with Ever Increasing Software Complexity

slide-3
SLIDE 3

Exponential Growth

Software complexity follows Moore’s law Driven by customers, not by programmers Programmers brains can’t keep up Result: periodic paradigm shifts... ... obsoleting all the legacy

Time Complexity

Primary Use:

slide-4
SLIDE 4

Exponential Growth

Software complexity follows Moore’s law Driven by customers, not by programmers Programmers brains can’t keep up Result: periodic paradigm shifts... ... obsoleting all the legacy

Time Complexity Fortran, Basic Symbols and Expressions Commercial

Primary Use:

slide-5
SLIDE 5

Exponential Growth

Software complexity follows Moore’s law Driven by customers, not by programmers Programmers brains can’t keep up Result: periodic paradigm shifts... ... obsoleting all the legacy

Time Complexity Fortran, Basic Symbols and Expressions Commercial Pascal, C Structured programing Personal

Primary Use:

slide-6
SLIDE 6

Exponential Growth

Software complexity follows Moore’s law Driven by customers, not by programmers Programmers brains can’t keep up Result: periodic paradigm shifts... ... obsoleting all the legacy

Time Complexity Fortran, Basic Symbols and Expressions Commercial Pascal, C Structured programing Personal C++ Objects Graphical

Primary Use:

slide-7
SLIDE 7

Exponential Growth

Software complexity follows Moore’s law Driven by customers, not by programmers Programmers brains can’t keep up Result: periodic paradigm shifts... ... obsoleting all the legacy

Time Complexity Fortran, Basic Symbols and Expressions Commercial Pascal, C Structured programing Personal C++ Objects Graphical Java Multiple Machines Distributed

Primary Use:

slide-8
SLIDE 8

Exponential Growth

Software complexity follows Moore’s law Driven by customers, not by programmers Programmers brains can’t keep up Result: periodic paradigm shifts... ... obsoleting all the legacy

Time Complexity Fortran, Basic Symbols and Expressions Commercial Pascal, C Structured programing Personal C++ Objects Graphical Java Multiple Machines Distributed Python, XML Prebuilt components Commodity

Primary Use:

slide-9
SLIDE 9

Exponential Growth

Software complexity follows Moore’s law Driven by customers, not by programmers Programmers brains can’t keep up Result: periodic paradigm shifts... ... obsoleting all the legacy

Time Complexity Fortran, Basic Symbols and Expressions Commercial Pascal, C Structured programing Personal C++ Objects Graphical Java Multiple Machines Distributed Python, XML Prebuilt components Commodity

Tools P r

  • b

l e m s

Primary Use:

slide-10
SLIDE 10

Exponential Growth

Software complexity follows Moore’s law Driven by customers, not by programmers Programmers brains can’t keep up Result: periodic paradigm shifts... ... obsoleting all the legacy

Time Complexity Fortran, Basic Symbols and Expressions Commercial Pascal, C Structured programing Personal C++ Objects Graphical Java Multiple Machines Distributed Python, XML Prebuilt components Commodity

Tools P r

  • b

l e m s

Comfortable Cheap Fast Slow Tedious Expensive

Primary Use:

slide-11
SLIDE 11

Exponential Growth

Software complexity follows Moore’s law Driven by customers, not by programmers Programmers brains can’t keep up Result: periodic paradigm shifts... ... obsoleting all the legacy

Time Complexity Fortran, Basic Symbols and Expressions Commercial Pascal, C Structured programing Personal C++ Objects Graphical Java Multiple Machines Distributed Python, XML Prebuilt components Commodity

Tools P r

  • b

l e m s

Comfortable Cheap Fast Slow Tedious Expensive

You Are Here

Primary Use:

slide-12
SLIDE 12

Staying Ahead of Moore’s Law

Time Complexity Fortran, Basic Symbols and Expressions Commercial Pascal, C Structured programing Personal C++ Objects Graphical Java Multiple Machines Distributed Python, XML Prebuilt components Commodity

Can we integrate new paradigms incrementally?

YES

Can we select the best representation independently for any given concept?

YES

slide-13
SLIDE 13

Staying Ahead of Moore’s Law

Time Complexity Fortran, Basic Symbols and Expressions Commercial Pascal, C Structured programing Personal C++ Objects Graphical Java Multiple Machines Distributed Python, XML Prebuilt components Commodity

Can we integrate new paradigms incrementally?

YES

Can we select the best representation independently for any given concept?

YES

XL Concept programming

slide-14
SLIDE 14

Software Complexity

Scale Complexity

Millions of Objects, Billions of Bits

Domain Complexity

Ever Needed “X-Ray Spectrography for Dummies?”

Artificial Complexity

C++ Standard: >700 pages, highly technical

Business Complexity

Deliver this Yesterday, No Budget

slide-15
SLIDE 15

The Belief in the Best Paradigm

“Everything is an object”

In Smalltalk, 2+3*5=25, not 17 Object 2 gets message + with arg 3

“Everything is a function”

Functional languages: Lisp, OCaml But the computer doesn’t think that way ... and neither do many of us☺

slide-16
SLIDE 16

A Simple Example

How Can We Can Get Stuck so Easily?

slide-17
SLIDE 17

Computing a Maximum

Mathematical Definition is Well Known

Compares elements with an order relation Max(a1, a2, ..., an)

Not Exactly a New Problem in Computing That Ought to be Easy!

slide-18
SLIDE 18

Maximum in C

Generally Defined as a Macro

Something like: #define max(x,y) ((x) < (y) ? (y) : (x)) Or maybe: #define max(x,y) ((x) >= (y) ? (x) : (y))

Some interesting questions

Why all the Parentheses? What About Side Effects in max(f(a++),c--)? What about max(x,y,z,t)?

slide-19
SLIDE 19

Maximum in C

Generally Defined as a Macro

Something like: #define max(x,y) ((x) < (y) ? (y) : (x)) Or maybe: #define max(x,y) ((x) >= (y) ? (x) : (y))

Some interesting questions

Why all the Parentheses? What About Side Effects in max(f(a++),c--)? What about max(x,y,z,t)?

Failed!

slide-20
SLIDE 20

Maximum in Java (using functions)

Defined in java.lang.Math as overloaded functions

You get max(int,int), max(long, long), ...

We got rid of side effects!

But what about max(x,y,z,t)? What about max("Hello", "World")? What about max(1, 2.5)?

slide-21
SLIDE 21

Maximum in Java (using functions)

Defined in java.lang.Math as overloaded functions

You get max(int,int), max(long, long), ...

We got rid of side effects!

But what about max(x,y,z,t)? What about max("Hello", "World")? What about max(1, 2.5)?

Failed!

slide-22
SLIDE 22

Maximum in Java (using Objects)

Defined in java.util.Collections as generic function

When Java looks up to C++, you get: public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)

Hey, we can now compare more than 2 things!

But why can't we write max(x,y,z,t)? Why should we create a collection to start with? Why e1.compareTo(e2)<0 and not e1 < e2? Throws ClassCastException or NoSuchElementException

slide-23
SLIDE 23

Maximum in Java (using Objects)

Defined in java.util.Collections as generic function

When Java looks up to C++, you get: public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)

Hey, we can now compare more than 2 things!

But why can't we write max(x,y,z,t)? Why should we create a collection to start with? Why e1.compareTo(e2)<0 and not e1 < e2? Throws ClassCastException or NoSuchElementException

Failed!

slide-24
SLIDE 24

Maximum in Lisp or Scheme

Defined as variadic function

Scheme: (define (max . a) (if (null? a) (error) (max-list a))

Much closer to an acceptable definition

Syntax is Natural for Lisp: (max 1 2 3 5) Still fails at run-time in same cases as Java

slide-25
SLIDE 25

Maximum in Lisp or Scheme

Defined as variadic function

Scheme: (define (max . a) (if (null? a) (error) (max-list a))

Much closer to an acceptable definition

Syntax is Natural for Lisp: (max 1 2 3 5) Still fails at run-time in same cases as Java

Failed!

slide-26
SLIDE 26

Why Can't We Get It Right?

That Ought to be Easy! But it's Hard

That simple problem is not solved after 30+ years

There is a gap between:

Concepts, in your head Representations of concepts, in the code

Concept Programming is all about this gap

slide-27
SLIDE 27

General Ideas

Applying Concept Programming

slide-28
SLIDE 28

What is Concept Programming?

Code represents concepts

Reality: Shape, File, Credit, Shotgun Organization: Function, Visitor, Aspect Focus on concepts relevant to the program

Make the code “look like” the concept

Similarity in structure, behavior, locality Principle of least surprise

slide-29
SLIDE 29

Domains

Concept and Code live in separate domains

Concepts: Environment, Organization, Algorithms, Pictures Code: Source, Object, Data, Instructions, Bitmaps

Unlike objects or functions, you won’t find “concepts” in the code, only concept representations

Concept Code

slide-30
SLIDE 30

Bridging the Gap

Turning Concepts into Code is a lossy conversion

This is true with any language, any paradigm No two people have exactly the same concept in mind

Minimizing the loss remains a worthy goal

Concept Code

slide-31
SLIDE 31

What is a “Concept”?

An entity in the problem space...

Cars, Error Messages, Connections An object is only one possible representation

... that is relevant to the code space

What will it be used for? How do we represent it? Relevant here, irrelevant there

The set of concepts is not constrained

slide-32
SLIDE 32

Minority Paradigms

The set of concepts is infinite...

Special concepts can make life easier

Minority paradigms to fill the void

Logic programming, design by contract

To each its (incompatible) language!

Prolog, Eiffel

Not minor in usefulness

But the majority can't use them

slide-33
SLIDE 33

Limitations of the Tools

Many notations are difficult to add

Symbolic differentiation GUI Elements Debug-only code

We Need a Concept Programming Language

But a lot can be done without

slide-34
SLIDE 34

Pseudo Metrics

Identifying Non-Obvious Problems in the Code

slide-35
SLIDE 35

Pseudo-metrics

Syntactic Noise

Form that doesn’t map to the problem space

Semantic Noise

Meaning that doesn’t map to the problem space

Bandwidth

How much of the problem space is covered?

Signal/Noise Ratio

How much code actually deals with real problems?

slide-36
SLIDE 36

Pseudo-metrics

Syntactic Noise

Form that doesn’t map to the problem space Useless and potentially distracting visual clutter C: if (a == 3) { printf("Hello\n"); } C++: list<list<int> > l; // Watch that space! HTML: When N &lt; 0, N is said to be negative

slide-37
SLIDE 37

Pseudo-metrics

Semantic Noise

Meaning that doesn’t map to the problem space Unexpected behavior compared to “native” concept C: if (x = 0) y = max(f(), x); Zeroes x, calls f twice C++: object.GetBounds(&rect); Exposes two addresses Smalltalk: 2+3*5 25 instead of 17

slide-38
SLIDE 38

Pseudo-metrics

Bandwidth

How much of the problem space is covered? Conditions reuse in different cases C: int max(int x, int y); vs. macro C++: cout << complex(2.3, 5.2); vs. printf Ada: accept Help (X : item) do... vs. pthreads

slide-39
SLIDE 39

Pseudo-metrics

Signal/Noise Ratio

How much code actually deals with real problems? The rest is mostly useless fluff... Java:

class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); } }

slide-40
SLIDE 40

Metrics: Keep in Mind

These are pseudo metrics

You can’t measure things in the problem space Highly subjective metrics You can’t write a tool to measure them

Analogy to Music

Reducing noise is a worthy goal... But you cannot completely eliminate it Noise to one, music to the other

slide-41
SLIDE 41

Abstraction

Fighting Complexity by Reducing it to Tiny Bits

slide-42
SLIDE 42

Abstractions

Code is a particular concept abstraction This abstraction is necessary

You can’t run ideas in a computer

But: Abstractions introduce distortions

What you think is not what you get Abstraction penalty, inefficiency in generated code

slide-43
SLIDE 43

Abstraction Loss: Concept Cast

Replacing a concept with a related one

Often to workaround limits of the tools Example: replace f(x,y,z,...) with f(list)

Too often an unconscious decision

It works!

Maybe the most frequent abstraction loss

You lose some semantic signal... ... while introducing a lot of noise

slide-44
SLIDE 44

Abstractions vs. Complexity

Domain: Equivalence, aka least surprise

Programmers read FILE and think “file”

Scale: Layering and reuse

FILE can be reused, e.g. to build DATABASE

Artificial: Hide irrelevant details

You can safely ignore all the OS magic behind FILE

Business: Manageability, predictability

FILE behavior is reliable, portable, documented

slide-45
SLIDE 45

Step by Step

Define the problem space Identify individual concepts Document concept behaviors & relations Choose notation for each concept Select or invent representation

slide-46
SLIDE 46

XL: An Extensible Language

Applying Concept Programming to Language Design

slide-47
SLIDE 47

Considering Metrics

Syntactic Noise

if A < 3 then IO.WriteLn "A=", A

Semantic Noise

to GetBounds(O : object; out R : rectangle)

Bandwidth

function Max(x: ordered; ...) return ordered X : integer := Max(1, 3, 7, 2, 4)

Signal/Noise Ratio

type complex with Re, Im : real

slide-48
SLIDE 48

Extensibility

Symbolic differentiation

Standard notation: XL notation: {differentiation} d/dx(sin(x+1/x))

Compiler plug-ins implement extensions

Plug-in code uses specific extensions: translation differentiation when (d/'dvar'('expr')) where BeginsWithD(dvar) then ...

d dxsin(x + 1 x)

slide-49
SLIDE 49

Extensibility benefits

Represent arbitrary concepts Favors “natural” notations in the code Unifies “user” and “built-in” entities Leaves the computer to do the grunt work

slide-50
SLIDE 50

XL Concept-inspired Features

Expression reduction True and validated generic types Type-safe variable argument lists Iterators and generators All used to build “standard” elements

slide-51
SLIDE 51

XL Concept-inspired Features

Expression reduction

Generalizes operator overloading Efficient matrix linear algebra

function MultiplyAdd(A, B, C : matrix) return matrix written A*B+C

Easy special cases

function IsIdentity(M : matrix) return boolean written M = 1

slide-52
SLIDE 52

XL Concept-inspired Features

True generic types

Make functions implicitly generic Array operations

function Add (A, B : array) return array written A+B

Pointer operations

function Peek(P : ptr) return ptr.item written *P to Poke(P : ptr; V : ptr.item) written *P := V

slide-53
SLIDE 53

XL Concept-inspired Features

Validated generic types

Specify interface of a generic type Type with an order operation

generic type ordered where A, B : ordered // Code testing the Test : boolean := A < B // candidate types

Makes generic code more robust

function Min (X : ordered) return ordered Z : complex := Min(Z) // Error (unlike C++)

slide-54
SLIDE 54

XL Concept-inspired Features

Type-safe variable argument lists

A user-defined Pascal-style WriteLn:

to WriteLn(...) is // ... stand for rest of args Write ... // Pass rest of args Write new_line

Min and max functions that work:

function Min(X : ordered; ...) return ordered is result := Min(...) if X < result then result := X

slide-55
SLIDE 55

XL Concept-inspired Features

Iterators and generators

Define iterator over a range of integers

iterator It(var out C : T; L,H: T) written C in L..H is C := L while C <= H loop yield C += 1

Used in for loops (and implements for loops)

for K in 3..5 loop WriteLn "K=", K

slide-56
SLIDE 56

Maximum in XL

generic type ordered where A, B : ordered Test : boolean := A < B function Max (X : ordered) return ordered is return X function Max (X : ordered; ...) return ordered is result := Max(...) if result < X then result := X

slide-57
SLIDE 57

Maximum in XL

generic type ordered where A, B : ordered Test : boolean := A < B function Max (X : ordered) return ordered is return X function Max (X : ordered; ...) return ordered is result := Max(...) if result < X then result := X

Pass

slide-58
SLIDE 58

Bridging the Gap: Done?

Turning Concepts into Code is a lossy conversion

This is true with any language, any paradigm No two people have exactly the same concept in mind

Minimizing the loss remains a worthy goal

XL does this better ☺

Concept Code

slide-59
SLIDE 59

Concept Programming

The Art of Turning Ideas into Code

ank y!

TM

Christophe de Dinechin,

christophe@dinechin.org