Skip to content

Commit 3adab69

Browse files
committed
[refactor] all lenses are now closed
1 parent b031881 commit 3adab69

File tree

3 files changed

+39
-80
lines changed

3 files changed

+39
-80
lines changed

src/Data/Container/Morphism/Definition.idr

Lines changed: 26 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,86 +10,66 @@ Two different types of morphisms:
1010
dependent lenses, and dependent charts
1111
-------------------------------------------------------------------------------}
1212

13-
export infixr 1 =%> -- dependent lens
14-
export infixr 1 =&> -- dependent chart
15-
export infix 1 <%! -- constructor for dependent lens
16-
export infix 1 <&! -- constructor for dependent chart
17-
export prefix 0 !% -- constructor for closed dependent lens
18-
export prefix 0 !& -- constructor for closed dependent chart
13+
export infixr 1 =%> -- (closed) dependent lens
14+
export infixr 1 =&> -- (closed) dependent chart
15+
export prefix 0 !% -- constructor the (closed) dependent lens
16+
export prefix 0 !& -- constructor the (closed) dependent chart
1917
export infixl 5 %>> -- composition of dependent lenses
18+
export infixl 5 &>> -- composition of dependent charts
2019

2120
namespace DependentLenses
2221
||| Dependent lenses
2322
||| Forward-backward container morphisms
2423
public export
25-
record (=%>) (c1, c2 : Cont) where
26-
constructor (<%!)
27-
fwd : c1.Shp -> c2.Shp
28-
bwd : (x : c1.Shp) -> c2.Pos (fwd x) -> c1.Pos x
24+
data (=%>) : (c1, c2 : Cont) -> Type where
25+
(!%) : ((x : c1.Shp) -> (y : c2.Shp ** (c2.Pos y -> c1.Pos x))) -> c1 =%> c2
2926

3027
%name (=%>) f, g, h
31-
32-
33-
||| Constructor for closed dependent lens
34-
public export
35-
(!%) : {0 c1, c2 : Cont} ->
36-
((x : c1.Shp) -> (y : c2.Shp ** (c2.Pos y -> c1.Pos x))) ->
37-
c1 =%> c2
38-
(!%) f = (\x => fst (f x)) <%! (\x => snd (f x))
3928

4029
public export
41-
(%!) : {0 c1, c2 : Cont} ->
42-
(f : c1 =%> c2) ->
43-
(x : c1.Shp) -> (y : c2.Shp ** (c2.Pos y -> c1.Pos x))
44-
(%!) f x = (f.fwd x ** f.bwd x)
30+
(%!) : c1 =%> c2 -> (x : c1.Shp) -> (y : c2.Shp ** (c2.Pos y -> c1.Pos x))
31+
(%!) (!% f) x = f x
4532

4633
||| Composition of dependent lenses
4734
public export
4835
compDepLens : a =%> b -> b =%> c -> a =%> c
49-
compDepLens f g =
50-
(g.fwd . f.fwd) <%!
51-
(\x => f.bwd x . g.bwd (f.fwd x))
52-
36+
compDepLens (!% f) (!% g) = !% \x => let (b ** kb) = f x
37+
(c ** kc) = g b
38+
in (c ** kb . kc)
5339
public export
5440
(%>>) : a =%> b -> b =%> c -> a =%> c
5541
(%>>) = compDepLens
5642

5743
public export
5844
id : a =%> a
59-
id = !% \x => (x ** \y => y)
45+
id = !% \x => (x ** id)
6046

6147
namespace DependentCharts
6248
||| Dependent charts
6349
||| Forward-forward container morphisms
6450
public export
65-
record (=&>) (c1, c2 : Cont) where
66-
constructor (<&!)
67-
fwd : c1.Shp -> c2.Shp
68-
fwd' : (x : c1.Shp) -> c1.Pos x -> c2.Pos (fwd x)
69-
70-
71-
||| Constructor for closed dependent chart
72-
public export
73-
(!&) : {0 c1, c2 : Cont} ->
74-
((x : c1.Shp) -> (y : c2.Shp ** (c1.Pos x -> c2.Pos y))) ->
75-
c1 =&> c2
76-
(!&) f = (\x => fst (f x)) <&! (\x => snd (f x))
51+
data (=&>) : (c1, c2 : Cont) -> Type where
52+
(!&) : ((x : c1.Shp) -> (y : c2.Shp ** (c1.Pos x -> c2.Pos y))) -> c1 =&> c2
7753

54+
%name (=&>) f, g, h
7855

56+
public export
57+
(%!) : c1 =&> c2 -> (x : c1.Shp) -> (y : c2.Shp ** (c1.Pos x -> c2.Pos y))
58+
(%!) (!& f) x = f x
59+
7960
public export
8061
compDepChart : a =&> b -> b =&> c -> a =&> c
81-
compDepChart f g =
82-
(g.fwd . f.fwd) <&!
83-
(\x => g.fwd' (f.fwd x) . f.fwd' x)
62+
compDepChart (!& f) (!& g) = !& \x => let (b ** kb) = f x
63+
(c ** kc) = g b
64+
in (c ** kc . kb)
8465

8566
public export
8667
(&>>) : a =&> b -> b =&> c -> a =&> c
8768
(&>>) = compDepChart
8869

8970
public export
9071
id : a =&> a
91-
id = !& \x => (x ** \y => y)
92-
72+
id = !& \x => (x ** id)
9373

9474
-- experimental stuff below
9575
||| TODO is this the extension of a container?
@@ -101,16 +81,5 @@ val (shp !> pos) r = (s : shp) !> (pos s -> r)
10181
valContMap : {c1, c2 : Cont} -> {r : Type}
10282
-> (f : c1 =&> c2)
10383
-> (c1 `val` r) =%> (c2 `val` r)
104-
valContMap {c1=(shp !> pos)} {c2=(shp' !> pos')} (fwd <&! fwd')
105-
= fwd <%! (\x, k, x' => k (fwd' x x'))
106-
107-
-- ||| A container morphism
108-
-- public export
109-
-- record (~%>) (c1, c2 : ContF R) where
110-
-- constructor (<~!)
111-
-- fwd' : c1.Shp' -> c2.Shp'
112-
113-
114-
-- upd : c1 ~%> c2 ->
115-
-- %pair (=%>) fwd bwd
116-
84+
valContMap {c1=(shp !> pos)} {c2=(shp' !> pos')} (!& f)
85+
= !% \x => let (y ** ky) = f x in (y ** (. ky))

src/Data/Container/Morphism/Instances.idr

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Data.Container.Morphism.Instances
22

33
import Data.Fin
4+
import Data.Fin.Split
45

56
import Data.Container.Object.Instances
67
import Data.Container.Morphism.Definition
@@ -15,7 +16,13 @@ public export
1516
extMap : {c, d : Cont} ->
1617
c =%> d ->
1718
Ext c a -> Ext d a
18-
extMap (fwd <%! bwd) (sh <| index) = fwd sh <| index . bwd sh
19+
extMap (!% f) (sh <| index) = let (y ** ky) = f sh
20+
in y <| (index . ky)
21+
22+
23+
||| Reshape is an isomorphism!
24+
reshapeVectIndexes : {n, m : Nat} -> (Vect n >< Vect m) =%> Vect (n * m)
25+
reshapeVectIndexes = !% \((), ()) => (() ** splitProd)
1926

2027

2128
-- need to organise this
@@ -51,17 +58,13 @@ namespace BinTreeNode
5158
inorder = !% \b => (numNodes b ** inorderBackward b)
5259

5360

54-
5561
-- TODO reshape commented out for the same reason as reshapeTensorA is
5662
-- public export
5763
-- reshape : {oldShape, newShape : List Nat} ->
5864
-- Tensor oldShape a ->
5965
-- {auto prf : prod oldShape = prod newShape} ->
6066
-- Tensor newShape a
6167

62-
63-
64-
6568
-- Need to do some rewriting for preorder
6669
public export
6770
preorderBinTreeNode : (b : BinTreeShape) -> Fin (numNodes b) -> BinTreePosNode b
@@ -71,29 +74,16 @@ preorderBinTreeNode (NodeS lt rt) x = ?preorderBinTreeNode_rhs_1
7174
-- _ | Right FZ = ?whn
7275
-- _ | Right (FS g) = ?whr
7376

74-
7577
public export
7678
maybeToList : Maybe =%> List
7779
maybeToList = !% \b => case b of
7880
False => (0 ** absurd)
7981
True => (1 ** \_ => ())
8082

81-
reshapeVectIndexes : (Vect n >< Vect m) =%> Vect (n * m)
82-
reshapeVectIndexes = (\_ => ()) <%! (\((), ()) => ?reshapeVects_rhs2)
83-
84-
reshapeVects :
85-
(Vect n >< Vect m) `fullOf` a ->
86-
Vect (n * m) `fullOf` a
87-
reshapeVects (((), ()) <| index)
88-
= () <| ?reshapeVects_rhs_4
89-
90-
9183

9284
-- public export
9385
-- traverseLeaf : (x : BinTreeShape) -> FinBinTreeLeaf x -> Fin (numLeaves x)
9486
-- traverseLeaf LeafS Done = FZ
9587
-- traverseLeaf (NodeS lt rt) (GoLeft x) = weakenN (numLeaves rt) (traverseLeaf lt x)
9688
-- traverseLeaf (NodeS lt rt) (GoRight x) = shift (numLeaves lt) (traverseLeaf rt x)
97-
--
98-
99-
-- reshapings are isomorphisms
89+
--

src/Data/Tensor/Tensor.idr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,8 @@ namespace Reshape
699699
wrap : {c, d : Cont} ->
700700
c =%> d ->
701701
Cont.Tensor [c] =%> Cont.Tensor [d]
702-
wrap (fwd <%! bwd) = (\e => fwd (shapeExt e) <| \_ => ()) <%!
703-
(\e, (cp ** ()) => (bwd (shapeExt e) cp ** ()))
702+
wrap (!% f) = !% \e => let (y ** ky) = f (shapeExt e)
703+
in (y <| \_ => () ** \(cp ** ()) => (ky cp ** ()))
704704

705705
||| Effectively a wrapper around `extMap`
706706
||| Allows us to define views, traversals and general reshaping
@@ -745,7 +745,7 @@ namespace Reshape
745745
dLensReshape : {oldShape, newShape : List Nat} ->
746746
{auto prf : prod oldShape = prod newShape} ->
747747
Vect (prod oldShape) =%> Vect (prod newShape)
748-
dLensReshape = id <%! \(), i => rewrite prf in i
748+
dLensReshape = !% \() => (() ** \i => rewrite prf in i)
749749

750750
||| Restructuring for cubical tensors that leaves number of elements unchanged
751751
public export
@@ -893,4 +893,4 @@ namespace Concatenate
893893
public export
894894
concat : {shape : List Nat} -> {x : Nat} ->
895895
Tensor (x :: shape) a -> Tensor (y :: shape) a -> Tensor (x + y :: shape) a
896-
concat t t' = embedTopExt $ extractTopExt t ++ extractTopExt t'
896+
concat t t' = embedTopExt $ extractTopExt t ++ extractTopExt t'

0 commit comments

Comments
 (0)