{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": "true" }, "source": [ "# Table of Contents\n", "

1  TP 6 - Programmation pour la préparation à l'agrégation maths option info
2  Représentation des 𝜆-termes en OCaml
2.1  Grammaire
2.2  Ex1. Ecrire un type Caml représentant les 𝜆-termes
2.3  Ex2. Ecrire une fonction qui affiche un 𝜆-terme en une chaîne de caractère
$-24\">2.4  Ex3. Bonus : Ecrire une fonction qui transforme un 𝜆-terme en une chaîne représentant une fonction \"anonyme\" exécutable par Python. Rappel : lambda x: ... correspond à 𝜆𝑥.<...>
3  Calculs ?
4  Quelques termes utiles ?
4.1  Une valeur \"nulle\"
4.2  Composition
4.3  Conditionnelles
4.4  Nombres entiers en codage de Chuch
4.5  Successeur
4.6  Addition
4.7  Multiplication
4.8  Paires
4.9  Bonus : prédecesseur
4.10  Listes
4.11  Récursion 𝑈
4.12  Point fixe 𝑌
4.13  Bonus : la factorielle en 𝜆-calcul
5  Conclusion
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# TP 6 - Programmation pour la préparation à l'agrégation maths option info\n", "TP 6 : Lambda calcul, représentations, calculs et quelques termes pratiques.\n", "\n", "- Référence en théorie : [ce poly en français de Jean Goubault-Larrecqu (ENS Cachan)](http://www.lsv.fr/%7Egoubault/Lambda/lambda.pdf) ou le livre \"Logique réduction résolution\", par René Lalement.\n", "- Pour la pratique : [ce post de blog en anglais](http://matt.might.net/articles/python-church-y-combinator/)\n", "- Pour plus, [cette page wikipédia](https://fr.wikipedia.org/wiki/Lambda-calcul)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- En OCaml." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val print : ('a, out_channel, unit) format -> 'a = \n" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "The OCaml toplevel, version 4.04.2\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let print = Printf.printf;;\n", "Sys.command \"ocaml -version\";;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "# Représentation des $\\lambda$-termes en OCaml\n", "\n", "## Grammaire\n", "\n", "Avec une [grammaire BNF](https://fr.wikipedia.org/wiki/Forme_de_Backus-Naur), si `` désigne un nom d'expression valide (on se limitera à des noms en miniscules consistitués des 26 lettres a,b,..,z) :\n", "\n", " ::= \n", " | ()\n", " | fun -> \n", " | ()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex1. Ecrire un type Caml représentant les $\\lambda$-termes" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "type variable = string\n" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "type terme = V of variable | A of terme * terme | F of variable * terme\n" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type variable = string;;\n", "\n", "type terme =\n", " | V of variable\n", " | A of terme * terme\n", " | F of variable * terme\n", ";;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Par exemple, l'identité est le terme $\\lambda x. x$." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "File \"[7]\", line 1, characters 15-16:\n", "Warning 41: F belongs to several types: terme terme\n", "The first one was selected. Please disambiguate if this is wrong.\n" ] }, { "data": { "text/plain": [ "val identite : terme = F (\"x\", V \"x\")\n" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let identite = F(\"x\", V(\"x\"));;" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "File \"[8]\", line 1, characters 17-18:\n", "Warning 41: F belongs to several types: terme terme\n", "The first one was selected. Please disambiguate if this is wrong.\n" ] }, { "data": { "text/plain": [ "val identite_2 : terme = F (\"y\", V \"y\")\n" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let identite_2 = F(\"y\", V(\"y\"));;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Les deux termes sont différents mais égaux à $\\alpha$-renomage près." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Un autre exemple est le terme $\\Omega = (\\lambda x. xx)(\\lambda x. xx)$ (qui est le plus petit terme dont l'exécution par $\\beta$-réduction ne termine pas - cf [ce poly p7](http://www.lsv.fr/%7Egoubault/Lambda/lambda.pdf) si besoin)." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "File \"[9]\", line 1, characters 12-13:\n", "Warning 41: A belongs to several types: terme terme\n", "The first one was selected. Please disambiguate if this is wrong.\n" ] }, { "data": { "text/plain": [ "val omega : terme = A (F (\"x\", A (V \"x\", V \"x\")), F (\"x\", A (V \"x\", V \"x\")))\n" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let omega = A(F(\"x\", A(V(\"x\"), V(\"x\"))), F(\"x\", A(V(\"x\"), V(\"x\"))));;" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "File \"[10]\", line 1, characters 8-9:\n", "Warning 41: F belongs to several types: terme terme\n", "The first one was selected. Please disambiguate if this is wrong.\n" ] }, { "data": { "text/plain": [ "val u : terme = F (\"x\", A (V \"x\", V \"x\"))\n" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" }, { "name": "stderr", "output_type": "stream", "text": [ "File \"[10]\", line 2, characters 12-13:\n", "Warning 41: A belongs to several types: terme terme\n", "The first one was selected. Please disambiguate if this is wrong.\n" ] }, { "data": { "text/plain": [ "val omega : terme = A (F (\"x\", A (V \"x\", V \"x\")), F (\"x\", A (V \"x\", V \"x\")))\n" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let u = F(\"x\", A(V(\"x\"), V(\"x\")));;\n", "let omega = A(u, u);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex2. Ecrire une fonction qui affiche un $\\lambda$-terme en une chaîne de caractère\n", "C'est très rapide." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val sprintf : ('a, unit, string) format -> 'a = \n" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" }, { "name": "stderr", "output_type": "stream", "text": [ "File \"[13]\", line 4, characters 6-7:\n", "Warning 41: V belongs to several types: terme terme\n", "The first one was selected. Please disambiguate if this is wrong.\n" ] }, { "data": { "text/plain": [ "val string_of_terme : terme -> variable = \n" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let sprintf = Format.sprintf;;\n", "\n", "let rec string_of_terme = function\n", " | V(s) -> s\n", " | A(u, v) -> sprintf \"(%s)(%s)\" (string_of_terme u) (string_of_terme v)\n", " (* \"(\" ^ (string_of_terme u) ^ \")\" ^ \"(\" ^ (string_of_terme v) ^ \")\" *)\n", " | F(s, u) -> sprintf \"λ %s. (%s)\" s (string_of_terme u)\n", " (* \"lambda \" ^ s ^ \".(\" ^ (string_of_terme u) *)\n", ";;" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "λ x. (x)\n" ] }, { "data": { "text/plain": [ "- : unit = ()\n" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "λ y. (y)\n" ] }, { "data": { "text/plain": [ "- : unit = ()\n" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "(λ x. ((x)(x)))(λ x. ((x)(x)))\n" ] }, { "data": { "text/plain": [ "- : unit = ()\n" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print_endline (string_of_terme identite);;\n", "print_endline (string_of_terme identite_2);;\n", "print_endline (string_of_terme omega);;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex3. Bonus : Ecrire une fonction qui transforme un $\\lambda$-terme en une chaîne représentant une fonction \"anonyme\" exécutable par Python. Rappel : `lambda x: ...` correspond à $\\lambda x. <...>$" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val sprintf : ('a, unit, string) format -> 'a = \n" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "val python_of_terme : terme -> variable = \n" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let sprintf = Format.sprintf;;\n", "\n", "let rec python_of_terme = function\n", " | V(s) -> s\n", " | A(u, v) -> sprintf \"(%s)(%s)\" (python_of_terme u) (python_of_terme v)\n", " | F(s, u) -> sprintf \"lambda %s: (%s)\" s (python_of_terme u)\n", ";;" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "lambda x: (x)\n" ] }, { "data": { "text/plain": [ "- : unit = ()\n" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "lambda y: (y)\n" ] }, { "data": { "text/plain": [ "- : unit = ()\n" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "(lambda x: ((x)(x)))(lambda x: ((x)(x)))\n" ] }, { "data": { "text/plain": [ "- : unit = ()\n" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print_endline (python_of_terme identite);;\n", "print_endline (python_of_terme identite_2);;\n", "print_endline (python_of_terme omega);;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On peut ensuite simplement appeler Python sur un terme et vérifier s'il s'exécute ou non." ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val execute_python_string : string -> int = \n" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let execute_python_string (s : string) : int =\n", " Sys.command (sprintf \"python -c 'print(%s)'\" s)\n", ";;" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " at 0x7fd84099c5f0>\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "execute_python_string (python_of_terme identite);;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On peut vérifier avec Python que ce terme $\\Omega$ ne termine pas :" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Traceback (most recent call last):\n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n" ] }, { "data": { "text/plain": [ "- : int = 1\n" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" }, { "name": "stderr", "output_type": "stream", "text": [ " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", " File \"\", line 1, in \n", "RuntimeError: maximum recursion depth exceeded\n" ] } ], "source": [ "execute_python_string (python_of_terme omega);;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "# Calculs ?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "# Quelques termes utiles ?\n", "\n", "On peut définir des $\\lambda$-termes utiles avec notre représentation, et on vérifiera ensuite en les exécutant via Python qu'ils sont corrects." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Une valeur \"nulle\"" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val none : terme = F (\"x\", V \"x\")\n" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let none = F(\"x\", V(\"x\"));;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Composition" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val compose : terme -> terme -> terme = \n" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let compose u v = A(u, v);;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conditionnelles\n", "Ce n'est qu'un des encodages possibles." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val si : terme = F (\"cond\", F (\"v\", F (\"f\", A (A (V \"cond\", V \"v\"), V \"f\"))))\n" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let si = F(\"cond\", F(\"v\", F(\"f\", A(A(V(\"cond\"), V(\"v\")), V(\"f\")))));;" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "λ cond. (λ v. (λ f. (((cond)(v))(f))))\n" ] }, { "data": { "text/plain": [ "- : unit = ()\n" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print_endline (string_of_terme si);;" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val vrai : terme = F (\"v\", F (\"f\", V \"v\"))\n" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "val faux : terme = F (\"v\", F (\"f\", V \"f\"))\n" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let vrai = F(\"v\", F(\"f\", V(\"v\")));;\n", "let faux = F(\"v\", F(\"f\", V(\"f\")));;" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "λ v. (λ f. (v))\n" ] }, { "data": { "text/plain": [ "- : unit = ()\n" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "λ v. (λ f. (f))\n" ] }, { "data": { "text/plain": [ "- : unit = ()\n" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print_endline (string_of_terme vrai);;\n", "print_endline (string_of_terme faux);;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Nombres entiers en codage de Chuch\n", "\n", "On rappelle que pour $n \\in \\mathbb{N}$, $[n] = \\lambda f. \\lambda x. f^n(x)$ est son codage dit codage de Church dans les $\\lambda$-termes." ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val zero : terme = F (\"f\", F (\"x\", V \"x\"))\n" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "val un : terme = F (\"f\", F (\"x\", A (V \"f\", V \"x\")))\n" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let zero = F(\"f\", F(\"x\", V(\"x\")));;\n", "let un = F(\"f\", F(\"x\", A(V(\"f\"), V(\"x\"))));;" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val terme_of_int : int -> terme = \n" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let terme_of_int (n : int) : terme =\n", " let rec aux = function\n", " | 0 -> V(\"x\")\n", " | n -> A(V(\"f\"), aux (n-1))\n", " in\n", " F(\"f\", F(\"x\", (aux n)))\n", ";;" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val deux : terme = F (\"f\", F (\"x\", A (V \"f\", A (V \"f\", V \"x\"))))\n" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let deux = terme_of_int 2;;" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "λ f. (λ x. ((f)((f)(x))))\n" ] }, { "data": { "text/plain": [ "- : unit = ()\n" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ " at 0x7f453cab55f0>\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print_endline (string_of_terme deux);;\n", "execute_python_string (python_of_terme deux);;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On peut faire l'opération inverse, interpréter un entier de Church en Python." ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val entier_natif_python : string = \"lambda c: c(lambda x: x+1)(0)\"\n" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let entier_natif_python = \"lambda c: c(lambda x: x+1)(0)\";;" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "execute_python_string (sprintf \"(%s)(%s)\" entier_natif_python (python_of_terme deux));;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> On est obligé de passer par une astuce, parce que $\\lambda x. x + 1$ et $0$ ne sont pas des $\\lambda$-termes." ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "execute_python_string (sprintf \"(%s)(%s)\" entier_natif_python (python_of_terme (terme_of_int 10)));;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bien sûr, tout ça est très limité !" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "s_push: parser stack overflow\n", "MemoryError\n" ] }, { "data": { "text/plain": [ "- : int = 1\n" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "execute_python_string (sprintf \"(%s)(%s)\" entier_natif_python (python_of_terme (terme_of_int 100)));;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Successeur" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Le successeur s'écrit $\\mathrm{succ} = \\lambda n. \\lambda f. \\lambda z. f(n(f)(z))$." ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val successeur : terme =\n", " F (\"n\", F (\"f\", F (\"z\", A (V \"f\", A (A (V \"n\", V \"f\"), V \"z\")))))\n" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let successeur = F(\"n\", F(\"f\", F(\"z\", A(V(\"f\"), A(A(V(\"n\"), V(\"f\")), V(\"z\"))))));;" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "λ n. (λ f. (λ z. ((f)(((n)(f))(z)))))\n" ] }, { "data": { "text/plain": [ "- : unit = ()\n" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print_endline (string_of_terme successeur);;" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val dix : terme =\n", " F (\"f\",\n", " F (\"x\",\n", " A (V \"f\",\n", " A (V \"f\",\n", " A (V \"f\",\n", " A (V \"f\",\n", " A (V \"f\",\n", " A (V \"f\", A (V \"f\", A (V \"f\", A (V \"f\", A (V \"f\", V \"x\"))))))))))))\n" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "val onze : terme =\n", " A (F (\"n\", F (\"f\", F (\"z\", A (V \"f\", A (A (V \"n\", V \"f\"), V \"z\"))))),\n", " F (\"f\",\n", " F (\"x\",\n", " A (V \"f\",\n", " A (V \"f\",\n", " A (V \"f\",\n", " A (V \"f\",\n", " A (V \"f\",\n", " A (V \"f\", A (V \"f\", A (V \"f\", A (V \"f\", A (V \"f\", V \"x\")))))))))))))\n" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let dix = terme_of_int 10;;\n", "let onze = A(successeur, dix);;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A noter que ce terme `onze` ne sera pas le même que celui (plus court) obtenu par `terme_of_int 11` :" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val onze2 : terme =\n", " F (\"f\",\n", " F (\"x\",\n", " A (V \"f\",\n", " A (V \"f\",\n", " A (V \"f\",\n", " A (V \"f\",\n", " A (V \"f\",\n", " A (V \"f\",\n", " A (V \"f\", A (V \"f\", A (V \"f\", A (V \"f\", A (V \"f\", V \"x\")))))))))))))\n" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let onze2 = terme_of_int 11;;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Mais ils s'exécutent de la même façon :" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "11\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "11\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "execute_python_string (sprintf \"(%s)(%s)\" entier_natif_python (python_of_terme dix));;\n", "execute_python_string (sprintf \"(%s)(%s)\" entier_natif_python (python_of_terme onze));;\n", "execute_python_string (sprintf \"(%s)(%s)\" entier_natif_python (python_of_terme onze2));;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Addition" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "La somme s'écrit $\\mathrm{somme} = \\lambda n. \\lambda m. \\lambda f. \\lambda z. n(f)(m(f)(z))$." ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val somme : terme =\n", " F (\"n\",\n", " F (\"m\",\n", " F (\"f\", F (\"z\", A (A (V \"n\", V \"f\"), A (A (V \"m\", V \"f\"), V \"z\"))))))\n" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let somme = F(\"n\", F(\"m\", F(\"f\", F(\"z\", A((A(V(\"n\"), V(\"f\"))), A((A(V(\"m\"), V(\"f\"))), V(\"z\")) )))));;" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "λ n. (λ m. (λ f. (λ z. (((n)(f))(((m)(f))(z))))))\n" ] }, { "data": { "text/plain": [ "- : unit = ()\n" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print_endline (string_of_terme somme);;" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val trois : terme =\n", " A\n", " (A\n", " (F (\"n\",\n", " F (\"m\",\n", " F (\"f\", F (\"z\", A (A (V \"n\", V \"f\"), A (A (V \"m\", V \"f\"), V \"z\")))))),\n", " F (\"f\", F (\"x\", A (V \"f\", V \"x\")))),\n", " F (\"f\", F (\"x\", A (V \"f\", A (V \"f\", V \"x\")))))\n" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let trois = A(A(somme, un), deux);;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Comme pour le successeur, ce terme est bien plus compliqué que l'encodage de Church, mais ils s'exécutent de la même manière." ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val trois2 : terme = F (\"f\", F (\"x\", A (V \"f\", A (V \"f\", A (V \"f\", V \"x\")))))\n" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let trois2 = terme_of_int 3;;" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "execute_python_string (sprintf \"(%s)(%s)\" entier_natif_python (python_of_terme trois));;\n", "execute_python_string (sprintf \"(%s)(%s)\" entier_natif_python (python_of_terme trois2));;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multiplication" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "La multiplication est $\\mathrm{mul} = \\lambda n. \\lambda m. \\lambda f. \\lambda z. m(n(f))(z)$." ] }, { "cell_type": "code", "execution_count": 122, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "val mul : terme =\n", " F (\"n\", F (\"m\", F (\"f\", F (\"z\", A (A (V \"m\", A (V \"n\", V \"f\")), V \"z\")))))\n" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let mul = F(\"n\", F(\"m\", F(\"f\", F(\"z\", A(A(V(\"m\"), A(V(\"n\"), V(\"f\"))), V(\"z\"))))));;" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val trois : terme = F (\"f\", F (\"x\", A (V \"f\", A (V \"f\", A (V \"f\", V \"x\")))))\n" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "val six : terme =\n", " A\n", " (A\n", " (F (\"n\",\n", " F (\"m\", F (\"f\", F (\"z\", A (A (V \"m\", A (V \"n\", V \"f\")), V \"z\"))))),\n", " F (\"f\", F (\"x\", A (V \"f\", A (V \"f\", A (V \"f\", V \"x\")))))),\n", " F (\"f\", F (\"x\", A (V \"f\", A (V \"f\", V \"x\")))))\n" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "val six2 : terme =\n", " A\n", " (A\n", " (F (\"n\",\n", " F (\"m\", F (\"f\", F (\"z\", A (A (V \"m\", A (V \"n\", V \"f\")), V \"z\"))))),\n", " F (\"f\", F (\"x\", A (V \"f\", A (V \"f\", V \"x\"))))),\n", " F (\"f\", F (\"x\", A (V \"f\", A (V \"f\", A (V \"f\", V \"x\"))))))\n" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let trois = terme_of_int 3 ;;\n", "let six = A(A(mul, trois), deux);;\n", "let six2 = A(A(mul, deux), trois);;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Comme pour le successeur, ce terme est bien plus compliqué que l'encodage de Church, mais ils s'exécutent de la même manière." ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "6\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "6\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "execute_python_string (sprintf \"(%s)(%s)\" entier_natif_python (python_of_terme six));;\n", "execute_python_string (sprintf \"(%s)(%s)\" entier_natif_python (python_of_terme six2));;\n", "execute_python_string (sprintf \"(%s)(%s)\" entier_natif_python (python_of_terme (terme_of_int 6)));;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Paires" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "La représentation de la paire est simplement $\\mathrm{pair} = \\lambda a. \\lambda b. \\lambda f. f(a)(b)$." ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val a : terme = V \"a\"\n", "val b : terme = V \"b\"\n", "val f : terme = V \"f\"\n" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "val pair : terme = F (\"a\", F (\"b\", F (\"f\", A (A (V \"f\", V \"a\"), V \"b\"))))\n" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let a = V(\"a\") and b = V(\"b\") and f = V(\"f\");;\n", "let pair = F(\"a\", F(\"b\", F(\"f\", A(A(f, a), b))));;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Et ensuite les deux extracteurs sont immédiats : $\\mathrm{gauche} = \\lambda p. p(\\lambda a. \\lambda b. a)$ et $\\mathrm{droite} = \\lambda p. p(\\lambda a. \\lambda b. b)$.\n", "(on retrouve `vrai` et `faux`)" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val gauche : terme = F (\"f\", A (V \"f\", F (\"v\", F (\"f\", V \"v\"))))\n" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "val droite : terme = F (\"f\", A (V \"f\", F (\"v\", F (\"f\", V \"f\"))))\n" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let gauche = F(\"f\", A(f, vrai));;\n", "let droite = F(\"f\", A(f, faux));;" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val exemple_pair : terme =\n", " A\n", " (A (F (\"a\", F (\"b\", F (\"f\", A (A (V \"f\", V \"a\"), V \"b\")))),\n", " F (\"f\", F (\"x\", A (V \"f\", A (V \"f\", V \"x\"))))),\n", " F (\"f\", F (\"x\", A (V \"f\", A (V \"f\", A (V \"f\", V \"x\"))))))\n" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let exemple_pair = A(A(pair, deux), trois);;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On vérifie qu'on peut extraire `[2]` et `[3]` de cette paire `[(2, 3)]` :" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "execute_python_string (sprintf \"(%s)(%s)\" entier_natif_python (python_of_terme (A(gauche, exemple_pair))));;\n", "execute_python_string (sprintf \"(%s)(%s)\" entier_natif_python (python_of_terme (A(droite, exemple_pair))));;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bonus : prédecesseur" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> Avec les paires, c'est possible.\n", "> Idée de l'algorithme : en ayant `[n]`, on commence par la paire `[(0, 0)]` et on itère la fonction `fun (a,b) -> (a+1, a)` exactement `n` fois (et ça c'est facile par définition du codage `[n]`, ce qui donne la paire `[(n, n-1)]` et en récupérant la deuxième coordonnée on a `[n-1]`.\n", "> C'est corrigé en Exercice 12 du poly de Jean Goubault-Larrecqu.\n", "\n", "On va découper ça en morceau :" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val constructeur_pair : terme -> terme -> terme = \n" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "val pi1 : terme -> terme = \n" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "val pi2 : terme -> terme = \n" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "val constructeur_succ : terme -> terme = \n" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "val pair_00 : terme =\n", " A\n", " (A (F (\"a\", F (\"b\", F (\"f\", A (A (V \"f\", V \"a\"), V \"b\")))),\n", " F (\"f\", F (\"x\", V \"x\"))),\n", " F (\"f\", F (\"x\", V \"x\")))\n" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let constructeur_pair u v = A(A(pair, u), v);;\n", "let pi1 u = A(gauche, u);;\n", "let pi2 u = A(droite, u);;\n", "let constructeur_succ u = A(successeur, u);;\n", "\n", "let pair_00 = constructeur_pair zero zero;;" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val p : terme = V \"p\"\n" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "val succ_1 : terme =\n", " F (\"p\",\n", " A\n", " (A (F (\"a\", F (\"b\", F (\"f\", A (A (V \"f\", V \"a\"), V \"b\")))),\n", " A (F (\"n\", F (\"f\", F (\"z\", A (V \"f\", A (A (V \"n\", V \"f\"), V \"z\"))))),\n", " A (F (\"f\", A (V \"f\", F (\"v\", F (\"f\", V \"v\")))), V \"p\"))),\n", " A (F (\"f\", A (V \"f\", F (\"v\", F (\"f\", V \"v\")))), V \"p\")))\n" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let p = V(\"p\");;\n", "let succ_1 = F(\"p\", constructeur_pair (constructeur_succ(pi1(p))) (pi1(p)));;" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val n : terme = V \"n\"\n" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "val predecesseur : terme =\n", " F (\"n\",\n", " A (F (\"f\", A (V \"f\", F (\"v\", F (\"f\", V \"f\")))),\n", " A\n", " (A (V \"n\",\n", " F (\"p\",\n", " A\n", " (A (F (\"a\", F (\"b\", F (\"f\", A (A (V \"f\", V \"a\"), V \"b\")))),\n", " A\n", " (F (\"n\",\n", " F (\"f\", F (\"z\", A (V \"f\", A (A (V \"n\", V \"f\"), V \"z\"))))),\n", " A (F (\"f\", A (V \"f\", F (\"v\", F (\"f\", V \"v\")))), V \"p\"))),\n", " A (F (\"f\", A (V \"f\", F (\"v\", F (\"f\", V \"v\")))), V \"p\")))),\n", " A\n", " (A (F (\"a\", F (\"b\", F (\"f\", A (A (V \"f\", V \"a\"), V \"b\")))),\n", " F (\"f\", F (\"x\", V \"x\"))),\n", " F (\"f\", F (\"x\", V \"x\"))))))\n" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let n = V(\"n\");;\n", "let predecesseur = F(\"n\", pi2(A(A(n, succ_1), pair_00)));;" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val cinq : terme =\n", " A\n", " (F (\"n\",\n", " A (F (\"f\", A (V \"f\", F (\"v\", F (\"f\", V \"f\")))),\n", " A\n", " (A (V \"n\",\n", " F (\"p\",\n", " A\n", " (A (F (\"a\", F (\"b\", F (\"f\", A (A (V \"f\", V \"a\"), V \"b\")))),\n", " A\n", " (F (\"n\",\n", " F (\"f\", F (\"z\", A (V \"f\", A (A (V \"n\", V \"f\"), V \"z\"))))),\n", " A (F (\"f\", A (V \"f\", F (\"v\", F (\"f\", V \"v\")))), V \"p\"))),\n", " A (F (\"f\", A (V \"f\", F (\"v\", F (\"f\", V \"v\")))), V \"p\")))),\n", " A\n", " (A (F (\"a\", F (\"b\", F (\"f\", A (A (V \"f\", V \"a\"), V \"b\")))),\n", " F (\"f\", F (\"x\", V \"x\"))),\n", " F (\"f\", F (\"x\", V \"x\")))))),\n", " F (\"f\",\n", " F (\"x\",\n", " A (V \"f\", A (V \"f\", A (V \"f\", A (V \"f\", A (V \"f\", A (V \"f\", V \"x\")))))))))\n" ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "val cinq2 : terme =\n", " F (\"f\",\n", " F (\"x\", A (V \"f\", A (V \"f\", A (V \"f\", A (V \"f\", A (V \"f\", V \"x\")))))))\n" ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let cinq = A(predecesseur, (terme_of_int 6));;\n", "let cinq2 = terme_of_int 5;;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Comme pour le successeur, ce terme est bien plus compliqué que l'encodage de Church, mais ils s'exécutent de la même manière." ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "execute_python_string (sprintf \"(%s)(%s)\" entier_natif_python (python_of_terme cinq));;\n", "execute_python_string (sprintf \"(%s)(%s)\" entier_natif_python (python_of_terme cinq2));;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On vérifie que `pred 0 = 0` :" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "val zero2 : terme =\n", " A\n", " (F (\"n\",\n", " A (F (\"f\", A (V \"f\", F (\"v\", F (\"f\", V \"f\")))),\n", " A\n", " (A (V \"n\",\n", " F (\"p\",\n", " A\n", " (A (F (\"a\", F (\"b\", F (\"f\", A (A (V \"f\", V \"a\"), V \"b\")))),\n", " A\n", " (F (\"n\",\n", " F (\"f\", F (\"z\", A (V \"f\", A (A (V \"n\", V \"f\"), V \"z\"))))),\n", " A (F (\"f\", A (V \"f\", F (\"v\", F (\"f\", V \"v\")))), V \"p\"))),\n", " A (F (\"f\", A (V \"f\", F (\"v\", F (\"f\", V \"v\")))), V \"p\")))),\n", " A\n", " (A (F (\"a\", F (\"b\", F (\"f\", A (A (V \"f\", V \"a\"), V \"b\")))),\n", " F (\"f\", F (\"x\", V \"x\"))),\n", " F (\"f\", F (\"x\", V \"x\")))))),\n", " F (\"f\", F (\"x\", V \"x\")))\n" ] }, "execution_count": 146, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let zero2 = A(predecesseur, zero);;" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "0\n" ] }, { "data": { "text/plain": [ "- : int = 0\n" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "execute_python_string (sprintf \"(%s)(%s)\" entier_natif_python (python_of_terme zero2));;\n", "execute_python_string (sprintf \"(%s)(%s)\" entier_natif_python (python_of_terme zero));;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Listes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Récursion $U$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Point fixe $Y$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bonus : la factorielle en $\\lambda$-calcul" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "# Conclusion\n", "\n", "Fin. À la séance prochaine. Le TP6 traitera de ?? (en février)." ] } ], "metadata": { "kernelspec": { "display_name": "OCaml 4.04.2", "language": "OCaml", "name": "ocaml-jupyter" }, "language_info": { "codemirror_mode": "text/x-ocaml", "file_extension": ".ml", "mimetype": "text/x-ocaml", "name": "OCaml", "nbconverter_exporter": null, "pygments_lexer": "OCaml", "version": "4.04.2" }, "toc": { "colors": { "hover_highlight": "#DAA520", "running_highlight": "#FF0000", "selected_highlight": "#FFD700" }, "moveMenuLeft": true, "nav_menu": { "height": "511px", "width": "251px" }, "navigate_menu": true, "number_sections": true, "sideBar": true, "threshold": 4, "toc_cell": true, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }