Zulip Chat Archive
Stream: new members
Topic: Don't understand imports and opens?
aron (Mar 03 2025 at 11:28):
After a lot of trial and error I figured out that I can import the HashMap
data structure by importing Std.Data.HashMap.Basic
and referencing it like Std.HashMap
:
import Std.Data.HashMap.Basic
abbrev MyMap := Std.HashMap String Nat
and I can avoid the Std
prefix by open
ing Std
:
import Std.Data.HashMap.Basic
open Std
abbrev MyMap := HashMap String Nat
-
But why is
HashMap
in theStd
namespace when it's defined inStd.Data.HashMap.Basic
? -
It looks like I can skip the
Basic
in the import path. Is that becauselean/Std/Data/HashMap.lean
importsStd.Data.HashMap.Basic
? Does that mean that importingStd.Data.HashMap
includes everythingHashMap.lean
imports, i.e. it works sort of like an include? -
Even if that's the case, I still don't get how
import Std
works, because there is nolean/Std.lean
file, so what module does that import actually reference?
Markus Himmel (Mar 03 2025 at 11:30):
- There is no one-to-one correspondence between namespaces and files.
- Yes, imports are transitive.
- There is in fact such a file: https://github.com/leanprover/lean4/blob/master/src/Std.lean
Damiano Testa (Mar 03 2025 at 12:06):
Also, import
gives you access to the declarations in a given file, regardless of their namespace. open
allows you to refer to the available declarations by shorter names depending on what you opened. The two mechanisms are independent of each other, although modules and namespaces tend to have similar names, since those are often chosen semantically.
aron (Mar 03 2025 at 12:16):
@Markus Himmel:
- so when I import a module I'm referring to a file location, not a namespace? but hm yeah I can see now that
lean/Std/Data/HashMap/Basic.lean
type declares theHashMap
type as being innamespace Std
- ok :+1:
- oh yeah you're right :man_facepalming: I was looking at the wrong location (inside the
lean/Std/
directory instead of insidelean/
)
aron (Mar 03 2025 at 12:20):
I'll be honest I find it very confusing that you import based on file location but you reference a type/value based on namespace. That seems inconsistent.
In the .NET world for example if you want to import a type, you import and qualify the type name by the same thing: its full module name (which is namespace based)
Markus Himmel (Mar 03 2025 at 12:49):
aron said:
- so when I import a module I'm referring to a file location, not a namespace?
Yes, this is correct. I would say this makes sense in Lean because we must make sure that there is some notion of "before" such that every declaration only refers to declarations that came before it, to prevent introducing logical inconsistencies through circular reasoning. A natural way to ensure this is by having files import each other and forbidding cyclic imports.
Last updated: May 02 2025 at 03:31 UTC