Zulip Chat Archive

Stream: new members

Topic: Implicit types and optional parameters


Sébastien Boisgérault (Aug 12 2025 at 14:05):

Hi everyone :wave:

I have just been surprised by an interaction between default (optional) parameters and type genericity.

The functions f and g

def f {α : Type} (a : α) : List α := [a]
def g (a := "") : List String := [a]

work without any problem but the hybrid function h

def h {α : Type} (a : α := "") : List α := [a]

does not typecheck. The error is

Application type mismatch: In the application
  optParam α ""
the argument
  ""
has type
  String : Type
but is expected to have type
  α : Type

Could anyone provide some context (and maybe a workaround?) for this situation?

Cheers!

Sébastien

Yaël Dillies (Aug 12 2025 at 14:07):

Hey :wave: What do you intend to do with this function h?

Kenny Lau (Aug 12 2025 at 14:07):

you want by exact to delay elaboration

Aaron Liu (Aug 12 2025 at 14:07):

def h {α : Type} (a : α := by exact "") : List α := [a]

-- h "" : List String
#check (h)

Sébastien Boisgérault (Aug 12 2025 at 14:25):

Yaël Dillies said:

Hey :wave: What do you intend to do with this function h?

In the real use case, h is oneOrMore:

partial def oneOrMore {α : Type} {β₁ β₂ β₃ : Type} (p : Parser α)
  (parsePrefix : Parser β₁ := parseNothing)
  (parsePostFix : Parser β₂ := parseNothing)
  (parseSeparator : Parser β₃ := parseNothing) : Parser (List α):= sorry

The default argument parseNothing is of type Parser Unit but I'd rather not force it for all parsers (for the prefix, separator and postfix I would just ignore the parsed value).

Does it help?

Aaron Liu (Aug 12 2025 at 14:31):

If you are ignoring the parsed value you should probably take a Parser Unit anyways to signal that

Aaron Liu (Aug 12 2025 at 14:31):

the value can be discarded at the call site with docs#Functor.discard

Aaron Liu (Aug 12 2025 at 14:32):

this is how do notation works

Sébastien Boisgérault (Aug 12 2025 at 14:33):

Aaron Liu said:

def h {α : Type} (a : α := by exact "") : List α := [a]

Thanks! That works for the toy example and for the real one AFAICT. A bit mysterious for me ATM but appreciated nonetheless! :pray:

Sébastien Boisgérault (Aug 12 2025 at 14:38):

Aaron Liu said:

the value can be discarded at the call site with docs#Functor.discard

I was in the process of implementing a drop function ... :grinning_face_with_smiling_eyes:


Last updated: Dec 20 2025 at 21:32 UTC