2module Transform.SortDefinitions (boundVars) where
4import Control.Monad.State
5import qualified Data.Set as Set
6import qualified Data.Map as Map
7import SourceSyntax.Everything
10boundVars :: Pattern -> Set.Set String
13 PVar x -> Set.singleton x
14 PAlias x p -> Set.insert x (boundVars p)
15 PData _ ps -> Set.unions (map boundVars ps)
16 PRecord fields -> Set.fromList fields
17 PAnything -> Set.empty
18 PLiteral _ -> Set.empty
20free x = modify (Set.insert x)
21bound boundVars = modify (\freeVars -> Set.difference freeVars boundVars)
23reorderAndGetDependencies pattern expr =
27 modify (Set.union globalFrees)
28 let addDep var deps = Map.insert var localFrees deps
29 dependencies = Set.foldr addDep Map.empty (boundVars pattern)
30 return (expr', dependencies)
32reorder :: LExpr t v -> State (Set.Set String) (LExpr t v)
33reorder lexpr@(L a b expr) =
37 -- Actually do some reordering
39 do -- swap in a fresh environment
41 -- swap in the old environment
43 bound (boundVars (patterns defs))
44 Let `liftM` defs' `ap` body'
47 patterns = flip concatMap defs $
49 Def pattern _ -> [pattern]
50 TypeAnnotation _ _ -> []
52 -- Be careful adding and restricting freeVars
53 Var x -> free x >> return expr
56 uncurry Lambda `liftM` bindingReorder (p,e)
60 Binop op `liftM` reorder e1 `ap` reorder e2
63 Case `liftM` reorder e `ap` mapM bindingReorder cases
67 Data name `liftM` mapM reorder es
69 -- Just pipe the reorder though
70 Literal _ -> return expr
73 Range `liftM` reorder e1 `ap` reorder e2
76 ExplicitList `liftM` mapM reorder es
79 App `liftM` reorder e1 `ap` reorder e2
82 MultiIf `liftM` mapM reorderPair branches
85 Access `liftM` reorder e `ap` return lbl
88 Remove `liftM` reorder e `ap` return lbl
91 Insert `liftM` reorder e `ap` return lbl `ap` reorder v
94 Modify `liftM` reorder e `ap` mapM reorderField fields
97 Record `liftM` mapM reorderField fields
99 Markdown _ -> return expr
101reorderField (label, expr) =
102 (,) label `liftM` reorder expr
105 (,) `liftM` reorder e1 `ap` reorder e2
107bindingReorder :: (Pattern, LExpr t v) -> State (Set.Set String) (Pattern, LExpr t v)
108bindingReorder (pattern,expr) =
109 do expr' <- reorder expr
110 bound (boundVars pattern)
111 return (pattern, expr')
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list