# Hierarchy (Family Tree) Domain Model

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)}
megan

• An ancestor could be a parent's parent so better to use the word Parent instead of Ancestor and Child instead of Descendant. – TheQuickBrownFox Jun 12 '18 at 13:36
• 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. – TheQuickBrownFox Jun 12 '18 at 13:49
• Everyone has 2 parents :) – TheQuickBrownFox Jun 12 '18 at 13:49
• :) 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. – David Faivre Jun 13 '18 at 0:03
• I cross posted here, and got an interesting response: stackoverflow.com/a/50842376/79113 – David Faivre Jun 18 '18 at 12:37