a multi stage monadic metalanguage
play

A Multi-stage Monadic Metalanguage Eugenio Moggi - PowerPoint PPT Presentation

A Multi-stage Monadic Metalanguage Eugenio Moggi moggi@disi.unige.it DISI, Univ. of Genova (joint work with S.Fagorzi) FOSSACS03 p.1/8 Overview: Motivations Why MSP (Multi-stage programming)? 1) Staging (Partial Evaluation) is an


  1. A Multi-stage Monadic Metalanguage Eugenio Moggi moggi@disi.unige.it DISI, Univ. of Genova (joint work with S.Fagorzi) FOSSACS’03 – p.1/8

  2. Overview: Motivations Why MSP (Multi-stage programming)? 1) Staging (Partial Evaluation) is an optimization technique usable in many areas: exploit info at early stages to generate customized code for later stages. 2) GC (Global Computing) applications operate with incomplete information about a dynamically changing environment, thus they need to interleave meta-programming activities (code generation, assembly of components) normal computational activities (IO, modification of local store,. . . ) security checks (at administrative domain boundaries) MSP supports interleaving of code generation and normal computation. FOSSACS’03 – p.2/8

  3. Overview: Aims MMML (Multi-stage Monadic MetaLanguage) provides reference semantics describing the subtle interactions between staging (code generation) and computational effects. MMML adopts simple type system (fails to statically detect all run-time errors): no best option for a static type system [TD99,CMS??,Nan02,NT03], there are cases when dynamic error detection is preferable [FMP02]. FOSSACS’03 – p.2/8

  4. Overview: Aims and Approach MMML (Multi-stage Monadic MetaLanguage) provides reference semantics describing the subtle interactions between staging (code generation) and computational effects. MMML adopts simple type system (fails to statically detect all run-time errors): no best option for a static type system [TD99,CMS??,Nan02,NT03], there are cases when dynamic error detection is preferable [FMP02]. computational types Mτ : explicit distinction between values and computations code type � τ � (as in MetaML ): reflective nature of languages for MSP annotated term constructors: operationally relevant information explicit in terms (as in multi-level languages for binding-time analysis) terms à la CRS (Combinatory Reduction Systems): for syntactic uniformity ChAM-like (Chemical Abstract Machine) approach: transparent simplification > (like ChAM heating) – PURITY programmable computation > (like ChAM reduction) FOSSACS’03 – p.2/8

  5. Technical Part on MMML Article stresses also that the addition of staging to pre-existing monadic metalanguage is MODULAR! FOSSACS’03 – p.3/8

  6. MMML : Syntax τ ∈ T : : = X | Mτ | ref τ | τ 1 → τ 2 | � τ � type e ∈ E : : = x | l | c ([ x i ] e i | i ∈ m ) term, x ∈ X variable, l ∈ L location c ∈ C : : = ret | do | new | get | set | λ | @ | up | dn | c V | c M term constructor Syntax of terms à la CRS [Klo80] λ -abstraction λx.e becomes λ ([ x ] e ) application e 1 e 2 becomes @( e 1 , e 2 ) Haskell’s do notation do { x ← e 1 ; e 2 } becomes do ( e 1 , [ x ] e 2 ) Alternative BNF for c ∈ C op ∈ Op : : = ret | do | new | get | set | λ | @ | up | dn ordinary term constructor c ∈ C : : = op B annotated term constructor B ∈ { V, M } ∗ Warning: in multi-level languages (e.g. in λ M of [GJ95,Dav96]) c V and c M are identified, i.e. op B becomes op n where n is the length of the sequence B . FOSSACS’03 – p.4/8

  7. MMML : Naive Type System τ ∈ T : : = X | Mτ | ref τ | τ 1 → τ 2 | � τ � type e ∈ E : : = x | l | c ([ x i ] e i | i ∈ m ) term, x ∈ X variable, l ∈ L location c ∈ C : : = ret | do | new | get | set | λ | @ | up | dn | c V | c M term constructor standard Γ ⊢ Σ e : τ typing judgment, where Γ type assignment x : τ to variables { Γ , x i : τ i ⊢ Σ e i : τ i | i ∈ m } x : τ ∈ Γ c : ( τ i ⇒ τ i | i ∈ m ) ⇒ τ x c Γ ⊢ Σ x : τ Γ ⊢ Σ c ([ x i ] e i | i ∈ m ): τ each term constructor c has a type schema ( τ i ⇒ τ i | i ∈ m ) ⇒ τ FOSSACS’03 – p.4/8

  8. MMML : Type Schema for term constructors τ ∈ T : : = X | Mτ | ref τ | τ 1 → τ 2 | � τ � type e ∈ E : : = x | l | c ([ x i ] e i | i ∈ m ) term, x ∈ X variable, l ∈ L location c ∈ C : : = ret | do | new | get | set | λ | @ | up | dn | c V | c M term constructor ret : τ ⇒ Mτ return value , do : Mτ 1 , ( τ 1 ⇒ Mτ 2 ) ⇒ Mτ 2 sequential execution new : τ ⇒ M ( ref τ ) , get : ref τ ⇒ Mτ , set : ref τ, τ ⇒ M ( ref τ ) store operations λ : ( τ 1 ⇒ τ 2 ) ⇒ ( τ 1 → τ 2 ) abstraction , @: ( τ 1 → τ 2 ) , τ 1 ⇒ τ 2 application up : τ ⇒� τ � cross stage persistence , dn : � τ �⇒ Mτ compilation If c : ( τ i ⇒ τ i | i ∈ m ) ⇒ τ , then c V : ( � τ i �⇒� τ i � | i ∈ m ) ⇒� τ � code value constructor c M : ( � τ i �⇒ M � τ i � | i ∈ m ) ⇒ M � τ � code generation where � τ i | i ∈ m � stands for ( � τ i � | i ∈ m ) . For instance λ V : ( � τ 1 �⇒� τ 2 � ) ⇒� τ 1 → τ 2 � code for abstraction λ M : ( � τ 1 �⇒ M � τ 2 � ) ⇒ M � τ 1 → τ 2 � generate code for abstraction FOSSACS’03 – p.4/8

  9. MMML : Type Schema for term constructors τ ∈ T : : = X | Mτ | ref τ | τ 1 → τ 2 | � τ � type e ∈ E : : = x | l | c ([ x i ] e i | i ∈ m ) term, x ∈ X variable, l ∈ L location c ∈ C : : = ret | do | new | get | set | λ | @ | up | dn | c V | c M term constructor up : τ ⇒� τ � cross stage persistence (binary inclusion) dn : � τ �⇒ Mτ compilation (may raise unresolved link error) If λ : ( τ 1 ⇒ τ 2 ) ⇒ ( τ 1 → τ 2 ) , then λ V : ( � τ 1 �⇒� τ 2 � ) ⇒� τ 1 → τ 2 � code for abstraction λ M : ( � τ 1 �⇒ M � τ 2 � ) ⇒ M � τ 1 → τ 2 � generate code for abstraction FOSSACS’03 – p.4/8

  10. Operational Semantics: ChAM-like approach > Id ′ simplification is β -reduction @( λ ([ x ] e 2 ) , e 1 ) > e 2 [ x : = e 1 ] Id > Id ′ | done | err computation, where Id Id ≡ ( X | µ, e, E ) ∈ Conf program configuration (closed system) X ∈ P fin ( X ) set of names generated so far fin ∆ µ ∈ S = L → E current store e program fragment under consideration E ∈ EC : : = ✷ | do ( E, e ) | c M ( v, [ x ] E, f ) evaluation context for e where f : : = [ x ] e abstraction and v : : = [ x ] ret ( e ) value abstraction FOSSACS’03 – p.5/8

  11. Operational Semantics: Properties > Id ′ simplification is β -reduction @( λ ([ x ] e 2 ) , e 1 ) > e 2 [ x : = e 1 ] Id > Id ′ | done | err computation, where Id Id ≡ ( X | µ, e, E ) ∈ Conf program configuration (closed system) CR > is confluent, and induced equivalence = is a congruence Id 1 > D 1 computation is preserved by further simplification, i.e. ∗ ∗ ∨ ∨ Id 2 > D 2 ⇒ Id ′ implies Id ′ well-formed SR (Subject Reduction) Id well-formed and Id = = = ∆ PR (Progress) Id well-formed implies Id = = = ⇒ , where = = = ⇒ = > ∪ > FOSSACS’03 – p.5/8

  12. Operational Semantics: Computation Rules I > Id ′ simplification is β -reduction @( λ ([ x ] e 2 ) , e 1 ) > e 2 [ x : = e 1 ] Id > Id ′ | done | err computation, where Id Id ≡ ( X | µ, e, E ) ∈ Conf program configuration (closed system) Administrative Steps A.0 ( X | µ, ret ( e ) , ✷ ) > done A.1 ( X | µ, do ( e 1 , [ x ] e 2 ) , E ) > ( X | µ, e 1 , E [ do ( ✷ , [ x ] e 2 )]) A.2 ( X | µ, ret ( e 1 ) , E [ do ( ✷ , [ x ] e 2 )]) > ( X | µ, e 2 [ x : = e 1 ] , E ) Imperative Steps I.1 ( X | µ, new ( e ) , E ) > ( X | µ { l : e } , ret ( l ) , E ) where l �∈ dom ( µ ) I.2 ( X | µ, get ( l ) , E ) > ( X | µ, ret ( e ) , E ) provided e = µ ( l ) I.3 ( X | µ, set ( l, e ) , E ) > ( X | µ { l = e } , ret ( l ) , E ) provided l ∈ dom ( µ ) FOSSACS’03 – p.5/8

  13. Operational Semantics: Computation Rules II > Id ′ simplification is β -reduction @( λ ([ x ] e 2 ) , e 1 ) > e 2 [ x : = e 1 ] Id > Id ′ | done | err computation, where Id Id ≡ ( X | µ, e, E ) ∈ Conf program configuration (closed system) Code Generation Steps (only unary case λ M ) G.1 ( X | µ, λ M ([ x ] e ) , E ) > ( X, x | µ, e, E [ λ M ([ x ] ✷ )]) rename x to avoid clashes with X G.2 ( X | µ, ret ( e ) , E [ λ M ([ x ] ✷ )]) > ( X | µ, ret ( λ V ([ x ] e )) , E ) occurrences of x in e are re-captured FOSSACS’03 – p.5/8

  14. Operational Semantics: Computation Rules II > Id ′ simplification is β -reduction @( λ ([ x ] e 2 ) , e 1 ) > e 2 [ x : = e 1 ] Id > Id ′ | done | err computation, where Id Id ≡ ( X | µ, e, E ) ∈ Conf program configuration (closed system) Compilation and Run-time Errors � ( X | µ, ret ( e ) , E ) if e = vc ↓ C.1 ( X | µ, dn ( vc ) , E ) > if vc ↓ undefined err where vc ∈ VC : : = x | up ( e ) | c V ([ x i ] vc i | i ∈ m ) code value and Demotion vc ↓ maps code value vc : � τ � to represented term e : τ x ↓ undefined up ( e ) ↓ = e (base case, like x ) c V ([ x i ] vc i | i ∈ m ) ↓ = c ([ x i ] e i | i ∈ m ) provided e i = vc i [ x i : = up ( x i )] ↓ for i ∈ m FOSSACS’03 – p.5/8

  15. An important Lemma If ( X | µ, e, E ) > ( X ′ | µ ′ , e ′ , E ′ ) and FV( µ, e ) ∪ CV( E ) ⊆ X and FV( E ) ⊆ X − CV( E ) then X ⊆ X ′ , dom ( µ ) ⊆ dom ( µ ′ ) and FV( µ ′ , e ′ ) ∪ CV( E ′ ) ⊆ X ′ and FV( E ′ ) ⊆ X ′ − CV( E ′ ) where FV( _ ) set of free variables, CV( _ ) set of captured variables, e.g. ∆ FV( c M ( v, [ x ] E, f )) = FV( v, f ) ∪ (FV( E ) − x ) ∆ = x ⊎ CV( E ) CV( c M ( v, [ x ] E, f )) FOSSACS’03 – p.6/8

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend