23 | ||| "State" as defined in https://arxiv.org/abs/2403.13001 and open games
24 | ||| Given a shape of any container, state can be defined
132 | ||| Interaction between composition and tensor product
139 | ||| Specific distributive law we need
147 | ||| Ext is a functor of type Cont -> [Type, Type]
148 | ||| On objects it maps a container to a polynomial functor
149 | ||| On morphisms it maps a dependent lens to a natural transformation
150 | ||| This is the action on morphisms
159 | ||| Wraps a dependent lens `c =%> d`
160 | ||| into one of type `c >@ Scalar =%> d >@ Scalar`
161 | ||| Needed because `c >@ Scalar` isn't automatically reduced to `c`
170 | ||| Wraps a dependent lens `c =%> d`
171 | ||| into one of type `c >< Scalar =%> d >< Scalar`
172 | ||| Needed because `c >< Scalar` isn't automatically reduced to `c`
181 | ||| Helper function allowing `shape` in `cubicalShape` to have zero annotation
187 | ||| Given a list of cubical containers, return the list of their dimensions
192 | ||| Size of a list of cubical containers is the product of their dimensions
197 | ||| Layout-aware dependent lens flattening a cubical tensor
211 | ||| Layout-aware dependent lens unflattening a tensor
224 | ||| This is simply a rewrite!
232 | ||| Reshapes a cubical tensor by first flattening it to a linear representation,
233 | ||| casting the type to the new shape, and then unflattening it back
234 | ||| Is generic over layout order
252 | ||| This and the above function should be one and the same, up to rebracketing
260 | ||| Functionality for transforming a tensor into a hancock tensor
270 | ||| Helper to compute the unique shape of Tensor when all containers are Naperian
279 | ||| Analogous to `naperianPosEq` but for the HancockTensor structure
280 | ||| We can't use `naperianPosEq` directly because the shape of the resulting
281 | ||| container is not Unit, it is only isomorphic to it
290 | hancockTensorPosEq
292 | ||| Tensor shape is isomorphic to HancockTensor shape when all containers in
293 | ||| the shape are Naperian. This is one arrow of that isomorphism
312 | -- ||| Technically this is Unit, but hard to prove
313 | -- public export
314 | -- foldOverNaperianShapeComp : {shape : List Cont} ->
315 | -- (allNap : All IsNaperian shape) =>
316 | -- (Tensor shape).Shp
317 | -- foldOverNaperianShapeComp {shape = []} = ()
318 | -- foldOverNaperianShapeComp {allNap = ((MkIsNaperian pos) :: ns)}
319 | -- = () <| \_ => foldOverNaperianShapeComp
320 | --
321 | -- public export
322 | -- naperianHancockShape : {shape : List Cont} ->
323 | -- (allNap : All IsNaperian shape) =>
324 | -- (HancockTensor shape).Shp = Unit
325 | -- naperianHancockShape = believe_me ()
326 | --
327 | -- public export
328 | -- foldOverNaperianShapeHancock : {shape : List Cont} ->
329 | -- (allNap : All IsNaperian shape) =>
330 | -- (HancockTensor shape).Shp
331 | -- foldOverNaperianShapeHancock {shape = []} = ()
332 | -- foldOverNaperianShapeHancock {allNap = ((MkIsNaperian _) :: _)}
333 | -- = ((), foldOverNaperianShapeHancock)
336 | -- public export
337 | -- tensorIsNaperianShape : {shape : List Cont} ->
338 | -- (allNap : All IsNaperian shape) =>
339 | -- IsNaperian (Tensor shape)
340 | -- tensorIsNaperianShape {shape = []} = MkIsNaperian ()
341 | -- tensorIsNaperianShape {shape = (_ :: ss), allNap = ((MkIsNaperian pos) :: ns)}
342 | -- = let tg = tensorIsNaperianShape {shape = ss}
343 | -- in ?tensorIsNaperianShape_rhs_1
344 | -- --in rewrite naperianShpEq @{tg}
345 | -- --in (rewrite (EmptyExtEq {c=(Nap pos)})
346 | -- --in let tg = MkIsNaperian in ?tensorIsNaperianShape_rhs_2)
348 | -- public export
349 | -- transformToHancock : {shape : List Cont} ->
350 | -- All IsNaperian shape =>
351 | -- Tensor shape =%> HancockTensor shape
352 | -- transformToHancock {shape = []} = id
353 | -- transformToHancock {shape = (_ :: ss)} @{((MkIsNaperian pos) :: ns)}
354 | -- = let f = (%!) (transformToHancock {shape = ss} @{ns})
355 | -- (_ ** h) = f (foldOverNaperianShapeComp {shape=ss})
356 | -- in !% \(() <| content) => (((), foldOverNaperianShapeHancock) **
357 | -- \(p, fld) => (p ** ?hhh))
358 | -- -- (((), rewrite -- foldOverNaperianShapeHancock {shape=ss} @{ns} in ()) **
359 | -- -- \(p, fld) => (p ** ?bnn))
361 | -- need to organise this
388 | ||| Traverses a binary tree container in order, producing a list container
393 | -- Need to do some rewriting for preorder
398 | --preorderBinTreeNode (NodeS lt rt) n with (strengthenN {m=numNodes lt} n)
399 | -- _ | Left p = ?whl
400 | -- _ | Right FZ = ?whn
401 | -- _ | Right (FS g) = ?whr
403 | -- public export
404 | -- traverseLeaf : (x : BinTreeShape) -> FinBinTreeLeaf x -> Fin (numLeaves x)
405 | -- traverseLeaf LeafS Done = FZ
406 | -- traverseLeaf (NodeS lt rt) (GoLeft x) = weakenN (numLeaves rt) (traverseLeaf lt x)
407 | -- traverseLeaf (NodeS lt rt) (GoRight x) = shift (numLeaves lt) (traverseLeaf rt x)
408 | --
421 | -- TODO here maybe need to uncomment during merge?
422 | -- public export
423 | -- selectShape : {cs : Vect k Cont} ->
424 | -- (shapes : All Shp cs) -> (i : Fin k) -> Any Shp cs
425 | -- selectShape (s :: _) FZ = Here s
426 | -- selectShape (_ :: ss) (FS j) = There (selectShape ss j)
427 | --
428 | -- ||| Extract the position from an AnyPos at a given index
429 | -- public export
430 | -- extractPos : {n : Nat} -> {xs : Vect n Cont} ->
431 | -- {shapes : All Shp xs} ->
432 | -- (i : Fin n) ->
433 | -- AnyShpPos (selectShape shapes i) ->
434 | -- AnyPos shapes
435 | -- extractPos {shapes = (_ :: _)} FZ (Here x) = Here x
436 | -- extractPos {shapes = (_ :: _)} (FS j) (There rest)
437 | -- = There $ extractPos j rest
438 | --
439 | -- public export
440 | -- SampleAndChoose : {n : Nat} -> {xs : Vect n Cont} ->
441 | -- ConvexComb xs =%> (Sample n >@ Any xs)
442 | -- SampleAndChoose = !% \(d, shapes) =>
443 | -- (d <| selectShape shapes ** \(i ** grad) => (0, [extractPos i grad]))
445 | -- SampleAndChooseWithDist = !% \(d, shapes) =>
446 | -- (d <| electShape shapes ** \(i ** grad) => (0, [(i ** extractPos i grad)]))
448 | -- public export
449 | -- GetDist : {n : Nat} -> {xs : Vect n Cont} ->
450 | -- ConvexComb xs =%> Simplex n
451 | -- GetDist = !% \(d, shapes) => (d ** \d' => (d', ?GetDist_rhs))