27 | ||| "State" as defined in https://arxiv.org/abs/2403.13001 and open games
28 | ||| Given a shape of any container, state can be defined
146 | ||| The following is the proof that for any container `c` there is an
147 | ||| isomorphism in `Cont` between `c` and `CartesianClosure UnitCont c`
148 | ||| This holds in any monoidal closed category: `X ≅ [I, X]`
159 | ||| For a overview of this interaction from the categorical perspective, see
160 | ||| the Poly book (https://arxiv.org/abs/2312.00990) (Section 6.3.4)
162 | ||| Interaction between composition and tensor product
163 | ||| Swaps the operations, and middle two containers
164 | ||| Not an isomorphism!
171 | ||| Tensor product embeds into composition
172 | ||| A special case of `duoidal`
179 | ||| Going the other way is impossible without any constraints
180 | ||| Two possibilities on constraints (this, and `compToTensor2`)
193 | ||| Specific distributive law we need
202 | ||| Wraps a dependent lens `c =%> d`
203 | ||| into one of type `c >@ Scalar =%> d >@ Scalar`
204 | ||| Needed because `c >@ Scalar` isn't automatically reduced to `c`
217 | ||| Wraps a dependent lens `c =%> d`
218 | ||| into one of type `c >< Scalar =%> d >< Scalar`
219 | ||| Needed because `c >< Scalar` isn't automatically reduced to `c`
226 | ||| Helper function allowing `shape` in `cubicalShape` to have zero annotation
232 | ||| Given a list of cubical containers, return the list of their dimensions
237 | ||| Size of a list of cubical containers is the product of their dimensions
242 | ||| Layout-aware dependent lens flattening a cubical tensor
256 | ||| Layout-aware dependent lens unflattening a tensor
269 | ||| This is simply a rewrite!
277 | ||| Reshapes a cubical tensor by first flattening it to a linear representation,
278 | ||| casting the type to the new shape, and then unflattening it back
279 | ||| Is generic over layout order
302 | -- ||| experiment, does this work?
303 | -- public export
304 | -- transposeMiddle : IsNaperian c => IsNaperian e =>
305 | -- Tensor [c, e, d] =%>
308 | --||| Transpose a given element to the front of the shape
309 | --public export
310 | --transposeToFront : (shape : List Cont) ->
311 | -- (c : Cont) ->
312 | -- (elem : Elem c shape) =>
313 | -- All IsNaperian (dropAfterElem shape elem) =>
314 | -- Tensor shape =%> Tensor (c :: dropElem shape elem)
315 | --transposeToFront (_ :: xs) c @{Here} @{allNap} = ?transposeToFront_rhs_0
316 | --transposeToFront (y :: xs) c @{(There x)} @{allNap} = ?transposeToFront_rhs_1
318 | ||| Functionality for transforming a tensor into a hancock tensor
328 | ||| Helper to compute the unique shape of Tensor when all containers are Naperian
337 | ||| Analogous to `naperianPosEq` but for the HancockTensor structure
338 | ||| We can't use `naperianPosEq` directly because the shape of the resulting
339 | ||| container is not Unit, it is only isomorphic to it
348 | hancockTensorPosEq
350 | ||| Tensor shape is isomorphic to HancockTensor shape when all containers in
351 | ||| the shape are Naperian. This is one arrow of that isomorphism
375 | -- ||| Technically this is Unit, but hard to prove
376 | -- public export
377 | -- foldOverNaperianShapeComp : {shape : List Cont} ->
378 | -- (allNap : All IsNaperian shape) =>
379 | -- (Tensor shape).Shp
380 | -- foldOverNaperianShapeComp {shape = []} = ()
381 | -- foldOverNaperianShapeComp {allNap = ((MkIsNaperian pos) :: ns)}
382 | -- = () <| \_ => foldOverNaperianShapeComp
383 | --
384 | -- public export
385 | -- naperianHancockShape : {shape : List Cont} ->
386 | -- (allNap : All IsNaperian shape) =>
387 | -- (HancockTensor shape).Shp = Unit
388 | -- naperianHancockShape = believe_me ()
389 | --
390 | -- public export
391 | -- foldOverNaperianShapeHancock : {shape : List Cont} ->
392 | -- (allNap : All IsNaperian shape) =>
393 | -- (HancockTensor shape).Shp
394 | -- foldOverNaperianShapeHancock {shape = []} = ()
395 | -- foldOverNaperianShapeHancock {allNap = ((MkIsNaperian _) :: _)}
396 | -- = ((), foldOverNaperianShapeHancock)
399 | -- public export
400 | -- tensorIsNaperianShape : {shape : List Cont} ->
401 | -- (allNap : All IsNaperian shape) =>
402 | -- IsNaperian (Tensor shape)
403 | -- tensorIsNaperianShape {shape = []} = MkIsNaperian ()
404 | -- tensorIsNaperianShape {shape = (_ :: ss), allNap = ((MkIsNaperian pos) :: ns)}
405 | -- = let tg = tensorIsNaperianShape {shape = ss}
406 | -- in ?tensorIsNaperianShape_rhs_1
407 | -- --in rewrite naperianShpEq @{tg}
408 | -- --in (rewrite (EmptyExtEq {c=(Nap pos)})
409 | -- --in let tg = MkIsNaperian in ?tensorIsNaperianShape_rhs_2)
411 | -- public export
412 | -- transformToHancock : {shape : List Cont} ->
413 | -- All IsNaperian shape =>
414 | -- Tensor shape =%> HancockTensor shape
415 | -- transformToHancock {shape = []} = id
416 | -- transformToHancock {shape = (_ :: ss)} @{((MkIsNaperian pos) :: ns)}
417 | -- = let f = (%!) (transformToHancock {shape = ss} @{ns})
418 | -- (_ ** h) = f (foldOverNaperianShapeComp {shape=ss})
419 | -- in !% \(() <| content) => (((), foldOverNaperianShapeHancock) **
420 | -- \(p, fld) => (p ** ?hhh))
421 | -- -- (((), rewrite -- foldOverNaperianShapeHancock {shape=ss} @{ns} in ()) **
422 | -- -- \(p, fld) => (p ** ?bnn))
424 | -- need to organise this
451 | ||| Traverses a binary tree container in order, producing a list container
456 | -- Need to do some rewriting for preorder
461 | --preorderBinTreeNode (NodeS lt rt) n with (strengthenN {m=numNodes lt} n)
462 | -- _ | Left p = ?whl
463 | -- _ | Right FZ = ?whn
464 | -- _ | Right (FS g) = ?whr
480 | -- public export
481 | -- traverseLeaf : (x : BinTreeShape) -> FinBinTreeLeaf x -> Fin (numLeaves x)
482 | -- traverseLeaf LeafS Done = FZ
483 | -- traverseLeaf (NodeS lt rt) (GoLeft x) = weakenN (numLeaves rt) (traverseLeaf lt x)
484 | -- traverseLeaf (NodeS lt rt) (GoRight x) = shift (numLeaves lt) (traverseLeaf rt x)
485 | --
502 | -- TODO here maybe need to uncomment during merge?
503 | -- public export
504 | -- selectShape : {cs : Vect k Cont} ->
505 | -- (shapes : All Shp cs) -> (i : Fin k) -> Any Shp cs
506 | -- selectShape (s :: _) FZ = Here s
507 | -- selectShape (_ :: ss) (FS j) = There (selectShape ss j)
508 | --
509 | -- ||| Extract the position from an AnyPos at a given index
510 | -- public export
511 | -- extractPos : {n : Nat} -> {xs : Vect n Cont} ->
512 | -- {shapes : All Shp xs} ->
513 | -- (i : Fin n) ->
514 | -- AnyShpPos (selectShape shapes i) ->
515 | -- AnyPos shapes
516 | -- extractPos {shapes = (_ :: _)} FZ (Here x) = Here x
517 | -- extractPos {shapes = (_ :: _)} (FS j) (There rest)
518 | -- = There $ extractPos j rest
519 | --
520 | -- public export
521 | -- SampleAndChoose : {n : Nat} -> {xs : Vect n Cont} ->
522 | -- ConvexComb xs =%> (Sample n >@ Any xs)
523 | -- SampleAndChoose = !% \(d, shapes) =>
524 | -- (d <| selectShape shapes ** \(i ** grad) => (0, [extractPos i grad]))
526 | -- SampleAndChooseWithDist = !% \(d, shapes) =>
527 | -- (d <| electShape shapes ** \(i ** grad) => (0, [(i ** extractPos i grad)]))
529 | -- public export
530 | -- GetDist : {n : Nat} -> {xs : Vect n Cont} ->
531 | -- ConvexComb xs =%> Simplex n
532 | -- GetDist = !% \(d, shapes) => (d ** \d' => (d', ?GetDist_rhs))