Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Scrapping “Scrap Your Nameplate” Michael D. Adams.

Similar presentations


Presentation on theme: "1 Scrapping “Scrap Your Nameplate” Michael D. Adams."— Presentation transcript:

1 1 Scrapping “Scrap Your Nameplate” Michael D. Adams

2 2 Introduction “Scrap Your Nameplate” by James Cheney (author of αProlog) “Scrap Your Boilerplate” by Simon Peyton Jones et al. Nominal Logic αKanren Nominal Programing

3 3 Introduction SYN fails at any synergy between αL and SYB SYN is αProgramming rather than αLogic SYN doesn't match the theory (but it could)

4 4 Overview What SYN does User Level Behind the Scenes Examples YAαP

5 5 Overview How to improve SYN Fixing the logic to match the theory Fixing the “fixed” logic to be efficient Misc Solve the AST restrictions How Haskell/Scheme should be changed

6 6 SYN: User Level Types: Name, a \\\ b class Nom t where swap :: Name -> Name -> t -> t fresh :: Name -> t -> Bool aeq :: t -> t -> Bool class Subst class FreeVars

7 7 SYN: Thesis “Writing 'nameplate' code is boring” (Only issue in a language with user defined types) Use SYB to implement SYN One CAS to rule them all

8 8 What is SYB? SYB writes the boring parts functions over tree shaped data types for you Best seen with example

9 9 Write “getUsedVars” data Type = VarTy Name | FnTy Type Type | AllTy (Name \\\ Type) data Term = Var Name | App Term Term | Lam Type (Name \\\ Term) | TyLam (Name \\\ Term) | TyApp Term Type

10 10 What is SYB? Write all functions in terms of gfoldl gfoldl is automatically generated for all data types class (Typeable a) => Data a where gfoldl :: (forall a b. (Data a) => c (a -> b) -> a -> c b) -> (forall g. g -> c g) -> a -> c a

11 11 SYB → SYN Scrap Your Boilerplate getUsedVars Scrap Your Nameplate getFreeVars SYN requires: All binders use (Name \\\ a) Info about variables

12 12 SYB → SYN instance HasVar Type where is_var (VarTy x) = Just x is_var _ = Nothing var x = VarTy x instance HasVar Term where is_var (Var x) = Just x is_var _ = Nothing var x = Var x

13 13 SYB → SYN You write HasVar SYB writes gfoldl SYN gives you Nom, Subst, FreeVars for FREE!

14 14 So what is wrong?

15 15 So what is wrong? Consider this function dirty (Lam ty1 (x1 \\\ (Lam ty2 (x2 \\\ body)))) = (Lam ty1 (x1 \\\ (Lam ty2 (x2 \\\ (App (Var x1) body))))) What if x1 = x2?

16 16 SYN Solution unAbs :: (FreshMonad m) => (a \\\ t) -> m (a, t) unAbs (a \\\ t) = do a' <- freshen a return (a', swap a a' t)

17 17 Why is the solution wrong? R: But that ignores the nominal logic side conditions! M: What side conditions? R: Functions should only be able to deconstruct (a \\\ t) if “a” remains fresh in its output M: Example?

18 18 Why is the solution wrong? R: dirtier (Lam ty1 rest) = do (x, body) <- unAbs rest return body M: Why is that a problem? R: This breaks observational equivalance

19 19 Why is the solution wrong? M: How? R: do t1 = Lam ty (a \\\ Var a) t2 <- dirtier t1 t3 <- dirtier t1 return (t2 == t3, t2 `aeq` t3)

20 20 How do we fix the solution? liftAlpha :: ((a, t1) -> m t2) -> ((a \\\ t1) -> m t2) liftAlpha f (a \\\ t1) = do a' <- freshen a t2 <- f (a, swap a a' t1) if fresh a t2 then return t2 else throwError

21 21 Can these checks be fast? Static checks See Rice's Theorem Dynamic checks O(size(t)) or maybe not... Or maybe nominal theory is wrong R: No it isn't!

22 22 Can these checks be fast? (swap a a' t1) causes O(size(t)) cost Eliminate by introducing suspended swaps Suspended swaps “pushed-down” on demand Combine neighboring swaps (swap a b (swap b c t)) → (swap a c t) only if swaps generated by liftAlpha because “a” and “b” are gensym and thus are fresh in “t” Constant overhead cost

23 23 Can these checks be fast? (fresh a' t2) also causes O(size(t)) cost (fresh a' t1) stops when it sees (swap a' a (a \\\ t2)) = (a' \\\ t2) (fresh a' t1) will not stop when it sees (swap a' a (... (Var a))) Problem!

24 24 Can these checks be fast? (fresh a' t2) also causes O(size(t)) cost (fresh a t2) implies (fresh a' (swap a' a t2)) Keep cached free variable info on each term Ugh! But what else can we do?

25 25 Misc Improvements Parameterize over Binders Like HasVar is used to parameterize over variables class Bind a t where is_bind :: t -> Maybe (a, t) Suspended swaps behave a lot like thunks Add user defined thunks to Haskell? Thunk operations: Construct, Force, Combine Must avoid “isThunk” predicate

26 26 Conclusion SYN saves you from writing “nameplate” But doesn't ensure that your functions are nominally correct Dynamic checks for nominal correctness asymptotically expensive

27 27 Conclusion Optimizations might bring expense down to constant factor Might → No implementation yet, only design But they may require heavy weight meta-data on each value to be kept Are they worth it? Discuss


Download ppt "1 Scrapping “Scrap Your Nameplate” Michael D. Adams."

Similar presentations


Ads by Google