git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Write everythingM by hand.
commiteded2bd3c6b6e976c9c11cc6f8b82cfaecbba0b5 authorPhil Freeman <paf31@cantab.net> dateTue 15 Apr 2014, 20:02 (13 years ago) parentbeaef52
2 files changed+57−3
src/Language/PureScript/Declarations.hs+51 src/Language/PureScript/Sugar/DoNotation.hs+6−3
src/Language/PureScript/Declarations.hs+51
@@ -517,6 +517,7 @@ everywhereOnValues f g h = (f', g', h')
517517 handleDoNotationElement (DoNotationLet ds) = DoNotationLet (map f' ds)
518518 handleDoNotationElement (PositionedDoNotationElement pos e) = PositionedDoNotationElement pos (handleDoNotationElement e)
519519
520+
520521everywhereOnValuesTopDownM :: (Functor m, Applicative m, Monad m) =>
521522 (Declaration -> m Declaration) ->
522523 (Value -> m Value) ->
@@ -566,6 +567,55 @@ everywhereOnValuesTopDownM f g h = (f' <=< f, g' <=< g, h' <=< h)
566567 handleDoNotationElement (DoNotationLet ds) = DoNotationLet <$> mapM (f' <=< f) ds
567568 handleDoNotationElement (PositionedDoNotationElement pos e) = PositionedDoNotationElement pos <$> handleDoNotationElement e
568569
570+everywhereOnValuesM :: (Functor m, Applicative m, Monad m) =>
571+ (Declaration -> m Declaration) ->
572+ (Value -> m Value) ->
573+ (Binder -> m Binder) ->
574+ (Declaration -> m Declaration, Value -> m Value, Binder -> m Binder)
575+everywhereOnValuesM f g h = (f' <=< f, g' <=< g, h' <=< h)
576+ where
577+ f' (DataBindingGroupDeclaration ds) = (DataBindingGroupDeclaration <$> mapM f' ds) >>= f
578+ f' (ValueDeclaration name nameKind bs grd val) = (ValueDeclaration name nameKind <$> mapM h' bs <*> maybeM g' grd <*> g' val) >>= f
579+ f' (BindingGroupDeclaration ds) = (BindingGroupDeclaration <$> mapM (\(name, nameKind, val) -> (,,) name nameKind <$> g' val) ds) >>= f
580+ f' (TypeClassDeclaration name args implies ds) = (TypeClassDeclaration name args implies <$> mapM f' ds) >>= f
581+ f' (TypeInstanceDeclaration name cs className args ds) = (TypeInstanceDeclaration name cs className args <$> mapM f' ds) >>= f
582+ f' (PositionedDeclaration pos d) = (PositionedDeclaration pos <$> f' d) >>= f
583+ f' other = f other
584+
585+ g' (UnaryMinus v) = (UnaryMinus <$> g' v) >>= g
586+ g' (BinaryNoParens op v1 v2) = (BinaryNoParens op <$> (g' v1) <*> (g' v2)) >>= g
587+ g' (Parens v) = (Parens <$> g' v) >>= g
588+ g' (ArrayLiteral vs) = (ArrayLiteral <$> mapM g' vs) >>= g
589+ g' (ObjectLiteral vs) = (ObjectLiteral <$> mapM (sndM g') vs) >>= g
590+ g' (Accessor prop v) = (Accessor prop <$> g' v) >>= g
591+ g' (ObjectUpdate obj vs) = (ObjectUpdate <$> g' obj <*> mapM (sndM g') vs) >>= g
592+ g' (Abs name v) = (Abs name <$> g' v) >>= g
593+ g' (App v1 v2) = (App <$> g' v1 <*> g' v2) >>= g
594+ g' (IfThenElse v1 v2 v3) = (IfThenElse <$> g' v1 <*> g' v2 <*> g' v3) >>= g
595+ g' (Case vs alts) = (Case <$> mapM g' vs <*> mapM handleCaseAlternative alts) >>= g
596+ g' (TypedValue check v ty) = (TypedValue check <$> g' v <*> pure ty) >>= g
597+ g' (Let ds v) = (Let <$> mapM f' ds <*> g' v) >>= g
598+ g' (Do es) = (Do <$> mapM handleDoNotationElement es) >>= g
599+ g' (PositionedValue pos v) = (PositionedValue pos <$> g' v) >>= g
600+ g' other = g other
601+
602+ h' (ConstructorBinder ctor bs) = ConstructorBinder ctor <$> mapM h' bs
603+ h' (ObjectBinder bs) = (ObjectBinder <$> mapM (sndM h') bs) >>= h
604+ h' (ArrayBinder bs) = (ArrayBinder <$> mapM h' bs) >>= h
605+ h' (ConsBinder b1 b2) = (ConsBinder <$> h' b1 <*> h' b2) >>= h
606+ h' (NamedBinder name b) = (NamedBinder name <$> h' b) >>= h
607+ h' (PositionedBinder pos b) = (PositionedBinder pos <$> h' b) >>= h
608+ h' other = h other
609+
610+ handleCaseAlternative (CaseAlternative bs grd val) = CaseAlternative <$> mapM h' bs
611+ <*> maybeM g' grd
612+ <*> g' val
613+
614+ handleDoNotationElement (DoNotationValue v) = DoNotationValue <$> g' v
615+ handleDoNotationElement (DoNotationBind b v) = DoNotationBind <$> h' b <*> g' v
616+ handleDoNotationElement (DoNotationLet ds) = DoNotationLet <$> mapM f' ds
617+ handleDoNotationElement (PositionedDoNotationElement pos e) = PositionedDoNotationElement pos <$> handleDoNotationElement e
618+
569619everythingOnValues :: (r -> r -> r) ->
570620 (Declaration -> r) ->
571621 (Value -> r) ->
@@ -636,3 +686,4 @@ accumTypes f = everythingOnValues mappend forDecls forValues (const mempty) (con
636686 forValues _ = mempty
637687
638688
689+
src/Language/PureScript/Sugar/DoNotation.hs+6−3
@@ -18,8 +18,6 @@ module Language.PureScript.Sugar.DoNotation (
1818 desugarDoModule
1919) where
2020
21−import Data.Generics
22−
2321import Language.PureScript.Names
2422import Language.PureScript.Scope
2523import Language.PureScript.Declarations
@@ -37,15 +35,20 @@ desugarDoModule :: Module -> Either ErrorStack Module
3735desugarDoModule (Module mn ds exts) = Module mn <$> mapM desugarDo ds <*> pure exts
3836
3937desugarDo :: Declaration -> Either ErrorStack Declaration
40−desugarDo = everywhereM (mkM replace)
38+desugarDo =
39+ let (f, _, _) = everywhereOnValuesM return replace return
40+ in f
4141 where
4242 prelude :: ModuleName
4343 prelude = ModuleName [ProperName C.prelude]
44+
4445 bind :: Value
4546 bind = Var (Qualified (Just prelude) (Op (C.>>=)))
47+
4648 replace :: Value -> Either ErrorStack Value
4749 replace (Do els) = go els
4850 replace other = return other
51+
4952 go :: [DoNotationElement] -> Either ErrorStack Value
5053 go [] = error "The impossible happened in desugarDo"
5154 go [DoNotationValue val] = return val