Zulip Chat Archive

Stream: new members

Topic: applying a function to itself


view this post on Zulip Kenny Lau (Jun 03 2020 at 15:37):

meta def foo : Type :=
foo  foo

#check λ f g : foo, f g

view this post on Zulip Kenny Lau (Jun 03 2020 at 15:37):

type mismatch at application
  f g
term
  g
has type
  foo
but is expected to have type
  [foo._main]

view this post on Zulip Kenny Lau (Jun 03 2020 at 15:37):

how do I make it work?

view this post on Zulip Kenny Lau (Jun 03 2020 at 15:37):

I want to apply a function to itself

view this post on Zulip Kenny Lau (Jun 03 2020 at 15:37):

why does foo have no equational lemma?

view this post on Zulip Pedro Minicz (Jun 06 2020 at 04:52):

I am not familiar with meta definitions in Lean, but meta def foo : Type := foo → foo seems to be a hack-y attempt at an untyped identity function.

view this post on Zulip Pedro Minicz (Jun 06 2020 at 04:56):

You cannot apply a strictly one argument function to itself, because that necessarily would lead to a type mismatch. That is, you would have α → b and α = α → b, so α would have to be a fix-point ((... → b) → b) → b, which is not possible, because terms themselves can't be infinite.

view this post on Zulip Pedro Minicz (Jun 06 2020 at 05:02):

You can, however, (kind-of) apply an identify function to itself.

def id' {α : Type*} : α  α := λ x, x

id is a function of two arguments, the first of which is implicit, so Lean will try to figure it out for us if it can.

#check id' id'

Lean will figure out that the type α for the leftmost id' is Π {α : Type*}, α → α. You can make implicit arguments explicit using @:

#check @id' (Π {α : Type*}, α  α) @id'

view this post on Zulip Pedro Minicz (Jun 06 2020 at 05:03):

Another example of interest:

variable β : Type*

#check @id' (β  β) id'

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:05):

@Mario Carneiro

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:06):

hi

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:06):

it's not going to work

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:06):

why not

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:06):

because this would make the typechecker diverge

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:07):

what does diverge mean?

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:07):

not stop

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:07):

recursive meta definitions use macros to indicate the recursion, they aren't defeq to their unfolding

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:08):

but can we prove foo = [foo._main]?

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:08):

I'm not even sure the concept makes sense since the kernel never sees meta defs

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:09):

what does that even mean? In meta land everything is provable

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:09):

meta def foo : Type :=
foo  foo

#print prefix foo
/-
foo : Type
foo._main : Type
-/

#print foo
/-
meta def foo : Type :=
foo._main
-/

#print foo._main
/-
meta def foo._main : Type :=
id_rhs Type ([foo._main] → [foo._main])
-/

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:10):

what is the [] in [foo._main]?

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:10):

a macro

view this post on Zulip Kevin Buzzard (Jun 10 2020 at 14:10):

I just thought it meant "you dunfolded too much"

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:11):

how is [] defined?

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:11):

You are thinking of the projection macro

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:11):

It is expr.macro

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:11):

it is a separate constructor from pi, lam, var,mvar and everything else

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:12):

Here's a projection macro:

#print and.left
-- @[reducible]
-- def and.left : ∀ {a b : Prop}, a ∧ b → a :=
-- λ (a b : Prop) (c : a ∧ b), [and.left c]

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:12):

how do I produce a term of type [\a]?

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:12):

You can't

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:12):

seriously, this is super annoying

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:12):

alright sorry

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:12):

but expr.macro uses an opaque type macro_def that lean code can't create

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:13):

no, not you, macros are annoying

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:13):

can I use unchecked_cast?

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:13):

I think that will work

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:13):

#check λ f : foo, f $ unchecked_cast f

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:13):

but would it "work" like I want it to?

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:14):

hm, best think of a better test

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:14):

maybe use the Y combinator so that it halts

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:15):

You can at least use expr.macro_def_name to see what kind of macro you are looking at

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:17):

variable {α : Type*}

meta def Y : (α  α)  α | f := f (Y f)

#check λ f : α  α, (rfl : Y f = f (Y f))
/-
invalid type ascription, term has type
  ?m_2 = ?m_2
but is expected to have type
  Y f = f (Y f)
-/

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:17):

oh no

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:21):

is it true that |A| = |A^A| iff |A| = 1?

view this post on Zulip Kevin Buzzard (Jun 10 2020 at 14:21):

#print has_add.add
/-
@[pattern, reducible]
def has_add.add : Π {α : Type u} [c : has_add α], α → α → α :=
λ (α : Type u) [c : has_add α], [has_add.add c]
-/

Is that also a macro?

view this post on Zulip Kevin Buzzard (Jun 10 2020 at 14:21):

If |A|>1 then |A^A|>=|2^A|>|A|

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:22):

so we have proved that foo \equiv unit

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:23):

meta def cantor := cantor  Prop

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:23):

yay I broke Lean

view this post on Zulip Kevin Buzzard (Jun 10 2020 at 14:23):

You only meta-broke it -- that's allowed

view this post on Zulip Kevin Buzzard (Jun 10 2020 at 14:24):

Reid proved false the other day in meta land -- it wasn't hard

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:26):

@Mario Carneiro how about this as a test: can you prove false using unchecked_cast and cantor?

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:26):

you can prove false using just unchecked_cast

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:26):

meta def cantor := cantor  Prop

meta example : false :=
let R : cantor := λ A, ¬ (@unchecked_cast _ cantor A) A in
sorry

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:26):

i.e. something like this

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:27):

meta example : false := unchecked_cast trivial

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:27):

i.e. can you translate cantor's paradox

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:27):

yes

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:28):

you need to use unchecked_cast to prove the side condition on cantor_injective though

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:28):

so it's not very impressive

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:30):

Without the theorem that says that cantor = (cantor → Prop), you won't get anywhere in the proof

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:30):

A B : set cantor,
H : unchecked_cast A = unchecked_cast B
 A = B

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:30):

you mean this is unchecked_cast H?

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:31):

It's unchecked_cast of anything you want

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:31):

I think you are ascribing more meaning to this than is warranted

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:31):

do we even "know" that cantor = (cantor \to Prop)?

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:32):

oh right, in meta land everything is true

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:32):

In meta land, it's not a complete free for all, there is a semantics but it is more like the semantics of Haskell

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:32):

however unchecked_cast breaks even that

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:32):

oh no this doesn't terminate:

#reduce @unchecked_cast true false trivial

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:33):

unchecked_cast is implemented as a loop IIRC

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:33):

oh nvm

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:33):

#eval @unchecked_cast   3 -- 3

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:33):

try it for larger numbers :)

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:34):

guess the outputs:

#eval @unchecked_cast empty  (unchecked_cast tt)
#eval @unchecked_cast empty  (unchecked_cast ff)

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:36):

One of these is not like the others:

#eval (list.range 34).map (λ n, @unchecked_cast   (2^n - 1))
-- [0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383,
--  32767, 65535, 131071, 262143, 524287, 1048575, 2097151, 4194303, 8388607,
--  16777215, 33554431, 67108863, 134217727, 268435455, 536870911, 1073741823,
--  -1, 4294967295, 8589934591]

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:38):

Oh haha the reason #reduce @unchecked_cast true false trivial doesn't terminate is for the very silly reason that strings are involved

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:40):

this works fine

meta def {u} unchecked_cast' {α : Sort u} {β : Sort u} : α  β :=
cast (undefined_core "")

#reduce @unchecked_cast' true false trivial
-- eq.rec true.intro (undefined_core {data := list.nil char})

view this post on Zulip Kenny Lau (Jun 10 2020 at 14:40):

why does it being "undefined" make it not terminate?

view this post on Zulip Patrick Massot (Jun 10 2020 at 14:41):

It reminds me that we have something for @Gabriel Ebner who is already hunting down ideas for Lean 3.17.0: fix https://github.com/leanprover-community/doc-gen/blob/master/src/export_json.lean#L284-L287. I had to use the same itersplit trick in the sphere eversion infrastructure.

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:41):

it terminates but only after about O(size of unicode * length of string * very large #reduce constant) steps

view this post on Zulip Gabriel Ebner (Jun 10 2020 at 14:42):

I'm not sure if these two are related, but this applies for both of them: please file a #mwe at the lean bug tracker!

view this post on Zulip Mario Carneiro (Jun 10 2020 at 14:42):

wait, is there a lean feature suggestion in this thread?

view this post on Zulip Rob Lewis (Jun 10 2020 at 14:42):

Mario Carneiro said:

One of these is not like the others:

#eval (list.range 34).map (λ n, @unchecked_cast   (2^n - 1))
-- [0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383,
--  32767, 65535, 131071, 262143, 524287, 1048575, 2097151, 4194303, 8388607,
--  16777215, 33554431, 67108863, 134217727, 268435455, 536870911, 1073741823,
--  -1, 4294967295, 8589934591]

https://oeis.org/search?q=0%2C+1%2C+3%2C+7%2C+15%2C+31%2C+63%2C+127%2C+255%2C+511%2C+1023%2C+2047%2C+4095%2C+8191%2C+16383%2C+32767%2C+65535%2C+131071%2C+262143%2C+524287%2C+1048575%2C+2097151%2C+4194303%2C+8388607%2C++16777215%2C+33554431%2C+67108863%2C+134217727%2C+268435455%2C+536870911%2C+1073741823%2C++-1%2C+4294967295%2C+8589934591&sort=&language=english&go=Search
:(

view this post on Zulip Kevin Buzzard (Jun 10 2020 at 14:49):

When I've got my number theorists hat on and I find a non-OEIS sequence I add it! But I'm not sure they'll like this one...

view this post on Zulip Kevin Buzzard (Jun 10 2020 at 14:50):

Kenny Lau said:

guess the outputs:

#eval @unchecked_cast empty  (unchecked_cast tt)
#eval @unchecked_cast empty  (unchecked_cast ff)

http://bash.org/?10958 (aah, the old days)


Last updated: May 08 2021 at 03:17 UTC