I'm wanting to model a family tree in the F# type system. The basic requirements:

  • A family tree is made up of family members
  • A family member has a required ancestor
  • A family member has an optional partner

My struggle is what to do with the "root" family member. I thought about making it an option:

type FamilyMember = { Name:string; Ancestor:FamilyMember option; ... }

but it seems all family members are required to come from somewhere.

So, that lead me to think an ancestor union type, where either it is an empty root or it is a descendant. Is there a cleaner way?

Here's what I have:

type Person = string
type FamilyMember = {
    Person: Person
    Partner: Person option
    Ancestor: Ancestor
and Ancestor = 
    | Root
    | Descendent of FamilyMember

let joe = { Person = "joe"; Partner = Some("joyce"); Ancestor = Root; }
let steve = { Person = "steve"; Partner = Some("pat"); Ancestor = Descendent(joe) }
let megan = { Person = "megan"; Partner = None; Ancestor = Descendent(steve)}
  • \$\begingroup\$ An ancestor could be a parent's parent so better to use the word Parent instead of Ancestor and Child instead of Descendant. \$\endgroup\$ Jun 12, 2018 at 13:36
  • \$\begingroup\$ A family tree is not a tree in the computer science sense of the word. It is (roughly) a directed acyclic graph, which cannot be stored as a simple record containing records. Hopefully that gives you something to look into. \$\endgroup\$ Jun 12, 2018 at 13:49
  • \$\begingroup\$ Everyone has 2 parents :) \$\endgroup\$ Jun 12, 2018 at 13:49
  • \$\begingroup\$ :) Thanks for the feedback! I'm interested in only modeling a single line of decent (paternal or maternal), so the second parent is simply a spouse. \$\endgroup\$ Jun 13, 2018 at 0:03
  • \$\begingroup\$ I cross posted here, and got an interesting response: stackoverflow.com/a/50842376/79113 \$\endgroup\$ Jun 18, 2018 at 12:37


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.