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\$ – TheQuickBrownFox Jun 12 '18 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\$ – TheQuickBrownFox Jun 12 '18 at 13:49
  • \$\begingroup\$ Everyone has 2 parents :) \$\endgroup\$ – TheQuickBrownFox Jun 12 '18 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\$ – David Faivre Jun 13 '18 at 0:03
  • \$\begingroup\$ I cross posted here, and got an interesting response: stackoverflow.com/a/50842376/79113 \$\endgroup\$ – David Faivre Jun 18 '18 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.