# Chaitin's construction

### From HaskellWiki

EndreyMark (Talk | contribs) m (Reaneming >>^ to >>@ (in the sense of liftM2 apply), because arrows use also symbol >>^ in another sense (p >>^ f = p >>> arr f)) |
EndreyMark (Talk | contribs) (Rephrasings, annotations, and code examples for partial function approach and total function approach) |
||

Line 25: | Line 25: | ||

Having seen this, decoding is rather straightforward. |
Having seen this, decoding is rather straightforward. |
||

− | Let us represent it e.g with the following LL1 parser. Of course, we can build it on top of more sophisticated parser libraries (Parsec, arrow parsers) |
+ | Let us represent it e.g. with the following LL1 parser (or maybe is it an LL0 one?). Of course, we can build it on top of more sophisticated parser libraries (Parsec, arrow parsers). It may be easier to attach othor monad transformers to this simpler parser, but the ask does not require such possibility. |

==== Decoding module ==== |
==== Decoding module ==== |
||

Line 108: | Line 108: | ||

<haskell> |
<haskell> |
||

− | module Parser (Parser, item) where |
+ | module Parser (Parser, runParser, item) where |

− | import Control.Monad.State (StateT, get, put) |
+ | import Control.Monad.State (StateT, runStateT, get, put) |

type Parser token a = StateT [token] [] a |
type Parser token a = StateT [token] [] a |
||

− | item :: Parser a |
+ | runParser :: Parser token a -> [token] -> [(a, [token])] |

+ | runParser = runStateT |
||

+ | |||

+ | item :: Parser token token |
||

item = do |
item = do |
||

token : tokens <- get |
token : tokens <- get |
||

Line 148: | Line 148: | ||

=== Approach based on decoding with total function === |
=== Approach based on decoding with total function === |
||

− | Here, <math>\mathrm{dc}</math> is a partial function (from finite bit sequences). If this is confusing or annoying, then we can choose a more Haskell-like approach, making <math>\mathrm{dc}</math> a total function: |
+ | Seen above, <math>\mathrm{dc}</math> was a partial function (from finite bit sequences). We can implement it e.g. as |

+ | <haskell> |
||

+ | dc :: [Bit] -> CL |
||

+ | dc = fst . head . runParser clP |
||

+ | </haskell> |
||

+ | where the use of <hask>head</hask> reveals that it is a partial function (of course, because not every bit sequence is a correct coding of a CL-term). |
||

+ | |||

+ | If this is confusing or annoying, then we can choose a more Haskell-like approach, making <math>\mathrm{dc}</math> a total function: |
||

<haskell> |
<haskell> |
||

dc :: [Bit] -> Maybe CL |
dc :: [Bit] -> Maybe CL |
||

+ | dc = fst . head . runParser (safe clP) |
||

+ | </haskell> |
||

+ | where |
||

+ | <haskell> |
||

+ | safe :: MonadPlus m => m a -> m (Maybe a) |
||

+ | safe p = liftM Just p `mplus` return Nothing |
||

</haskell> |
</haskell> |
||

then, Chaitin's construction will be |
then, Chaitin's construction will be |

## Revision as of 21:10, 3 August 2006

## Contents |

## 1 Introduction

Are there any real numbers which are defined exactly, but cannot be computed? This question leads us to exact real arithmetic, foundations of mathematics and computer science.

See Wikipedia article on Chaitin's construction, referring to e.g.

- Computing a Glimpse of Randomness (written by Cristian S. Calude, Michael J. Dinneen, and Chi-Kou Shu)
- Omega and why math has no TOEs (Gregory Chaitin).

## 2 Basing it on combinatory logic

Some more direct relatedness to functional programming: we can base Ω on combinatory logic (instead of a Turing machine).

### 2.1 Coding

See the prefix coding system described in Binary Lambda Calculus and Combinatory Logic (page 20) written by John Tromp:

of course, *c*, *d* are meta-variables, and also some other notations are changed slightly.

### 2.2 Decoding

Having seen this, decoding is rather straightforward. Let us represent it e.g. with the following LL1 parser (or maybe is it an LL0 one?). Of course, we can build it on top of more sophisticated parser libraries (Parsec, arrow parsers). It may be easier to attach othor monad transformers to this simpler parser, but the ask does not require such possibility.

#### 2.2.1 Decoding module

module Decode (clP) where import Parser (Parser, item) import CL (CL, k, s, apply) import CLExt ((>>@)) import PreludeExt (bool) clP :: Parser Bool CL clP = item (bool applicationP baseP) applicationP :: Parser Bool CL applicationP = clP >>@ clP baseP :: Parser Bool CL baseP = item (bool k s) kP, sP :: Parser Bool CL kP = return k sP = return s

#### 2.2.2 Combinatory logic term modules

##### 2.2.2.1 CL

module CL (CL, k, s, apply) where import Tree (Tree (Leaf, Branch)) import BaseSymbol (BaseSymbol, kay, ess) type CL = Tree BaseSymbol k, s :: CL k = Leaf kay s = Leaf ess apply :: CL -> CL -> CL apply = Branch

##### 2.2.2.2 CL extension

module CLExt ((>>@)) where import CL (CL, apply) import Control.Monad (Monad, liftM2) (>>@) :: Monad m => m CL -> m CL -> m CL (>>@) = liftM2 apply

##### 2.2.2.3 Base symbol

module BaseSymbol (BaseSymbol, kay, ess) where data BaseSymbol = K | S kay, ess :: BaseSymbol kay = K ess = S

#### 2.2.3 Utility modules

##### 2.2.3.1 Binary tree

module Tree (Tree (Leaf, Branch)) where data Tree a = Leaf a | Branch (Tree a) (Tree a)

##### 2.2.3.2 Parser

module Parser (Parser, runParser, item) where import Control.Monad.State (StateT, runStateT, get, put) type Parser token a = StateT [token] [] a runParser :: Parser token a -> [token] -> [(a, [token])] runParser = runStateT item :: Parser token token item = do token : tokens <- get put tokens return token

##### 2.2.3.3 Prelude extension

module PreludeExt (bool) where bool :: a -> a -> Bool -> a bool thenC elseC t = if t then thenC else elseC

### 2.3 Approach based on decoding with partial function

Now, Chaitin's construction will be here

where

- hnf
- should denote an unary predicate “has normal form” (“terminates”)
- dc
- should mean an operator “decode” (a function from finite bit sequences to combinatory logic terms)
- should denote the set of all finite bit sequences
- Dom
_{dc} - should denote the set of syntactically correct bit sequences (semantically, they may either terminate or diverge), i.e. the domain of the decoding function, i.e. the range of the coding function. Thus,
- “Absolute value”
- should mean the length of a bit sequence (not combinatory logic term evaluation!)

### 2.4 Approach based on decoding with total function

Seen above, dc was a partial function (from finite bit sequences). We can implement it e.g. as

dc :: [Bit] -> CL dc = fst . head . runParser clP

If this is confusing or annoying, then we can choose a more Haskell-like approach, making dc a total function:

dc :: [Bit] -> Maybe CL dc = fst . head . runParser (safe clP)

where

safe :: MonadPlus m => m a -> m (Maybe a) safe p = liftM Just p `mplus` return Nothing

then, Chaitin's construction will be

where should denote false truth value.

## 3 Related concepts

## 4 To do

Writing a program in Haskell -- or in combinatory logic:-) -- which could help in making conjectures on combinatory logic-based Chaitin's constructions. It would make only approximations, in a similar way that most Mandelbrot plotting softwares work: it would ask for a maximum limit of iterations.

chaitin --computation=cl --coding=tromp --limit-of-iterations=5000 --digits=10 --decimal