History

Add transformation rules for top level methods

This can be quite effective at removing runtime overhead when just using an overloaded method directly. It is, however, still quite limited (only works on top level interfaces, so no inlining of parents, e.g. like X a => X (List a), and sometimes generating the transform rules fails for reasons I haven't worked out yet). This is experimental, I might change the way this works at any point, but it's a nice improvement for now.

Edwin Brady committed 6 years ago

Commit b617cae1 parent 11cbc303 files · +30 −6Browse files

Unified · Split

3 files
  1. libs/prelude/Prelude.idr +0 −1
  2. src/Core/Transform.idr +1 −2
  3. src/Idris/Elab/Implementation.idr +29 −3
libs/prelude/Prelude.idr+0−1
@@ -1416,7 +1416,6 @@ Applicative IO where · 1415 unmodified lines
1416 io_bind a (\a' =>1416 io_bind a (\a' =>
1417 io_pure (f' a')))1417 io_pure (f' a')))
1418 1418
1419%inline
1420public export1419public export
1421Monad IO where1420Monad IO where
1422 b >>= k = io_bind b k1421 b >>= k = io_bind b k
src/Core/Transform.idr+1−2
@@ -108,8 +108,7 @@ trans env stk (Ref fc Func fn) · 107 unmodified lines
108 = do defs <- get Ctxt108 = do defs <- get Ctxt
109 case lookup fn (transforms defs) of109 case lookup fn (transforms defs) of
110 Nothing => pure (unload stk (Ref fc Func fn))110 Nothing => pure (unload stk (Ref fc Func fn))
111 Just ts => do fullapp <- normaliseHoles defs env111 Just ts => do let fullapp = unload stk (Ref fc Func fn)
112 (unload stk (Ref fc Func fn))
113 let (u, tm') = apply ts fullapp112 let (u, tm') = apply ts fullapp
114 upd <- get Upd113 upd <- get Upd
115 put Upd (upd || u)114 put Upd (upd || u)
src/Idris/Elab/Implementation.idr+29−3
@@ -217,10 +217,14 @@ elabImplementation {vars} fc vis opts_in pass env nest is cons iname ps impln nu · 216 unmodified lines
217 -- case of dependencies)217 -- case of dependencies)
218 218
219 -- 5. Elaborate the method bodies219 -- 5. Elaborate the method bodies
220 220 let upds = map methNameUpdate fns
221 body' <- traverse (updateBody (map methNameUpdate fns)) body221 body' <- traverse (updateBody upds) body
222 log 10 $ "Implementation body: " ++ show body'222 log 10 $ "Implementation body: " ++ show body'
223 traverse (processDecl [] nest env) body'223 traverse (processDecl [] nest env) body'
224
225 -- 6. Add transnformation rules for top level methods
226 traverse (addTransform impName upds) (methods cdata)
227
224 -- Reset the open hints (remove the named implementation)228 -- Reset the open hints (remove the named implementation)
225 setOpenHints hs229 setOpenHints hs
226 pure ()) mbody230 pure ()) mbody
@@ -283,7 +287,8 @@ elabImplementation {vars} fc vis opts_in pass env nest is cons iname ps impln nu · 56 unmodified lines
283 methName : Name -> Name287 methName : Name -> Name
284 methName (NS _ n) = methName n288 methName (NS _ n) = methName n
285 methName n289 methName n
286 = DN (show n) (UN (show n ++ "_" ++ show iname ++ "_" ++290 = DN (show n)
291 (UN (show n ++ "_" ++ show iname ++ "_" ++
287 maybe "" show impln ++ "_" ++292 maybe "" show impln ++ "_" ++
288 showSep "_" (map show ps)))293 showSep "_" (map show ps)))
289 294
@@ -404,3 +409,24 @@ elabImplementation {vars} fc vis opts_in pass env nest is cons iname ps impln nu · 114 unmodified lines
404 updateBody ns _409 updateBody ns _
405 = throw (GenericMsg fc410 = throw (GenericMsg fc
406 "Implementation body can only contain definitions")411 "Implementation body can only contain definitions")
412
413 addTransform : Name -> List (Name, Name) ->
414 (Name, RigCount, Bool, RawImp) ->
415 Core ()
416 addTransform iname ns (top, _, _, ty)
417 = do log 3 $ "Adding transform for " ++ show top ++ " : " ++ show ty ++
418 "\n\tfor " ++ show iname ++ " in " ++ show ns
419 let lhs = IImplicitApp fc (IVar fc top)
420 (Just (UN "__con"))
421 (IVar fc iname)
422 let Just mname = lookup (dropNS top) ns
423 | Nothing => pure ()
424 let rhs = IVar fc mname
425 log 5 $ show lhs ++ " ==> " ++ show rhs
426 handleUnify
427 (processDecl [] nest env
428 (ITransform fc (UN (show top ++ " " ++ show iname)) lhs rhs))
429 (\err =>
430 log 5 $ "Can't add transform " ++
431 show lhs ++ " ==> " ++ show rhs ++
432 "\n\t" ++ show err)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t