4

Suppose I have a set A ⊆ nat. I want to model in Isabelle a function f : A ⇒ Y. I could use either:

  1. a partial function, i.e. one of type nat ⇒ Y option, or
  2. a total function, i.e. one of type nat ⇒ Y that is unspecified for inputs not in A.

I wonder which is the 'better' option. I see a couple of factors:

  • The "partial function" approach is better because it is easier to compare partial functions for equality. That is, if I want to see if f is equal to another function, g : A ⇒ Y, then I just say f = g. To compare under-specified total functions f and g, I would have to say ∀x ∈ A. f x = g x.

  • The "under-specified total function" approach is better because I don't have to faff with the constructing/deconstructing option types all the time. For instance, if f is an under-specified total function, and x ∈ A, then I can just say f x, but if f is a partial function I would have to say (the ∘ f) x. For another instance, it's trickier to do function composition on partial functions than on total functions.

For a concrete instance relevant to this question, consider the following attempt at formalising simple graphs.

type_synonym node = nat
record 'a graph = 
  V :: "node set"
  E :: "(node × node) set"
  label :: "node ⇒ 'a"

A graph comprises a set of nodes, an edge relation between them, and a label for each node. We only care about the label of nodes that are in V. So, should label be a partial function node ⇒ 'a option with dom label = V, or should it just be a total function that is unspecified outside of V?

John Wickerson
  • 1,204
  • 12
  • 23

1 Answers1

2

It is probably a matter of taste and may also depend on the use you have in mind, so I'll just give you my personal taste, which would be option 2. the total function. The reason is that I think the bounded quantification in both approaches will be unavoidable anyway. I think that with approach 1. you will find that the easiest way to handle the Option is to limit the domain (bounded quantification) that you are reasoning about. As for the graph example, graph theorems always say something like for all nodes in V. But as I said, it is probably a matter of taste.

Bryan Olivier
  • 5,207
  • 2
  • 16
  • 18
  • Thanks Bryan. Then I'll defend the "partial function" approach... If `label` is partial, then equivalence of graphs `G` and `H` is just ordinary term-equality, `G=H`. But if `label` is total, then the equivalence is more complicated, and hence much harder to reason about. – John Wickerson Mar 14 '13 at 15:58
  • 1
    Which gives rise to the question: "how important is term equality for graphs?". Maybe two graphs being isomorphic is more interesting. – chris Mar 15 '13 at 02:19
  • Indeed, so we're going to need a complicated equivalence regardless. I guess that's a downside of me using graphs as my illustrative example. – John Wickerson Mar 15 '13 at 07:30