53
: Forth A New Hope Interactive Systems Programming in the Age of the Pocket Supercomputer ;

Forth: A New Hope

Embed Size (px)

DESCRIPTION

Slide Deck from SVFiG January 2014 meeting.Discusses principles and design patterns for a new Forth implementation. Survey: Unix integration, annotative inferred typing, and a modular package management system.

Citation preview

Page 1: Forth: A New Hope

: Forth A New Hope

Interactive Systems Programming in the Age of the Pocket Supercomputer

;

Page 2: Forth: A New Hope

Forth is the Future

Page 3: Forth: A New Hope

If we write it

Page 4: Forth: A New Hope

A Problem Oriented Language

Page 5: Forth: A New Hope

A New Set of

Problems

Page 6: Forth: A New Hope

Convergence:

• (FL)ops / W

• Heterogenous Architectures

• Dynamic / JIT Optimization

Page 7: Forth: A New Hope

Forth has two problems…

Page 8: Forth: A New Hope

Cache Invalidation

Page 9: Forth: A New Hope

Naming Things

Page 10: Forth: A New Hope
Page 11: Forth: A New Hope
Page 12: Forth: A New Hope

Validation

• Lack of static typing a strength

• Lack of any typing whatsoever?

• This is a real problem for the user

• Compilers don’t care for it either

Page 13: Forth: A New Hope

Naming

• Words are phenomenally powerful

• Wordlists / Vocabularies are okay…

• This is our highest abstraction at ANS level

• Kiss of Death

Page 14: Forth: A New Hope

These Problems Compound!

Page 15: Forth: A New Hope

Validation Naming *

• No way to convey type information

• Collections of words cannot be handled as a unit

• The result is idiosyncratic code, and no libraries

• Every Forth program is one book

• Every Forth group has their own dialect

Page 16: Forth: A New Hope

Smaller Problemsforth grew up poor.

let’s waste some bytes.

Page 17: Forth: A New Hope

Too Small

• ANS model impoverished by necessity

• Existing implementations miserly with memory

• Forth exists for all 8-16 bit systems

• 32 and 64 are the only numbers that matter

Page 18: Forth: A New Hope

Too Literal

• Anonymous values are hard

— This relates to lack of typing

• Introspection is brittle across versions

• Poor concept of surrounding environment

• Monotonic memory model means missing modularity

Page 19: Forth: A New Hope

Not Literal Enough

• ‘cell’ varies by a factor of 4

• Words for absolute widths are nonstandard

• Floats are a mess

• Unsophisticated I/O model

— These are library problems

• Without modularity, everything is a keyword

Page 20: Forth: A New Hope

A New FIGForth

Page 21: Forth: A New Hope

Implementation Precedes Standards

Page 22: Forth: A New Hope

We Need Forths( we have forths )

Page 23: Forth: A New Hope

One Forth at a Time

Page 24: Forth: A New Hope

A Consular Forth

• Unix Native

• 32 and 64 bit literal

• Full Introspection

• Dynamic Optimization

• Modularity

• Annotative, Inferred Type System

Page 25: Forth: A New Hope

Unix Native

• Hence, written in C

• Uses a file system

• Knows: pipes, ports, files, terminals

• Is good friends with shell and python

• “batteries included elsewhere”

• Provide a rich experience, focus on Forth

Page 26: Forth: A New Hope

Unix Native

• Source with image caching

• Late-binding libraries

• Integrated package management

• Interactive ‘Forth Console’:

— shell / editor / umbilical terminal

Page 27: Forth: A New Hope

32 - 64 Bit Literal

• What it says on the label

• Floats on data stack

• Consistent words for doubles and longs

• Gforth supports 128 bit fixed point: useless

• Counted strings have a cell of count

• Compatible means 32 and 64, period

Page 28: Forth: A New Hope

Full Introspection

• Indirect threading model

• All optimization JITted, in principle

• Compilation is information preserving

• Clean return stack ( stack, rack, bag )

• Full anonymity incl. create/does

Page 29: Forth: A New Hope

Dynamic Optimization

• Information preserving threading model

• Even easy wins done lazily (constant folding, peepholes, inlining)

• Hotspot detection, JITting, etc: faster when it matters the most

• Type information would be helpful here!

Page 30: Forth: A New Hope

Modularity

• Wordlist == Vocabulary

• Books, Shelves, Library

• ISBN number, author, name

• word.:book or book .: word

• Runtime handles loading and freeing

• “Codex”, “Ledger”, “Journal”, “Core”

Page 31: Forth: A New Hope
Page 32: Forth: A New Hope

ISBN?• Just a UUID, SHA-1, etc.

• Version numbering considered harmful:

— a bug-fix for 0.3 when HEAD is at 0.5

— same ‘version’ for several environments

• Put user info in the docstring

• Computers prefer unique numbers

• Peer to peer package management!

Page 33: Forth: A New Hope

URLesque:book:author.de-facto-unique.string/bookname#5c2b3ab0-91b2-11e3-

baa8-0800200c9a66

Page 34: Forth: A New Hope

Forthright

book: bookname

author: author.de-facto.unique.string

UABN: 5c2b3ab0-91b2-11e3-baa8-0800200c9a66

require” ./file.forth” \ or on search path

load: bookname as: nickname \ etc.

Page 35: Forth: A New Hope

And Of Course

Page 36: Forth: A New Hope

Types!

Page 37: Forth: A New Hope

Type is not defined. At all.

• Tag All The Things!

• One Syntax To Rule Them All

• Quacking Ducks and Unit Tests

• Turing Complete Logic Layer

• “Objects”. So Many Objects.

Page 38: Forth: A New Hope

Does Forth Need Types?Does a fish need a bicycle?

Page 39: Forth: A New Hope

Contrapositive:

If Forth did not need types, we would not fake them, like this:

!: new-word ( buf off — buf+ off- flag )

etc ;

Page 40: Forth: A New Hope

Fabri

• Assert type and stack effect of all core words

• Infer type of new words on this basis

• Assert what cannot be inferred

• Write your assertion, then write your word = unit test

• “Static”, no effect on running code

Page 41: Forth: A New Hope

Fabri

• “Opt out”: can be removed at runtime

• Or, may be retained and used explicitly

• Warning-only: If Forth compiles it, Fabri compiles it

• Integrated with the JIT: case -> dispatch table etc.

• Hyperstatic and forward only (supports SSA)

Page 42: Forth: A New Hope

Fabri

• Formalizes existing practice

• You can trust the stack diagram!

• Types introduced as needed

• Extensible, interactive

• Override through assertion

• Basic component of runtime for full effect

Page 43: Forth: A New Hope

Ok, But…

Why?

Page 44: Forth: A New Hope

Meet the Epiphany IV• 64 cores @ ~800 MHz

• 32 kiB local memory per core, addressable by all

• 102 GFLOPS peak performance (floats ~10% of logic)

• Flat shared-memory 32 bit RAM model

• Network on chip with absolute routing: two cores may communicate without intermediate action

• 2 Watts peak consumption

• Technology scales to 1024, 4096 cores

Page 45: Forth: A New Hope

Chuck Moore’s Law: joules / op

Page 46: Forth: A New Hope

Epiphany IV

• http://www.adapteva.com/epiphanyiv/

• Programmable in C

• JVM, Ruby, Python: Dead. Toast. Finished.

• Lua is a maybe

• Forth is a leading contender

• GreenArray on steroids

Page 47: Forth: A New Hope

Convergence:

• Interactive console

• Package management

• Type system, testing, continuous integration

• Umbilical development

• Dominate the new paradigm: thousands of heterogenous cores and partially-shared memory

Page 48: Forth: A New Hope

Three Projects

• FigForth “Teen Edition”

—Ficus runtime environment

—Banyan package system

• Fabri: Type engine

• Forge: Interactive Console

Page 49: Forth: A New Hope

BSD Licensed

Page 50: Forth: A New Hope

BSD Licensed

• GPL contaminates image files

• Stealing a compiler is pointless

• “Linking” is a nebulous, harmful concept

• Embedded, Mobile, Microcontrol, “real problems”

• Maximum adoption and industry support

• Presumption of goodwill

Page 51: Forth: A New Hope

So Far

• Er… well I’ve written a bit of a console in gforth

• Registered forth.io

• Pforth exists in 32 and 64 bit editions

• Needs some love and attention

• Fabri has several rounds of hammock time remaining

• Actual development closely tied to Ficus

Page 52: Forth: A New Hope

Discussion to continue on

SVFiG list…

Page 53: Forth: A New Hope