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
152 | ||| The following is the proof that for any container `c` there is an
153 | ||| isomorphism in `Cont` between `c` and `CartesianClosure UnitCont c`
154 | ||| This holds in any monoidal closed category: `X ≅ [I, X]`
165 | ||| For a overview of this interaction from the categorical perspective, see
166 | ||| the Poly book (https://arxiv.org/abs/2312.00990) (Section 6.3.4)
168 | ||| Interaction between composition and tensor product
169 | ||| Swaps the operations, and middle two containers
170 | ||| Not an isomorphism!
177 | ||| Tensor product embeds into composition
178 | ||| A special case of `duoidal`
185 | ||| Going the other way is impossible without any constraints
186 | ||| Two possibilities on constraints (this, and `compToTensor2`)
199 | ||| Specific distributive law we need
209 | ||| "State" as defined in https://arxiv.org/abs/2403.13001 and open games
210 | |||
211 | ||| ┌─────────────┐
212 | ||| │ ├──► (x : c.Shp)
213 | ||| │ State │
214 | ||| │ ├◄── c.Pos x
215 | ||| └─────────────┘
220 | ||| Given a shape of any container, state can be defined
236 | ||| "Costate" as defined in https://arxiv.org/abs/2403.13001 and open games
237 | |||
238 | ||| ┌─────────────┐
239 | ||| (x : c.Shp) ──►┤ │
240 | ||| │ Costate │
241 | ||| c.Pos x ◄──┤ │
242 | ||| └─────────────┘
275 | ||| Wraps a dependent lens `c =%> d`
276 | ||| into one of type `c >@ Scalar =%> d >@ Scalar`
277 | ||| Needed because `c >@ Scalar` isn't automatically reduced to `c`
290 | ||| Wraps a dependent lens `c =%> d`
291 | ||| into one of type `c >< Scalar =%> d >< Scalar`
292 | ||| Needed because `c >< Scalar` isn't automatically reduced to `c`
299 | ||| Helper function allowing `shape` in `cubicalShape` to have zero annotation
305 | ||| Given a list of cubical containers, return the list of their dimensions
310 | ||| Size of a list of cubical containers is the product of their dimensions
315 | ||| Layout-aware dependent lens flattening a cubical tensor
329 | ||| Layout-aware dependent lens unflattening a tensor
342 | ||| This is simply a rewrite!
350 | ||| Reshapes a cubical tensor by first flattening it to a linear representation,
351 | ||| casting the type to the new shape, and then unflattening it back
352 | ||| Is generic over layout order
375 | -- ||| experiment, does this work?
376 | -- public export
377 | -- transposeMiddle : IsNaperian c => IsNaperian e =>
378 | -- Tensor [c, e, d] =%>
381 | --||| Transpose a given element to the front of the shape
382 | --public export
383 | --transposeToFront : (shape : List Cont) ->
384 | -- (c : Cont) ->
385 | -- (elem : Elem c shape) =>
386 | -- All IsNaperian (dropAfterElem shape elem) =>
387 | -- Tensor shape =%> Tensor (c :: dropElem shape elem)
388 | --transposeToFront (_ :: xs) c @{Here} @{allNap} = ?transposeToFront_rhs_0
389 | --transposeToFront (y :: xs) c @{(There x)} @{allNap} = ?transposeToFront_rhs_1
391 | ||| Functionality for transforming a tensor into a hancock tensor
401 | ||| Helper to compute the unique shape of Tensor when all containers are Naperian
410 | ||| Analogous to `naperianPosEq` but for the HancockTensor structure
411 | ||| We can't use `naperianPosEq` directly because the shape of the resulting
412 | ||| container is not Unit, it is only isomorphic to it
421 | hancockTensorPosEq
423 | ||| Tensor shape is isomorphic to HancockTensor shape when all containers in
424 | ||| the shape are Naperian. This is one arrow of that isomorphism
448 | -- ||| Technically this is Unit, but hard to prove
449 | -- public export
450 | -- foldOverNaperianShapeComp : {shape : List Cont} ->
451 | -- (allNap : All IsNaperian shape) =>
452 | -- (Tensor shape).Shp
453 | -- foldOverNaperianShapeComp {shape = []} = ()
454 | -- foldOverNaperianShapeComp {allNap = ((MkIsNaperian pos) :: ns)}
455 | -- = () <| \_ => foldOverNaperianShapeComp
456 | --
457 | -- public export
458 | -- naperianHancockShape : {shape : List Cont} ->
459 | -- (allNap : All IsNaperian shape) =>
460 | -- (HancockTensor shape).Shp = Unit
461 | -- naperianHancockShape = believe_me ()
462 | --
463 | -- public export
464 | -- foldOverNaperianShapeHancock : {shape : List Cont} ->
465 | -- (allNap : All IsNaperian shape) =>
466 | -- (HancockTensor shape).Shp
467 | -- foldOverNaperianShapeHancock {shape = []} = ()
468 | -- foldOverNaperianShapeHancock {allNap = ((MkIsNaperian _) :: _)}
469 | -- = ((), foldOverNaperianShapeHancock)
472 | -- public export
473 | -- tensorIsNaperianShape : {shape : List Cont} ->
474 | -- (allNap : All IsNaperian shape) =>
475 | -- IsNaperian (Tensor shape)
476 | -- tensorIsNaperianShape {shape = []} = MkIsNaperian ()
477 | -- tensorIsNaperianShape {shape = (_ :: ss), allNap = ((MkIsNaperian pos) :: ns)}
478 | -- = let tg = tensorIsNaperianShape {shape = ss}
479 | -- in ?tensorIsNaperianShape_rhs_1
480 | -- --in rewrite naperianShpEq @{tg}
481 | -- --in (rewrite (EmptyExtEq {c=(Nap pos)})
482 | -- --in let tg = MkIsNaperian in ?tensorIsNaperianShape_rhs_2)
484 | -- public export
485 | -- transformToHancock : {shape : List Cont} ->
486 | -- All IsNaperian shape =>
487 | -- Tensor shape =%> HancockTensor shape
488 | -- transformToHancock {shape = []} = id
489 | -- transformToHancock {shape = (_ :: ss)} @{((MkIsNaperian pos) :: ns)}
490 | -- = let f = (%!) (transformToHancock {shape = ss} @{ns})
491 | -- (_ ** h) = f (foldOverNaperianShapeComp {shape=ss})
492 | -- in !% \(() <| content) => (((), foldOverNaperianShapeHancock) **
493 | -- \(p, fld) => (p ** ?hhh))
494 | -- -- (((), rewrite -- foldOverNaperianShapeHancock {shape=ss} @{ns} in ()) **
495 | -- -- \(p, fld) => (p ** ?bnn))
497 | -- need to organise this
524 | ||| Traverses a binary tree container in order, producing a list container
529 | -- Need to do some rewriting for preorder
534 | --preorderBinTreeNode (NodeS lt rt) n with (strengthenN {m=numNodes lt} n)
535 | -- _ | Left p = ?whl
536 | -- _ | Right FZ = ?whn
537 | -- _ | Right (FS g) = ?whr
553 | -- public export
554 | -- traverseLeaf : (x : BinTreeShape) -> FinBinTreeLeaf x -> Fin (numLeaves x)
555 | -- traverseLeaf LeafS Done = FZ
556 | -- traverseLeaf (NodeS lt rt) (GoLeft x) = weakenN (numLeaves rt) (traverseLeaf lt x)
557 | -- traverseLeaf (NodeS lt rt) (GoRight x) = shift (numLeaves lt) (traverseLeaf rt x)
558 | --
575 | -- TODO here maybe need to uncomment during merge?
576 | -- public export
577 | -- selectShape : {cs : Vect k Cont} ->
578 | -- (shapes : All Shp cs) -> (i : Fin k) -> Any Shp cs
579 | -- selectShape (s :: _) FZ = Here s
580 | -- selectShape (_ :: ss) (FS j) = There (selectShape ss j)
581 | --
582 | -- ||| Extract the position from an AnyPos at a given index
583 | -- public export
584 | -- extractPos : {n : Nat} -> {xs : Vect n Cont} ->
585 | -- {shapes : All Shp xs} ->
586 | -- (i : Fin n) ->
587 | -- AnyShpPos (selectShape shapes i) ->
588 | -- AnyPos shapes
589 | -- extractPos {shapes = (_ :: _)} FZ (Here x) = Here x
590 | -- extractPos {shapes = (_ :: _)} (FS j) (There rest)
591 | -- = There $ extractPos j rest
592 | --
593 | -- public export
594 | -- SampleAndChoose : {n : Nat} -> {xs : Vect n Cont} ->
595 | -- ConvexComb xs =%> (Sample n >@ Any xs)
596 | -- SampleAndChoose = !% \(d, shapes) =>
597 | -- (d <| selectShape shapes ** \(i ** grad) => (0, [extractPos i grad]))
599 | -- SampleAndChooseWithDist = !% \(d, shapes) =>
600 | -- (d <| electShape shapes ** \(i ** grad) => (0, [(i ** extractPos i grad)]))
602 | -- public export
603 | -- GetDist : {n : Nat} -> {xs : Vect n Cont} ->
604 | -- ConvexComb xs =%> Simplex n
605 | -- GetDist = !% \(d, shapes) => (d ** \d' => (d', ?GetDist_rhs))