Zulip Chat Archive

Stream: new members

Topic: is there a way to do lazy computation in lean


Shubham Kumar 🦀 (he/him) (Apr 15 2024 at 03:54):

same as the title, I was looking at Json serializer and it use lazy

public export
orElse : Either a b -> Lazy (Either a b) -> Either a b
orElse r@(Right _) _ = r
orElse _           v = v

public export
(<|>) : Parser v a -> Parser v a -> Parser v a
f <|> g = \vv => f vv `orElse` g vv

is there something similar in lean?

Kyle Miller (Apr 15 2024 at 03:55):

docs#Thunk

Shubham Kumar 🦀 (he/him) (Apr 15 2024 at 03:55):

Ah thanks!!

Kyle Miller (Apr 15 2024 at 03:56):

Though that's if you want to save the result. If you just want a one-shot lazy value, you may as well use Unit -> a

Shubham Kumar 🦀 (he/him) (Apr 15 2024 at 03:56):

Kyle Miller said:

Though that's if you want to save the result. If you just want a one-shot lazy value, you may as well use Unit -> a

ok so it's similar to ocaml, got it

Kyle Miller (Apr 15 2024 at 03:56):

docs#HOrElse.hOrElse uses that

Shubham Kumar 🦀 (he/him) (Apr 15 2024 at 04:15):

this is giving me error

instance : HOrElse.hOrElse (Either α β) (Thunk.pure (Either α β)) where

It seems right to me but the compiler doesn't

Shubham Kumar 🦀 (he/him) (Apr 15 2024 at 04:16):

error: application type mismatch
  HOrElse.hOrElse (Either α β) (Thunk.pure (Either α β))
argument
  Thunk.pure (Either α β)
has type
  Thunk Type : Type 1
but is expected to have type
  Unit  ?m.1635 : Type ?u.1632

Shubham Kumar 🦀 (he/him) (Apr 15 2024 at 05:51):

created an either type and implemented hOrElse

def hOrElse
  {α β : Type}
  {a : Either α β}
  {b : Thunk (Either α β)}
  {c : Either α β}
  (e₁ : a) (f : b) : c :=
  match e₁, f with
  | Either.Right r, _ => Either.Right r
  | _, v => v

but I am getting an error where a has to be a type but is Either a b isn't Either a type if I have defined it as an inductive type

inductive Either (α β : Type) : Type
  | Left  : α -> Either α β
  | Right : β -> Either α β

Eric Wieser (Apr 15 2024 at 07:28):

Either is builtin, it's docs#Sum

Shubham Kumar 🦀 (he/him) (Apr 15 2024 at 07:29):

Oh ok, sorry. Thanks for the help :pray:

Eric Wieser (Apr 15 2024 at 07:29):

Shubham Kumar 🦀 (he/him) said:

I am getting an error where a has to be a type but is Either a b isn't Either a type

The error doesn't say "Either isn't a type", it says "a isn't a type".

Eric Wieser (Apr 15 2024 at 07:30):

Nat is a type and 37 is a Nat but 37 is not a type

Eric Wieser (Apr 15 2024 at 07:32):

Though give the orElse semantics you want, you might want docs#Except instead of Sum

Kyle Miller (Apr 15 2024 at 20:05):

Shubham Kumar 🦀 (he/him) said:

this is giving me error

instance : HOrElse.hOrElse (Either α β) (Thunk.pure (Either α β)) where

It seems right to me but the compiler doesn't

Here's a hint: move your mouse over HOrElse.hOrElse. What's its type? Is it actually a typeclass?


Last updated: May 02 2025 at 03:31 UTC