deniok: (typed lambda)
deniok ([personal profile] deniok) wrote2010-09-21 10:45 pm
Entry tags:

Задачка

Хорошая задачка из Herman Geuvers, Introduction to Type Theory (упражнение 3.17):

Построить лямбда-терм с типом ((α → β) → α) → (α → α → β) → β.

Ответы скринить не буду.

UPD: Имеется в виду просто типизированная лямбда. То есть fix и прочие Y-комбинаторы нам недоступны.

UPD2: Рекурсия ни в каком виде недоступна!

[identity profile] cadadr.livejournal.com 2010-09-21 06:52 pm (UTC)(link)
Coq < Theorem cheat : forall A B : Prop, ((A -> B) -> A) -> (A -> A -> B) -> B.
1 subgoal
  
  ============================
   forall A B : Prop, ((A -> B) -> A) -> (A -> A -> B) -> B

cheat < auto.
Proof completed.
...

[identity profile] cadadr.livejournal.com 2010-09-21 08:49 pm (UTC)(link)
Ну, конкретно это можно и ручками выписать.

(no subject)

[identity profile] deni-ok.livejournal.com - 2010-09-21 21:04 (UTC) - Expand

(no subject)

[identity profile] zelych.livejournal.com - 2010-09-22 07:16 (UTC) - Expand

Знаю, да

[identity profile] cadadr.livejournal.com - 2010-09-22 11:18 (UTC) - Expand

[identity profile] vanja-y.livejournal.com 2010-09-21 07:56 pm (UTC)(link)
Ничего лучше чем
\f g -> g(Y(f.g))

не придумывается.

[identity profile] vanja-y.livejournal.com 2010-09-21 07:57 pm (UTC)(link)
то есть

\f g -> g (Y(f.g)) (Y(f.g))

конечно.

[identity profile] deni-ok.livejournal.com 2010-09-21 08:03 pm (UTC)(link)
это уже близко к победе.
хинт: никто не запрещает вложенные лямбды (a без Y, наоборот, можно обойтись, поскольку выше уже показали, что это теорема минимальной логики высказываний :)

(no subject)

[identity profile] anatoly borodin - 2010-09-21 20:37 (UTC) - Expand

(no subject)

[identity profile] ro-che.info - 2010-09-21 20:44 (UTC) - Expand

(no subject)

[identity profile] anatoly borodin - 2010-09-21 21:47 (UTC) - Expand

(no subject)

[identity profile] cadadr.livejournal.com - 2010-09-21 21:04 (UTC) - Expand

[identity profile] ro-che.info (from livejournal.com) 2010-09-21 08:39 pm (UTC)(link)
Y не имеет типа, вот в чем проблема.
А если б имел, то можно было бы еще проще -- "Y I" :)

[identity profile] vanja-y.livejournal.com 2010-09-21 08:00 pm (UTC)(link)
Можно ещё
\f g -> Y(g.f) Y(f.g)

но я не уверен, что такое счастье будет работать.

[identity profile] deni-ok.livejournal.com 2010-09-21 09:12 pm (UTC)(link)
наверное всё-таки
\f g -> Y(g.f)(Y(f.g))
Но это тоже с Y...

[identity profile] ro-che.info (from livejournal.com) 2010-09-21 08:40 pm (UTC)(link)
Красиво, мне понравилось :)

Prelude> :t \x y -> (\s -> s (x s)) (\s -> y s s)
\x y -> (\s -> s (x s)) (\s -> y s s)
  :: ((t -> t1) -> t) -> (t -> t -> t1) -> t1

[identity profile] deni-ok.livejournal.com 2010-09-21 08:49 pm (UTC)(link)
Правильно! У меня решение другое
Prelude> :t \f g -> g (f (\x -> g x x)) (f (\x -> g x x))
\f g -> g (f (\x -> g x x)) (f (\x -> g x x))
  :: ((t1 -> t) -> t1) -> (t1 -> t1 -> t) -> t
но я использовал хинт из первоисточника.

[identity profile] ro-che.info (from livejournal.com) 2010-09-21 08:54 pm (UTC)(link)
Оно не другое, я тоже сначала к такому пришел, просто потом "разложил на множители". Вот этот последний шаг мне особенно понравился :)

(no subject)

[identity profile] deni-ok.livejournal.com - 2010-09-21 20:58 (UTC) - Expand

просто же как две копейки

[identity profile] mr-aleph.livejournal.com 2010-09-21 10:30 pm (UTC)(link)
Prelude> :t (\x y -> (\x' -> y x' x') (x (\x' -> y x' x'))) 
(\x y -> (\x' -> y x' x') (x (\x' -> y x' x')))
  :: ((t1 -> t) -> t1) -> (t1 -> t1 -> t) -> t

Re: просто же как две копейки

[identity profile] deni-ok.livejournal.com 2010-09-22 07:50 am (UTC)(link)
ну да, все эти тавтологии пропозициональной логики, они такие, двухкопеечные

[identity profile] zelych.livejournal.com 2010-09-22 07:19 am (UTC)(link)
а я вот так придумал: z f g = ab a where (ab, a) = (g a, f ab)

[identity profile] deni-ok.livejournal.com 2010-09-22 07:39 am (UTC)(link)
Рекурсия в where!
Просто типизированная лямбда негодуе!11

(no subject)

[identity profile] zelych.livejournal.com - 2010-09-22 07:47 (UTC) - Expand

[identity profile] asviraspossible.livejournal.com 2010-09-22 01:51 pm (UTC)(link)
f :: ((a -> b) -> a) -> (a -> (a -> b)) -> b
f a b = x (a x)
where x = fix (b . a)

[identity profile] asviraspossible.livejournal.com 2010-09-22 01:57 pm (UTC)(link)
Если поменять местами, то:

f :: ((a -> b) -> a) -> (a -> (a -> b)) -> b
f a b = b x x
  where x = fix (a . b)

(no subject)

[identity profile] deni-ok.livejournal.com - 2010-09-22 14:03 (UTC) - Expand

[identity profile] kodt-rsdn.livejournal.com 2010-09-22 02:28 pm (UTC)(link)
Гаскель-шмаскель
term :: ((a->b)->a) -> (a->(a->b)) -> b

term f g = b where
  a  = f ab -- чтобы не плодить сущности, оставим единственное значение типа a
  ab = g a
  b  = ab a
  -- или
  a = (f.g) a
  b = g a a

Поскольку здесь возникает неподвижная точка, - любители могут зафигачить туда Y и сделать нерекурсивный терм.

Примеры
-- самое простое: неподвижная точка константной функции
foo ab   = "hello"              -- a :: String
goo a a' = length a + length a' -- b :: Int
t = term foo goo -- t=10

-- чуть-чуть заковыристее
foo ab = map ab [[1], [1,2], [1,2,3], [1,2,3,4]] -- a :: [Int]
goo a a' = length a + length a'                  -- b :: Int
t = term foo goo -- t=8

[identity profile] deni-ok.livejournal.com 2010-09-22 02:32 pm (UTC)(link)
Можно и без рекурсии

(no subject)

[identity profile] kodt-rsdn.livejournal.com - 2010-09-22 14:48 (UTC) - Expand

(no subject)

[identity profile] deni-ok.livejournal.com - 2010-09-22 15:02 (UTC) - Expand

(no subject)

[identity profile] kodt-rsdn.livejournal.com - 2010-09-22 15:02 (UTC) - Expand

(no subject)

[identity profile] deni-ok.livejournal.com - 2010-09-22 15:03 (UTC) - Expand

(no subject)

[identity profile] kodt-rsdn.livejournal.com - 2010-09-22 15:07 (UTC) - Expand

(no subject)

[identity profile] kodt-rsdn.livejournal.com - 2010-09-22 15:07 (UTC) - Expand

(no subject)

[identity profile] migmit.livejournal.com - 2010-09-25 08:27 (UTC) - Expand

(no subject)

[identity profile] kodt-rsdn.livejournal.com - 2010-09-25 22:02 (UTC) - Expand

(no subject)

[identity profile] migmit.livejournal.com - 2010-09-26 09:25 (UTC) - Expand

(no subject)

[identity profile] kodt-rsdn.livejournal.com - 2010-09-26 19:22 (UTC) - Expand

(no subject)

[identity profile] migmit.livejournal.com - 2010-09-26 19:47 (UTC) - Expand

(no subject)

[identity profile] kodt-rsdn.livejournal.com - 2010-09-26 20:00 (UTC) - Expand

[identity profile] migmit.livejournal.com 2010-09-25 08:26 am (UTC)(link)
Думал секунд 30:
\g b -> (\d -> d (g d)) (\a -> b a a)

С днем варения!

[identity profile] kitya.livejournal.com 2010-10-16 11:44 pm (UTC)(link)
Отанжоби омедето гозаимас! Ну и троекратное ура!

Re: С днем варения!

[identity profile] deni-ok.livejournal.com 2010-10-17 07:54 am (UTC)(link)
Спасибо!

Как и следовало ожидать

[identity profile] perikov.livejournal.com 2011-02-10 02:45 pm (UTC)(link)
все закончилось сокрушительной победой пруф ассистантов :) Coq, Agda, djinn рисуют такие вещи нараз:)

Re: Как и следовало ожидать

[identity profile] deni-ok.livejournal.com 2011-02-10 04:00 pm (UTC)(link)
хорошо хоть, что пока ещё большинство пользующихся пруф ассистантами понимают как они работают :)