Zulip Chat Archive

Stream: lean4

Topic: can you do a safe cast to a subtype in Lean?


Chris Lovett (Sep 03 2022 at 04:12):

Can lean 4 do something like this?

structure Command where
  name : string

structure RunnableCommand extends Command where
  run : (List String) -> IO UInt32

def runIt (cmd : Command) : IO UInt32 :=
  match cmd with
  | RunnableCommand c => c.run []
  | _ => IO.println "Not runnable"
  return 1

Sebastian Ullrich (Sep 03 2022 at 09:22):

You misunderstand inheritance, it is merely a shortcut for structural composition. Lean is not an object-oriented language, it does not have subtypes.

Sebastian Ullrich (Sep 03 2022 at 09:25):

For a constant number of "subclasses" known in advance (a "clossed class" in some languages), we usually model them as different constructors of an inductive data type instead.

Yuri de Wit (Sep 03 2022 at 13:03):

I had a similar question come up when looking into Julia's multiple dispatch in Lean4 using type classes (static vs dynamic aside):

def doSomething(cmd : Command) := ...

#eval doSomething (RunnableCommand.mk  ...)

In theory, a simple automatic coercion could make this work, no?

Sebastian Ullrich (Sep 03 2022 at 13:06):

That's the opposite casting direction, but yes, it can


Last updated: Dec 20 2023 at 11:08 UTC