26 | ||| If we model the idea of a container (S !> P) as a box
27 | ||| ┌──────┐
28 | ||| │ s:S │
29 | ||| ├──────┤
30 | ||| │ Ps │
31 | ||| └──────┘
32 | ||| then `pushDown` is interpreted as pushing down the container,
33 | ||| pruning anything that goes out of the box, and using `Unit` for
34 | ||| anything new that appears:
35 | ||| ┌──────┐
36 | ||| │ Unit │
37 | ||| ├──────┤
38 | ||| │ s:S │
39 | ||| └──────┘
40 | ||| Ps
116 | (aShp <| \aPos => f aPos <| \bPos => g (aPos ** bPos) ** \(aPos ** bPos ** cPos) => ((aPos ** bPos) ** cPos))
151 | ||| The following is the proof that for any container `c` there is an
152 | ||| isomorphism in `Cont` between `c` and `CartesianClosure UnitCont c`
153 | ||| This holds in any monoidal closed category: `X ≅ [I, X]`
164 | ||| For a overview of this interaction from the categorical perspective, see
165 | ||| the Poly book (https://arxiv.org/abs/2312.00990) (Section 6.3.4)
167 | ||| Interaction between composition and tensor product
168 | ||| Swaps the operations, and middle two containers
169 | ||| Not an isomorphism!
176 | ||| Tensor product embeds into composition
177 | ||| A special case of `duoidal`
184 | ||| Going the other way is impossible without any constraints
185 | ||| Two possibilities on constraints (this, and `compToTensor2`)
198 | ||| Specific distributive law we need
208 | ||| "State" as defined in https://arxiv.org/abs/2403.13001 and open games
209 | |||
210 | ||| ┌─────────────┐
211 | ||| │ ├──► (x : c.Shp)
212 | ||| │ State │
213 | ||| │ ├◄── c.Pos x
214 | ||| └─────────────┘
219 | ||| Given a shape of any container, state can be defined
235 | ||| "Costate" as defined in https://arxiv.org/abs/2403.13001 and open games
236 | |||
237 | ||| ┌─────────────┐
238 | ||| (x : c.Shp) ──►┤ │
239 | ||| │ Costate │
240 | ||| c.Pos x ◄──┤ │
241 | ||| └─────────────┘
274 | ||| Wraps a dependent lens `c =%> d`
275 | ||| into one of type `c >@ Scalar =%> d >@ Scalar`
276 | ||| Needed because `c >@ Scalar` isn't automatically reduced to `c`
289 | ||| Wraps a dependent lens `c =%> d`
290 | ||| into one of type `c >< Scalar =%> d >< Scalar`
291 | ||| Needed because `c >< Scalar` isn't automatically reduced to `c`
298 | ||| Helper function allowing `shape` in `cubicalShape` to have zero annotation
304 | ||| Given a list of cubical containers, return the list of their dimensions
309 | ||| Size of a list of cubical containers is the product of their dimensions
314 | ||| Layout-aware dependent lens flattening a cubical tensor
328 | ||| Layout-aware dependent lens unflattening a tensor
341 | ||| This is simply a rewrite!
349 | ||| Reshapes a cubical tensor by first flattening it to a linear representation,
350 | ||| casting the type to the new shape, and then unflattening it back
351 | ||| Is generic over layout order
374 | -- ||| experiment, does this work?
375 | -- public export
376 | -- transposeMiddle : IsNaperian c => IsNaperian e =>
377 | -- Tensor [c, e, d] =%>
380 | --||| Transpose a given element to the front of the shape
381 | --public export
382 | --transposeToFront : (shape : List Cont) ->
383 | -- (c : Cont) ->
384 | -- (elem : Elem c shape) =>
385 | -- All IsNaperian (dropAfterElem shape elem) =>
386 | -- Tensor shape =%> Tensor (c :: dropElem shape elem)
387 | --transposeToFront (_ :: xs) c @{Here} @{allNap} = ?transposeToFront_rhs_0
388 | --transposeToFront (y :: xs) c @{(There x)} @{allNap} = ?transposeToFront_rhs_1
390 | ||| Functionality for transforming a tensor into a hancock tensor
400 | ||| Helper to compute the unique shape of Tensor when all containers are Naperian
409 | ||| Analogous to `naperianPosEq` but for the HancockTensor structure
410 | ||| We can't use `naperianPosEq` directly because the shape of the resulting
411 | ||| container is not Unit, it is only isomorphic to it
420 | hancockTensorPosEq
422 | ||| Tensor shape is isomorphic to HancockTensor shape when all containers in
423 | ||| the shape are Naperian. This is one arrow of that isomorphism
447 | -- ||| Technically this is Unit, but hard to prove
448 | -- public export
449 | -- foldOverNaperianShapeComp : {shape : List Cont} ->
450 | -- (allNap : All IsNaperian shape) =>
451 | -- (Tensor shape).Shp
452 | -- foldOverNaperianShapeComp {shape = []} = ()
453 | -- foldOverNaperianShapeComp {allNap = ((MkIsNaperian pos) :: ns)}
454 | -- = () <| \_ => foldOverNaperianShapeComp
455 | --
456 | -- public export
457 | -- naperianHancockShape : {shape : List Cont} ->
458 | -- (allNap : All IsNaperian shape) =>
459 | -- (HancockTensor shape).Shp = Unit
460 | -- naperianHancockShape = believe_me ()
461 | --
462 | -- public export
463 | -- foldOverNaperianShapeHancock : {shape : List Cont} ->
464 | -- (allNap : All IsNaperian shape) =>
465 | -- (HancockTensor shape).Shp
466 | -- foldOverNaperianShapeHancock {shape = []} = ()
467 | -- foldOverNaperianShapeHancock {allNap = ((MkIsNaperian _) :: _)}
468 | -- = ((), foldOverNaperianShapeHancock)
471 | -- public export
472 | -- tensorIsNaperianShape : {shape : List Cont} ->
473 | -- (allNap : All IsNaperian shape) =>
474 | -- IsNaperian (Tensor shape)
475 | -- tensorIsNaperianShape {shape = []} = MkIsNaperian ()
476 | -- tensorIsNaperianShape {shape = (_ :: ss), allNap = ((MkIsNaperian pos) :: ns)}
477 | -- = let tg = tensorIsNaperianShape {shape = ss}
478 | -- in ?tensorIsNaperianShape_rhs_1
479 | -- --in rewrite naperianShpEq @{tg}
480 | -- --in (rewrite (EmptyExtEq {c=(Nap pos)})
481 | -- --in let tg = MkIsNaperian in ?tensorIsNaperianShape_rhs_2)
483 | -- public export
484 | -- transformToHancock : {shape : List Cont} ->
485 | -- All IsNaperian shape =>
486 | -- Tensor shape =%> HancockTensor shape
487 | -- transformToHancock {shape = []} = id
488 | -- transformToHancock {shape = (_ :: ss)} @{((MkIsNaperian pos) :: ns)}
489 | -- = let f = (%!) (transformToHancock {shape = ss} @{ns})
490 | -- (_ ** h) = f (foldOverNaperianShapeComp {shape=ss})
491 | -- in !% \(() <| content) => (((), foldOverNaperianShapeHancock) **
492 | -- \(p, fld) => (p ** ?hhh))
493 | -- -- (((), rewrite -- foldOverNaperianShapeHancock {shape=ss} @{ns} in ()) **
494 | -- -- \(p, fld) => (p ** ?bnn))
496 | -- need to organise this
523 | ||| Traverses a binary tree container in order, producing a list container
528 | -- Need to do some rewriting for preorder
533 | --preorderBinTreeNode (NodeS lt rt) n with (strengthenN {m=numNodes lt} n)
534 | -- _ | Left p = ?whl
535 | -- _ | Right FZ = ?whn
536 | -- _ | Right (FS g) = ?whr
552 | -- public export
553 | -- traverseLeaf : (x : BinTreeShape) -> FinBinTreeLeaf x -> Fin (numLeaves x)
554 | -- traverseLeaf LeafS Done = FZ
555 | -- traverseLeaf (NodeS lt rt) (GoLeft x) = weakenN (numLeaves rt) (traverseLeaf lt x)
556 | -- traverseLeaf (NodeS lt rt) (GoRight x) = shift (numLeaves lt) (traverseLeaf rt x)
557 | --
574 | -- TODO here maybe need to uncomment during merge?
575 | -- public export
576 | -- selectShape : {cs : Vect k Cont} ->
577 | -- (shapes : All Shp cs) -> (i : Fin k) -> Any Shp cs
578 | -- selectShape (s :: _) FZ = Here s
579 | -- selectShape (_ :: ss) (FS j) = There (selectShape ss j)
580 | --
581 | -- ||| Extract the position from an AnyPos at a given index
582 | -- public export
583 | -- extractPos : {n : Nat} -> {xs : Vect n Cont} ->
584 | -- {shapes : All Shp xs} ->
585 | -- (i : Fin n) ->
586 | -- AnyShpPos (selectShape shapes i) ->
587 | -- AnyPos shapes
588 | -- extractPos {shapes = (_ :: _)} FZ (Here x) = Here x
589 | -- extractPos {shapes = (_ :: _)} (FS j) (There rest)
590 | -- = There $ extractPos j rest
591 | --
592 | -- public export
593 | -- SampleAndChoose : {n : Nat} -> {xs : Vect n Cont} ->
594 | -- ConvexComb xs =%> (Sample n >@ Any xs)
595 | -- SampleAndChoose = !% \(d, shapes) =>
596 | -- (d <| selectShape shapes ** \(i ** grad) => (0, [extractPos i grad]))
598 | -- SampleAndChooseWithDist = !% \(d, shapes) =>
599 | -- (d <| electShape shapes ** \(i ** grad) => (0, [(i ** extractPos i grad)]))
601 | -- public export
602 | -- GetDist : {n : Nat} -> {xs : Vect n Cont} ->
603 | -- ConvexComb xs =%> Simplex n
604 | -- GetDist = !% \(d, shapes) => (d ** \d' => (d', ?GetDist_rhs))