27 | ||| "State" as defined in https://arxiv.org/abs/2403.13001 and open games
28 | |||
29 | ||| ┌─────────────┐
30 | ||| │ ├──► (x : c.Shp)
31 | ||| │ State │
32 | ||| │ ├◄── c.Pos x
33 | ||| └─────────────┘
38 | ||| Given a shape of any container, state can be defined
54 | ||| "Costate" as defined in https://arxiv.org/abs/2403.13001 and open games
55 | |||
56 | ||| ┌─────────────┐
57 | ||| (x : c.Shp) ──►┤ │
58 | ||| │ Costate │
59 | ||| c.Pos x ◄──┤ │
60 | ||| └─────────────┘
87 | ||| If we model the idea of a container (S !> P) as a box
88 | ||| ┌──────┐
89 | ||| │ s:S │
90 | ||| ├──────┤
91 | ||| │ Ps │
92 | ||| └──────┘
93 | ||| then `pushDown` is interpreted as pushing down the container,
94 | ||| pruning anything that goes out of the box, and using `Unit` for
95 | ||| anything new that appears:
96 | ||| ┌──────┐
97 | ||| │ Unit │
98 | ||| ├──────┤
99 | ||| │ s:S │
100 | ||| └──────┘
101 | ||| Ps
201 | ||| The following is the proof that for any container `c` there is an
202 | ||| isomorphism in `Cont` between `c` and `CartesianClosure UnitCont c`
203 | ||| This holds in any monoidal closed category: `X ≅ [I, X]`
214 | ||| For a overview of this interaction from the categorical perspective, see
215 | ||| the Poly book (https://arxiv.org/abs/2312.00990) (Section 6.3.4)
217 | ||| Interaction between composition and tensor product
218 | ||| Swaps the operations, and middle two containers
219 | ||| Not an isomorphism!
226 | ||| Tensor product embeds into composition
227 | ||| A special case of `duoidal`
234 | ||| Going the other way is impossible without any constraints
235 | ||| Two possibilities on constraints (this, and `compToTensor2`)
248 | ||| Specific distributive law we need
257 | ||| Wraps a dependent lens `c =%> d`
258 | ||| into one of type `c >@ Scalar =%> d >@ Scalar`
259 | ||| Needed because `c >@ Scalar` isn't automatically reduced to `c`
272 | ||| Wraps a dependent lens `c =%> d`
273 | ||| into one of type `c >< Scalar =%> d >< Scalar`
274 | ||| Needed because `c >< Scalar` isn't automatically reduced to `c`
281 | ||| Helper function allowing `shape` in `cubicalShape` to have zero annotation
287 | ||| Given a list of cubical containers, return the list of their dimensions
292 | ||| Size of a list of cubical containers is the product of their dimensions
297 | ||| Layout-aware dependent lens flattening a cubical tensor
311 | ||| Layout-aware dependent lens unflattening a tensor
324 | ||| This is simply a rewrite!
332 | ||| Reshapes a cubical tensor by first flattening it to a linear representation,
333 | ||| casting the type to the new shape, and then unflattening it back
334 | ||| Is generic over layout order
357 | -- ||| experiment, does this work?
358 | -- public export
359 | -- transposeMiddle : IsNaperian c => IsNaperian e =>
360 | -- Tensor [c, e, d] =%>
363 | --||| Transpose a given element to the front of the shape
364 | --public export
365 | --transposeToFront : (shape : List Cont) ->
366 | -- (c : Cont) ->
367 | -- (elem : Elem c shape) =>
368 | -- All IsNaperian (dropAfterElem shape elem) =>
369 | -- Tensor shape =%> Tensor (c :: dropElem shape elem)
370 | --transposeToFront (_ :: xs) c @{Here} @{allNap} = ?transposeToFront_rhs_0
371 | --transposeToFront (y :: xs) c @{(There x)} @{allNap} = ?transposeToFront_rhs_1
373 | ||| Functionality for transforming a tensor into a hancock tensor
383 | ||| Helper to compute the unique shape of Tensor when all containers are Naperian
392 | ||| Analogous to `naperianPosEq` but for the HancockTensor structure
393 | ||| We can't use `naperianPosEq` directly because the shape of the resulting
394 | ||| container is not Unit, it is only isomorphic to it
403 | hancockTensorPosEq
405 | ||| Tensor shape is isomorphic to HancockTensor shape when all containers in
406 | ||| the shape are Naperian. This is one arrow of that isomorphism
430 | -- ||| Technically this is Unit, but hard to prove
431 | -- public export
432 | -- foldOverNaperianShapeComp : {shape : List Cont} ->
433 | -- (allNap : All IsNaperian shape) =>
434 | -- (Tensor shape).Shp
435 | -- foldOverNaperianShapeComp {shape = []} = ()
436 | -- foldOverNaperianShapeComp {allNap = ((MkIsNaperian pos) :: ns)}
437 | -- = () <| \_ => foldOverNaperianShapeComp
438 | --
439 | -- public export
440 | -- naperianHancockShape : {shape : List Cont} ->
441 | -- (allNap : All IsNaperian shape) =>
442 | -- (HancockTensor shape).Shp = Unit
443 | -- naperianHancockShape = believe_me ()
444 | --
445 | -- public export
446 | -- foldOverNaperianShapeHancock : {shape : List Cont} ->
447 | -- (allNap : All IsNaperian shape) =>
448 | -- (HancockTensor shape).Shp
449 | -- foldOverNaperianShapeHancock {shape = []} = ()
450 | -- foldOverNaperianShapeHancock {allNap = ((MkIsNaperian _) :: _)}
451 | -- = ((), foldOverNaperianShapeHancock)
454 | -- public export
455 | -- tensorIsNaperianShape : {shape : List Cont} ->
456 | -- (allNap : All IsNaperian shape) =>
457 | -- IsNaperian (Tensor shape)
458 | -- tensorIsNaperianShape {shape = []} = MkIsNaperian ()
459 | -- tensorIsNaperianShape {shape = (_ :: ss), allNap = ((MkIsNaperian pos) :: ns)}
460 | -- = let tg = tensorIsNaperianShape {shape = ss}
461 | -- in ?tensorIsNaperianShape_rhs_1
462 | -- --in rewrite naperianShpEq @{tg}
463 | -- --in (rewrite (EmptyExtEq {c=(Nap pos)})
464 | -- --in let tg = MkIsNaperian in ?tensorIsNaperianShape_rhs_2)
466 | -- public export
467 | -- transformToHancock : {shape : List Cont} ->
468 | -- All IsNaperian shape =>
469 | -- Tensor shape =%> HancockTensor shape
470 | -- transformToHancock {shape = []} = id
471 | -- transformToHancock {shape = (_ :: ss)} @{((MkIsNaperian pos) :: ns)}
472 | -- = let f = (%!) (transformToHancock {shape = ss} @{ns})
473 | -- (_ ** h) = f (foldOverNaperianShapeComp {shape=ss})
474 | -- in !% \(() <| content) => (((), foldOverNaperianShapeHancock) **
475 | -- \(p, fld) => (p ** ?hhh))
476 | -- -- (((), rewrite -- foldOverNaperianShapeHancock {shape=ss} @{ns} in ()) **
477 | -- -- \(p, fld) => (p ** ?bnn))
479 | -- need to organise this
506 | ||| Traverses a binary tree container in order, producing a list container
511 | -- Need to do some rewriting for preorder
516 | --preorderBinTreeNode (NodeS lt rt) n with (strengthenN {m=numNodes lt} n)
517 | -- _ | Left p = ?whl
518 | -- _ | Right FZ = ?whn
519 | -- _ | Right (FS g) = ?whr
535 | -- public export
536 | -- traverseLeaf : (x : BinTreeShape) -> FinBinTreeLeaf x -> Fin (numLeaves x)
537 | -- traverseLeaf LeafS Done = FZ
538 | -- traverseLeaf (NodeS lt rt) (GoLeft x) = weakenN (numLeaves rt) (traverseLeaf lt x)
539 | -- traverseLeaf (NodeS lt rt) (GoRight x) = shift (numLeaves lt) (traverseLeaf rt x)
540 | --
557 | -- TODO here maybe need to uncomment during merge?
558 | -- public export
559 | -- selectShape : {cs : Vect k Cont} ->
560 | -- (shapes : All Shp cs) -> (i : Fin k) -> Any Shp cs
561 | -- selectShape (s :: _) FZ = Here s
562 | -- selectShape (_ :: ss) (FS j) = There (selectShape ss j)
563 | --
564 | -- ||| Extract the position from an AnyPos at a given index
565 | -- public export
566 | -- extractPos : {n : Nat} -> {xs : Vect n Cont} ->
567 | -- {shapes : All Shp xs} ->
568 | -- (i : Fin n) ->
569 | -- AnyShpPos (selectShape shapes i) ->
570 | -- AnyPos shapes
571 | -- extractPos {shapes = (_ :: _)} FZ (Here x) = Here x
572 | -- extractPos {shapes = (_ :: _)} (FS j) (There rest)
573 | -- = There $ extractPos j rest
574 | --
575 | -- public export
576 | -- SampleAndChoose : {n : Nat} -> {xs : Vect n Cont} ->
577 | -- ConvexComb xs =%> (Sample n >@ Any xs)
578 | -- SampleAndChoose = !% \(d, shapes) =>
579 | -- (d <| selectShape shapes ** \(i ** grad) => (0, [extractPos i grad]))
581 | -- SampleAndChooseWithDist = !% \(d, shapes) =>
582 | -- (d <| electShape shapes ** \(i ** grad) => (0, [(i ** extractPos i grad)]))
584 | -- public export
585 | -- GetDist : {n : Nat} -> {xs : Vect n Cont} ->
586 | -- ConvexComb xs =%> Simplex n
587 | -- GetDist = !% \(d, shapes) => (d ** \d' => (d', ?GetDist_rhs))