Resugaring: Lifting Evaluation Sequences through Syntactic Sugar Justin Pombrio, Shriram Krishnamurthi Brown University
Syntactic Sugar 2
Syntactic Sugar desugar x + 2 x . _ _ a d d _ _ ( 2 ) 3
Syntactic Sugar desugar x + 2 x . _ _ a d d _ _ ( 2 ) desugar [ x * x | x < - l s t ] m a p ( \ x - > x * x ) l s t 4
Syntactic Sugar desugar x + 2 x . _ _ a d d _ _ ( 2 ) desugar [ x * x | x < - l s t ] m a p ( \ x - > x * x ) l s t desugar x o r y l e t t = x i n i f t t h e n t e l s e y 5
Surface language (what you write) Syntactic Sugar desugar x + 2 x . _ _ a d d _ _ ( 2 ) desugar [ x * x | x < - l s t ] m a p ( \ x - > x * x ) l s t desugar x o r y l e t t = x i n i f t t h e n t e l s e y 6
Surface language Core language (what you write) (what runs) Syntactic Sugar desugar x + 2 x . _ _ a d d _ _ ( 2 ) desugar [ x * x | x < - l s t ] m a p ( \ x - > x * x ) l s t desugar x o r y l e t t = x i n i f t t h e n t e l s e y 7
Big surface language desugar Small core 8
Big surface language desugar Small core 9
Big surface language desugar Code analyzer Small Refactoring engine core Evaluator 10
Big surface language desugar Code analyzer Small Refactoring engine core Evaluator 11
Big surface language desugar Code analyzer Small Refactoring engine core Evaluator Evaluator 12
Surface Core n o t ( t r u e ) o r t r u e 13
Surface Core desugar n o t ( t r u e ) o r t r u e l e t t = n o t ( t r u e ) i n i f t t h e n t e l s e t r u e 14
Surface Core desugar n o t ( t r u e ) o r t r u e l e t t = n o t ( t r u e ) i n i f t t h e n t e l s e t r u e l e t t = f a l s e i n i f t t h e n t e l s e t r u e i f f a l s e t h e n f a l s e e l s e t r u e t r u e 15
Surface Core desugar n o t ( t r u e ) o r t r u e l e t t = n o t ( t r u e ) i n i f t t h e n t e l s e t r u e f a l s e o r t r u e l e t t = f a l s e i n i f t t h e n t e l s e t r u e i f f a l s e t h e n f a l s e e l s e t r u e t r u e t r u e 16
Confection core eval seq → surface eval seq 17
18
92 steps 19
92 steps 7 steps 20
Surface Core n o t ( t r u e ) o r t r u e Resugaring: Running sugar in reverse 21
Surface Core n o t ( t r u e ) o r t r u e f a l s e o r t r u e t r u e Resugaring: Running sugar in reverse 22
Surface Core desugar n o t ( t r u e ) o r t r u e l e t t = n o t ( t r u e ) i n i f t t h e n t e l s e t r u e f a l s e o r t r u e t r u e Resugaring: Running sugar in reverse 23
Surface Core desugar n o t ( t r u e ) o r t r u e l e t t = n o t ( t r u e ) i n i f t t h e n t e l s e t r u e f a l s e o r t r u e l e t t = f a l s e i n i f t t h e n t e l s e t r u e i f f a l s e t h e n f a l s e e l s e t r u e t r u e t r u e Resugaring: Running sugar in reverse 24
Surface Core desugar n o t ( t r u e ) o r t r u e l e t t = n o t ( t r u e ) i n resugar i f t t h e n t e l s e t r u e f a l s e o r t r u e l e t t = f a l s e i n i f t t h e n t e l s e t r u e i f f a l s e t h e n f a l s e e l s e t r u e t r u e t r u e Resugaring: Running sugar in reverse 25
Surface Core desugar n o t ( t r u e ) o r t r u e l e t t = n o t ( t r u e ) i n resugar i f t t h e n t e l s e t r u e resugar f a l s e o r t r u e l e t t = f a l s e i n i f t t h e n t e l s e t r u e i f f a l s e t h e n f a l s e e l s e t r u e t r u e t r u e Resugaring: Running sugar in reverse 26
Surface Core desugar n o t ( t r u e ) o r t r u e l e t t = n o t ( t r u e ) i n resugar i f t t h e n t e l s e t r u e resugar f a l s e o r t r u e l e t t = f a l s e i n i f t t h e n t e l s e t r u e resugar i f f a l s e t h e n f a l s e e l s e t r u e t r u e t r u e Resugaring: Running sugar in reverse 27
THREE KEY PROPERTIES OF RESUGARING 28
Surface Core desugar n o t ( t r u e ) o r t r u e l e t t = n o t ( t r u e ) i n i f t t h e n t e l s e t r u e resugar l e t t = f a l s e i n i f t t h e n t e l s e t r u e 29
Surface Core desugar n o t ( t r u e ) o r t r u e l e t t = n o t ( t r u e ) i n i f t t h e n t e l s e t r u e resugar t r u e o r t r u e l e t t = f a l s e i n o r t r u e i f t t h e n t e l s e t r u e 30
Surface Core desugar n o t ( t r u e ) o r t r u e l e t t = n o t ( t r u e ) i n i f t t h e n t e l s e t r u e resugar t r u e o r t r u e l e t t = f a l s e i n o r t r u e i f t t h e n t e l s e t r u e Emulation Each surface term must desugar to the core term it purports to represent 31
Surface Core desugar n o t ( t r u e ) o r t r u e l e t t = n o t ( t r u e ) i n i f t t h e n t e l s e t r u e resugar l e t t = f a l s e i n i f t t h e n t e l s e t r u e 32
Surface Core desugar n o t ( t r u e ) o r t r u e l e t t = n o t ( t r u e ) i n i f t t h e n t e l s e t r u e resugar l e t t = f a l s e i n l e t t = f a l s e i n i f t t h e n t e l s e t r u e i f t t h e n t e l s e t r u e 33
Surface Core desugar n o t ( t r u e ) o r t r u e l e t t = n o t ( t r u e ) i n i f t t h e n t e l s e t r u e resugar l e t t = f a l s e i n l e t t = f a l s e i n i f t t h e n t e l s e t r u e i f t t h e n t e l s e t r u e Abstraction Show things in terms of a sugar precisely when the programmer used that sugar. 34
Surface Core desugar n o t ( t r u e ) o r t r u e l e t t = n o t ( t r u e ) i n i f t t h e n t e l s e t r u e resugar l e t t = f a l s e i n i f t t h e n t e l s e t r u e resugar i f f a l s e t h e n f a l s e e l s e t r u e resugar t r u e t r u e 35
Surface Core desugar n o t ( t r u e ) o r t r u e l e t t = n o t ( t r u e ) i n i f t t h e n t e l s e t r u e resugar Coverage l e t t = f a l s e i n i f t t h e n t e l s e t r u e Show as many steps resugar as possible i f f a l s e t h e n f a l s e e l s e t r u e resugar t r u e t r u e 36
x o r y - > l e t t = x i n i f t t h e n t e l s e y 37
expand match substitute x o r y - > l e t t = x i n i f t t h e n t e l s e y 38
expand match substitute x o r y - > l e t t = x i n i f t t h e n t e l s e y substitute match unexpand 39
A Little Theory expand : Surf Term → Core Term unexpand : Core Term × Surf Term → Surf Term 40
A Little Theory expand : Surf Term → Core Term unexpand : Core Term × Surf Term → Surf Term It's a lens! 41
A Little Theory expand : Surf Term → Core Term unexpand : Core Term × Surf Term → Surf Term It's a lens! unexpand (expand T) T = T GetPut GetPut expand (unexpand T’ T) = T’ PutGet PutGet 42
A Little Theory expand : Surf Term → Core Term unexpand : Core Term × Surf Term → Surf Term It's a lens! unexpand (expand T) T = T GetPut GetPut expand (unexpand T’ T) = T’ PutGet PutGet Well-formedness criteria on rules ensure these laws. 43
Key Properties Emulation Abstraction Coverage 44
Key Properties Emulation (Lens Laws) Abstraction Coverage 45
Key Properties Emulation (Lens Laws) Abstraction (T agging – see paper) Coverage 46
Key Properties Emulation (Lens Laws) Abstraction (T agging – see paper) Coverage (Empirical) 47
A WORKING SYSTEM 48
92 steps 7 steps 49
Surface Core ( o r ( a n d # t # f ) # f ) 50
Recommend
More recommend