Zulip Chat Archive
Stream: lean4
Topic: Self type?
Alok Singh (Mar 11 2024 at 09:33):
I'm defining a QuadTree, and ran into this issue: how do I refer to a type in its fields?
structure Point where
x : Float
y : Float
structure Body where
pos : Point
mass : Float
structure QuadTree where
center : Point
size : Float
totalMass : Float
centerOfMass : Point
bodies : List Body
NW : Option QuadTree
NE : Option QuadTree
SW : Option QuadTree
SE : Option QuadTree
here the last 4 fields
Eric Wieser (Mar 11 2024 at 09:37):
This works:
inductive QuadTree
| mk
(center : Point)
(size : Float)
(totalMass : Float)
(centerOfMass : Point)
(bodies : List Body)
(NW : Option QuadTree)
(NE : Option QuadTree)
(SW : Option QuadTree)
(SE : Option QuadTree)
Eric Wieser (Mar 11 2024 at 09:38):
But then you have to write all the projections by hand
James Gallicchio (Mar 13 2024 at 08:37):
In my head this is one of the places where QPFs could help us metaprogram our way to victory -- but I am not confident in that claim or how much work it would take to get there...
Eric Wieser (Mar 13 2024 at 09:57):
It looks to me like this is just a missing feature in the structure
command? Unless I'm missing something, I would assume all the same projections could be generated
Eric Wieser (Mar 13 2024 at 09:58):
Maybe the issue here is structure eta, which is no longer possible?
Mario Carneiro (Mar 13 2024 at 10:01):
There is an exploit: Although you can't define recursive structures, you can get structures to appear in nested inductive types
Mario Carneiro (Mar 13 2024 at 10:03):
structure QuadTree' (A : Type) where
center : Point
size : Float
totalMass : Float
centerOfMass : Point
bodies : List Body
NW : Option A
NE : Option A
SW : Option A
SE : Option A
inductive QuadTree where
| mk : QuadTree' QuadTree → QuadTree
Kyle Miller (Mar 13 2024 at 18:08):
Eric Wieser said:
It looks to me like this is just a missing feature in the
structure
command?
I think we'll see recursive structures at some point, but it's not among the highest priority features at the moment.
(What's the issue with structure eta for recursive structures? I haven't thought at all about how recursive structures would interact with defeq.)
Eric Wieser (Mar 13 2024 at 22:17):
My limited understanding was that structure eta just greedily expands things, which no longer works if structures are recursive
Eric Wieser (Mar 13 2024 at 22:49):
(I said "missing feature" and not "bug" precisely because I think this is low priority)
Kyle Miller (Mar 14 2024 at 01:37):
Oops, I meant to be more explicit: recursive structures have a non-zero priority for the FRO, with a priority inversely proportional to implementation difficulty :smile:
My rough understanding, having not looked into it too deeply, is that for elaboration the command needs to be able to support it, and ideally it would support mutual
blocks too. It'd be good to know if there are any kernel issues to be aware of, but looking at the kernel source, I don't think so, because the kernel sees any non-recursive one-constructor index-free inductive type as being a "structure" (is_structure_like
). I don't know if the elaborator's type checker works with structure
s or "structures".
Alok Singh (Aug 13 2024 at 22:06):
this came up again but with typeclass constraints:
inductive Value
| mk
(data : Float)
(grad : Unit → Float)
(_backward : Unit → Option Float := fun () ↦ .none)
(_children : Lean.HashSet Value := .empty)
(_op : String := "")
from karpathy's micrograd in python. the hashset is the problem, it demands BEq Value
(and hashable for that matter). but defining beq after won't work and I can't use deriving. _children
should form a DAG since it's supposed to be a computation graph
Sebastian Ullrich (Aug 14 2024 at 08:56):
You should take a look at the new Std.HashSet.Raw
https://github.com/leanprover/lean4/blob/3efd0e4e1feae93e0fb5956492a2e32b2aa14821/src/Std/Data/HashSet/Raw.lean#L35-L36
Last updated: May 02 2025 at 03:31 UTC