Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk...

26
Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Transcript of Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk...

Page 1: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Outlines

Compiling Interaction Nets

Abubakar.Hassan at kcl.ac.uk

Department of Computer ScienceKings College London

April 4, 2006

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 2: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Outlines Interaction Nets Programming Language

Outline

1 Interaction Nets Programming LanguageBackgroundLanguage SyntaxForeign Language InterfaceModule System

2 Compilation of Interaction netsAbstract MachineCompilation schemes

3 Conclusions and Future work

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 3: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Programming Language

Part I

Interaction Nets Programming Language

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 4: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Programming Language

BackgroundLanguage SyntaxForeign Language InterfaceModule System

Interaction nets system

Define a set Σ of Agents

����α

?

@ �· · ·x1 xn

y = α(x1, . . . , xn)

and a set R of Rules

����α ����

β-�@

@

......

x1

xn

ym

y1

=⇒ N...

...x1

xn

ym

y1

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 5: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Programming Language

BackgroundLanguage SyntaxForeign Language InterfaceModule System

Example

Σ = {Z, S , +}:

����Z6

����S6

����+

@@��

x = Z() y = S(a) w = +(d , e)

R =

����Z

����+

����S

����+

����+

����S6

�� @

� @

��

� @@

=⇒ =⇒

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 6: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Programming Language

BackgroundLanguage SyntaxForeign Language InterfaceModule System

a = Add(x , y), a = Z =⇒ x = ya = Add(x , y), a = S(z) =⇒ x = S(b), z = Add(b, y)

Replace equals for equals:

Z = Add(x , x) =⇒S(Add(b, y)) = Add(S(b), y) =⇒

we replace the ‘=’ by ‘><’ and remove =⇒ if empty.

Z >< Add(x , x)S(Add(b, y)) >< Add(S(b), y)

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 7: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Programming Language

BackgroundLanguage SyntaxForeign Language InterfaceModule System

Net with active pair and Reduction

����S

����Z6

����Z6

����+

@@��

� ����S

I −→ ����Z

����Z6

����+

����S6

@@��

� ����S

I −→

����Z6

����S6

����S6

After replacing equals for equals:

S(Z) = Add(z ,S(Z)) −→ Z = Add(S ,S(Z))−→ S(S(Z))

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 8: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Programming Language

BackgroundLanguage SyntaxForeign Language InterfaceModule System

Language Syntax

We can see interaction nets as a configurationc = (Σ, 〈~t | u1 = v1, . . . un = vn〉,R)

P ::= (agent | rule)∗ netagent ::= agentName : arityrule ::= [ruleName: ] term >< term ( => net | )net ::= [netName: ] equation (, equation)∗ |equation ::= term = termterm ::= agentName(term, ..., term) | var

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 9: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Programming Language

BackgroundLanguage SyntaxForeign Language InterfaceModule System

Example

Z:0 S:1Add:2AddZ: Z >< Add(x,x)AddS: S(Add(b,y)) >< Add(S(b),y)S(Z) = Add(b,S(Z))

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 10: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Programming Language

BackgroundLanguage SyntaxForeign Language InterfaceModule System

Foreign Language Interface

P ::= (agent | rule)∗ netagent ::= agentName : arityrule ::= [ruleName: ] term >< term ( => net | )net ::= [netName: ] equation (, equation)∗ |equation ::= term = termterm ::= agentName{`}(term, ..., term)

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 11: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Programming Language

BackgroundLanguage SyntaxForeign Language InterfaceModule System

Example

fact:1 num:0 ans:0num{int n} >< fact(ans{Factorial.fact(n)})fact(x) = num{8}

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 12: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Programming Language

BackgroundLanguage SyntaxForeign Language InterfaceModule System

Module System

P ::= module { [import modName[. compName]](agent | rule)∗ net }

agent ::= [*]agentName : arityrule ::= [[*]ruleName: ] term >< term ( => net | )net ::= [[*]netName: ] equation (, equation)∗ |equation ::= term = termterm ::= [*]agentName{`}(term, ..., term)

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 13: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Programming Language

BackgroundLanguage SyntaxForeign Language InterfaceModule System

Example

module add {import arith.Zimport arith.S*Add:2*AddZ: Z >< Add(x,x)*AddS: S(Add(b,y)) >< Add(S(b),y)S(Z) = Add(b,S(Z)) }

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 14: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Compiler

Part II

Compilation of Interaction nets

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 15: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

CompilerAbstract Machineschemes

Compilation

Compiling a program p.net gives a linkable p.l and a p.sym. Welink p.l to get a loadable p.o which is ready for execution by theabstract machine.Stack based machine that executes the bcodes.

bcodes ::= S | A | MS ::= loadc | store | load | popA ::= mkAgent | mkVar | mkNet | mkRuleM ::= getVar | connectPorts | eval

A state of the machine is a configuration:

〈PC , ρ, S ,A,R〉

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 16: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

CompilerAbstract Machineschemes

Compilation schemes

A program may consist of a list of modules M1, ...Mn. Thecompilation scheme Cp compiles each module using the schemeCm. This in turn compiles each of the component

c = (Σ, 〈~t | u1 = v1, . . . un = vn〉,R)

in the module by using the appropriate scheme:

Cp = CmJM1K, ..., CmJMnK

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 17: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

CompilerAbstract Machineschemes

The compilation of a module generates the following code:

CmJ(Σ, 〈~t | u1 = v1, . . . un = vn〉,R)K =

ruleName :CRJr1K...ruleName :CRJrnKnetName :CNJu1 = v1K...netName :CNJun = vnKinterface :CT JtK

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 18: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

CompilerAbstract Machineschemes

Example

The code generated by compiling:module Erase{Eps:0EpsEpsRule: Eps >< EpsEpsNet: Eps = Eps}is:EpsEpsRule:loadc epsloadc 1mkAgentstore 0load 0store 1loadc epsloadc 1mkAgentstore 0load 0store 0

loadc 2load 0load 1mkRulepopEpsNet:loadc epsloadc 1mkAgentstore 0load 0store 1loadc eps

loadc 1mkAgentstore 0load 0store 0load 0load 1mkNetevalpop

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 19: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

CompilerAbstract Machineschemes

Execution starts from the initial configuration:

P ` 〈0, [], [], [], []〉

The machine starts executing the instructions under the labelEpsEpsRule. After interpreting all the code in this label, weobtain the configuration:

p ` 〈16, [], [], [], [Eps,Eps]〉

The configuration obtained after executing the code in the labelEpsNet, before the instruction eval:

p ` 〈32, [], [], [Eps,Eps], [Eps,Eps]〉

The final configuration obtained after the eval instruction:

p ` 〈33, [], [0 7→ ε], [], [Eps,Eps]〉

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 20: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Conclusions and Future work

Future work

We have seen for the first time a compilation scheme forInteraction nets.

extend the core language

study alternative compilation schemes

compilation for parallel hardware

optimisations

type system

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 21: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Conclusions and Future work

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 22: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Conclusions and Future work

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 23: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Conclusions and Future work

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 24: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Conclusions and Future work

Compilation Schemes

Cv JvK =

loadc vmkVarstore−i

CtJα(t1 · · · tn)K

CaJαKstore−iCaJt1Kstore−i + 1load iloadc 1load i + 1

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 25: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Conclusions and Future work

CnJα(u1, ..., un) = β(v1, ..., vn)K =

CtJα(u1 · · · un)KCtJβ(v1 · · · vn)KmkNetevalpop

Cr Jα(t1, ..., tn) >< β(u1, ..., un)K =

CtJα(t1 · · · tn)KCtJβ(u1 · · · un)KmkRulepop

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets

Page 26: Compiling Interaction Nets · Outlines Compiling Interaction Nets Abubakar.Hassan at kcl.ac.uk Department of Computer Science Kings College London April 4, 2006 Abubakar.Hassan at

Conclusions and Future work

CaJαK =

loadc αloadc armkAgentstore−iloadc 0connectPortsstore−i...CaJtnK

store−i + 1load iloadc nload i + 1loadc 0connectPortsstore−i

Abubakar.Hassan at kcl.ac.uk Compiling Interaction Nets