I’ve been using the following data structure for the representation of propositional logic in Haskell:
data Prop
= Pred String
| Not Prop
| And Prop Prop
| Or Prop Prop
| Impl Prop Prop
| Equiv Prop Prop
deriving (Eq, Ord)
Any comments on this structure are welcome.
However, now I want to extend my algoritms to handle FOL – predicate logic.
What would be a good way of representing FOL in Haskell?
I’ve seen versions that are – pretty much – an extension of the above, and versions that are based on more classic context-free grammars. Is there any literature on this, that could be recommended?
This is known as higher-order abstract syntax.
First solution: Use Haskell’s lambda.
A datatype could look like:
You can write a formula as:
This is described in detail in in The Monad Reader article. Highly recommended.
Second solution:
Use strings like
Then you can write a formula like
The advantage is that you can show the formula easily (it’s hard to show a
Obj -> Propfunction). The disadvantage is that you have to write changing names on collision (~alpha conversion) and substitution (~beta conversion). In both solutions, you can use GADT instead of two datatypes:Third solution: Use numerals to represent where the variable is bound, where lower means deeper. For example, in ForAll (Exists (Equals (Num 0) (Num 1))) the first variable will bind to Exists, and second to ForAll. This is known as de Bruijn numerals. See I am not a number – I am a free variable.