This commit is contained in:
@@ -27,6 +27,7 @@ import Gyehoek.GenSym
|
|||||||
import Control.Monad.Cont
|
import Control.Monad.Cont
|
||||||
import Data.Foldable
|
import Data.Foldable
|
||||||
import Data.List.NonEmpty (NonEmpty((:|)), toList)
|
import Data.List.NonEmpty (NonEmpty((:|)), toList)
|
||||||
|
import Data.List.NonEmpty qualified as NE
|
||||||
import Gyehoek.QBE (FuncDef(FuncDef))
|
import Gyehoek.QBE (FuncDef(FuncDef))
|
||||||
import Data.Foldable1
|
import Data.Foldable1
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
@@ -57,7 +58,8 @@ data Exp
|
|||||||
|
|
||||||
|
|
||||||
collapseBindings
|
collapseBindings
|
||||||
-- | Match constructor.
|
-- | Match constructor. (an affine fold would be preferable to a
|
||||||
|
-- prism here)
|
||||||
:: Prism' a b
|
:: Prism' a b
|
||||||
-- | Extract subexpression from match.
|
-- | Extract subexpression from match.
|
||||||
-> Getter b a
|
-> Getter b a
|
||||||
@@ -68,19 +70,44 @@ collapseBindings p l e =
|
|||||||
Just a -> collapseBindings p l (a ^. l) & _1 %~ (a:)
|
Just a -> collapseBindings p l (a ^. l) & _1 %~ (a:)
|
||||||
Nothing -> ([], e)
|
Nothing -> ([], e)
|
||||||
|
|
||||||
|
expandBindings
|
||||||
|
-- | Match constructor. (an affine fold would be preferable to a
|
||||||
|
-- prism here)
|
||||||
|
:: Prism' e (lhs, rhs, e)
|
||||||
|
-> e
|
||||||
|
-> (List (lhs, rhs), e)
|
||||||
|
expandBindings p = go [] where
|
||||||
|
go acc e =
|
||||||
|
case e ^? p of
|
||||||
|
Just (l,r,e') -> go ((l,r):acc) e'
|
||||||
|
Nothing -> (acc, e)
|
||||||
|
|
||||||
|
collapseBindings'
|
||||||
|
:: Foldable f => Prism' e (lhs, rhs, e) -> f (lhs, rhs)
|
||||||
|
-> e -> e
|
||||||
|
collapseBindings' p bs e = foldr (\(l,r) e -> p # (l,r,e)) e bs
|
||||||
|
|
||||||
|
-- | Technically unlawful.
|
||||||
|
bindingTelescope
|
||||||
|
:: Prism' e (lhs, rhs, e)
|
||||||
|
-> Iso' e (List (lhs, rhs), e)
|
||||||
|
bindingTelescope p = iso
|
||||||
|
(expandBindings p)
|
||||||
|
(uncurry $ collapseBindings' p)
|
||||||
|
|
||||||
foldLet
|
foldLet
|
||||||
:: Prism' Exp a
|
:: Prism' Exp (lhs, rhs, Exp)
|
||||||
-> Lens' a lhs
|
|
||||||
-> Lens' a rhs
|
|
||||||
-> Lens' a Exp
|
|
||||||
-> Grammar
|
-> Grammar
|
||||||
Position
|
Position
|
||||||
(Exp :- NonEmpty (lhs, rhs) :- t)
|
(Exp :- NonEmpty (lhs, rhs) :- t)
|
||||||
(Exp :- _)
|
(Exp :- rhs :- lhs :- t)
|
||||||
foldLet p lhs rhs exp =
|
foldLet p =
|
||||||
IG.Iso
|
IG.Iso
|
||||||
_
|
(\(e :- ((l1,r1):|bs) :- t) ->
|
||||||
_
|
collapseBindings' p bs e :- r1 :- l1 :- t)
|
||||||
|
(\(e :- r :- l :- t) ->
|
||||||
|
let (bs,e') = expandBindings p e
|
||||||
|
in e' :- ((l,r) :| bs) :- t)
|
||||||
|
|
||||||
instance SexpIso Val where
|
instance SexpIso Val where
|
||||||
sexpIso = match
|
sexpIso = match
|
||||||
@@ -91,6 +118,11 @@ instance SexpIso Val where
|
|||||||
nonEmptyIso :: Iso (NonEmpty a) (NonEmpty b) (a, List a) (b, List b)
|
nonEmptyIso :: Iso (NonEmpty a) (NonEmpty b) (a, List a) (b, List b)
|
||||||
nonEmptyIso = iso (\(x:|xs) -> (x,xs)) (uncurry (:|))
|
nonEmptyIso = iso (\(x:|xs) -> (x,xs)) (uncurry (:|))
|
||||||
|
|
||||||
|
nonEmptyGrammar :: Grammar p (NonEmpty x :- t) (List x :- x :- t)
|
||||||
|
nonEmptyGrammar = IG.Iso
|
||||||
|
_
|
||||||
|
_
|
||||||
|
|
||||||
instance SexpIso Exp where
|
instance SexpIso Exp where
|
||||||
sexpIso = match
|
sexpIso = match
|
||||||
$ With (. letapp)
|
$ With (. letapp)
|
||||||
@@ -103,12 +135,18 @@ instance SexpIso Exp where
|
|||||||
:: Grammar Position (Sexp :- t) (Exp :- (Prim Val :- (Text :- t)))
|
:: Grammar Position (Sexp :- t) (Exp :- (Prim Val :- (Text :- t)))
|
||||||
letprim =
|
letprim =
|
||||||
Gyehoek.Sexp.let_ symbol (sexpIso @(Prim Val)) (sexpIso @Exp)
|
Gyehoek.Sexp.let_ symbol (sexpIso @(Prim Val)) (sexpIso @Exp)
|
||||||
>>> foldLet #ExpLetPrim _1 _2 _3
|
>>> foldLet #ExpLetPrim
|
||||||
letapp :: Grammar
|
letapp :: Grammar
|
||||||
Position (Sexp :- t) (Exp :- List Val :- Val :- Text :- t)
|
Position (Sexp :- t) (Exp :- List Val :- Val :- Text :- t)
|
||||||
letapp =
|
letapp =
|
||||||
Gyehoek.Sexp.let_ symbol (sexpIso @(NonEmpty Val)) (sexpIso @Exp)
|
Gyehoek.Sexp.let_ symbol (sexpIso @(NonEmpty Val)) (sexpIso @Exp)
|
||||||
>>> foldLet #ExpLetApply _1 (lensProduct _2 _3 . from nonEmptyIso) _4
|
>>> foldLet (#ExpLetApply
|
||||||
|
. iso (\(rhs,f,xs,e) -> (rhs, f:|xs, e))
|
||||||
|
(\(rhs,f:|xs,e) -> (rhs,f,xs,e)))
|
||||||
|
>>> onTail nonEmptyGrammar
|
||||||
|
-- >>> IG.Iso
|
||||||
|
-- (\(e :- (f:|xs) :- t) -> e :- f :- xs :-)
|
||||||
|
-- _
|
||||||
-- foldLet =
|
-- foldLet =
|
||||||
-- IG.Iso
|
-- IG.Iso
|
||||||
-- (\(e :- ((r,f:|xs):|bs) :- t) ->
|
-- (\(e :- ((r,f:|xs):|bs) :- t) ->
|
||||||
|
|||||||
Reference in New Issue
Block a user