Zulip Chat Archive

Stream: new members

Topic: View list elements


Quinn Culver (Feb 25 2024 at 16:45):

I have defined this list

import Mathlib

structure Cashflow :=
(time : )
(amount : )

def CashflowSequence := List Cashflow

def cashflowExample22 : CashflowSequence :=
  (List.range 10).map (λ k => { time := k+1, amount := 1000 })

How can I view the elements of the list cashflowExample22 (to ensure I defined it correctly)?

Damiano Testa (Feb 25 2024 at 16:56):

Does #eval cashflowExample22 work? If CashflowSequence is an abbrev it might work.

Damiano Testa (Feb 25 2024 at 16:58):

Otherwise, you may have to add a deriving ToString or something similar to the definition of CashflowSequence -- depending on the actual code, something like this might work.

Quinn Culver (Feb 25 2024 at 16:59):

I just updated my MWE.

Quinn Culver (Feb 25 2024 at 17:06):

And I tried added deriving Repr to the end of the structure `Cashflow, but got error:

(kernel) invalid declaration, it uses unsafe declaration 'Real.instReprReal'Lean 4
unknown identifier 'reprCashflow✝'Lean 4
Repr.{u} (α : Type u) : Type u

Quinn Culver (Feb 25 2024 at 17:06):

and adding deriving ToString gives error:

default handlers have not been implemented yet, class: 'ToString' types: [Cashflow]

Quinn Culver (Feb 25 2024 at 17:16):

And no, #eval cashflowExample22 doesn't work.

Eric Wieser (Feb 25 2024 at 17:27):

you can write a Repr instance manually

Kyle Miller (Feb 25 2024 at 19:14):

It's got real numbers, so #eval isn't going to work here.

Kyle Miller (Feb 25 2024 at 19:14):

However, you can write a command to reduce the list and run norm_num on each entry.

import Mathlib

structure Cashflow :=
(time : )
(amount : )

def CashflowSequence := List Cashflow

def cashflowExample22 : CashflowSequence :=
  (List.range 10).map (λ k => { time := k+1, amount := 1000 })

section
open Lean Elab Meta Tactic
partial def reduce_list (e : Expr) : MetaM Expr := do
  let e'  whnf e
  if e'.isAppOfArity ``List.nil 1 then
    return e'
  else if e'.isAppOfArity ``List.cons 3 then
    let tail  reduce_list e'.appArg!
    return Expr.app e'.appFn! tail
  else
    return e
elab "reduce_list" : conv => withMainContext do
  Conv.changeLhs ( reduce_list ( Conv.getLhs))

macro "#list_norm_num " t:term : command =>
  `(command| #conv (reduce_list; norm_num [CoeT.coe, CoeHTCT.coe]) => $t)
end

#list_norm_num cashflowExample22
/-
[{ time := 1, amount := 1000 }, { time := 2, amount := 1000 }, { time := 3, amount := 1000 },
  { time := 4, amount := 1000 }, { time := 5, amount := 1000 }, { time := 6, amount := 1000 },
  { time := 7, amount := 1000 }, { time := 8, amount := 1000 }, { time := 9, amount := 1000 },
  { time := 10, amount := 1000 }]
-/

Kyle Miller (Feb 25 2024 at 19:15):

I told norm_num to unfold some coe functions that seemed to appear in your expression.

Eric Wieser (Feb 25 2024 at 20:05):

Quinn Culver said:

And I tried added deriving Repr to the end of the structure `Cashflow, but got error:

(kernel) invalid declaration, it uses unsafe declaration 'Real.instReprReal'Lean 4
unknown identifier 'reprCashflow✝'Lean 4
Repr.{u} (α : Type u) : Type u

This feels like a bug to me; deriving should automatically propagate unsafe, rather than erroring

Eric Wieser (Feb 25 2024 at 20:06):

Kyle Miller said:

It's got real numbers, so #eval isn't going to work here.

To elaborate on this thought for @Quinn Culver:

import Mathlib

#eval (3 : Real)
-- Real.ofCauchy (sorry /- 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, ... -/

Eric Wieser (Feb 25 2024 at 21:38):

Filed a bug as lean4#3498

Quinn Culver (Feb 26 2024 at 05:32):

What is the "sorry" doing there?

Quinn Culver (Feb 26 2024 at 05:34):

What does "COE" mean/stand for?

Quinn Culver (Feb 26 2024 at 05:36):

Eric Wieser said:

you can write a Repr instance manually

I barely understand Repr, so maybe I'm going into territory I don't belong, but what would that look like?

Kyle Miller (Feb 26 2024 at 05:47):

Quinn Culver said:

What does "COE" mean/stand for?

It's short for "coercion". The notation for them is the up arrow, and there's a coercion applied to k since k : Nat but the result needs to be a Real.


Last updated: May 02 2025 at 03:31 UTC