简体   繁体   中英

HaltingProblem in Agda?

I am working through a paper trying to implement their Haskell code in Agda. They want to formulate the halting problem by saying let bot be a program such that for any data type a :

bot :: a
bot = bot

They go on to define

data S = T

so the Halting problem is said as:

The function diverges : S → S defined by

diverges(T)= bot
diverges(bot)= T

is not computable and hence is not definable in our language

I tried implementing this in Agda as:

data S : Set where
  ⊤ : S

⊥ : _
⊥ = ⊥

diverges : S → S
diverges ⊤ = ⊥
diverges ⊥ = ⊤

and when I tried to load it, Agda said diverges ⊥ = ⊤ is an unreachable clause. Is this the error I am supposed to get or did I just implement the Haskell code incorrectly?

Your project probably isn't going to work, because Agda is not a Turing-complete language. For one thing, it only allows total functions, so it can't model any computation that might not halt. This means it can't even model full computations on Turing machines, for example, since Turing machines can fail to stop. So it's very unlikely that all the programs in the paper can be implemented in Agda.

In fact, it's not even possible to code all total computations in Agda, by a simple diagonal argument. Imagine the following algorithm: "Examine a text file and determine whether it's legal Agda. If it's not, output the empty string; if it is, run that Agda program on that very same text file, and add a space to the end of the output." This is a well-defined algorithm; most of the complexity is in "determine whether it's legal Agda" and "run that Agda program", and programs that do these certainly exist. But no Agda program can implement it, because any candidate would give different output when run on its own source code. Any total language is subject to a similar argument.

Weird circular-with-a-twist arguments like this are at the core of understanding the Halting Problem, so the paper you're reading will probably contain many of them.

Incidentally, the function diverges is not definable in Haskell. Computable functions in Haskell must give more defined outputs for more defined inputs, and ⊤ is considered more defined than ⊥ (it's an actual value, as opposed to the symbol for "this is undefined").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM