Definition active_term_order_superset t t' :=
  lexprod lt value_list_order (rank (fst t), snd t) (rank (fst t'), snd t').Definition active_term_order_superset_wf : well_founded active_term_order_superset.Fixpoint unify (s : list (variable * value)) (v : value) (p : pattern) {struct p} :
  option (list (variable * value)) :=
  let unify_list := fix unify_list
    (s : list (variable * value)) (lv : list value) (lp : list pattern) {struct lp} :
    option (list (variable * value)) :=
    match lv, lp with
    | [], [] => Some s
    | v::lv', p::lp' => match unify s v p with
      | None => None
      | Some s' => unify_list s' lv' lp'
      end
    | _, _ => None
    end in
  match p with
  |  p_var x =>
    match assoc (variable_beq variable_eq_dec) x s with
    | None => Some ((x, v) :: s)
    | Some v' => if value_beq constructor_eq_dec v v' then Some s else None
    end
  | p_capply c' lp =>match v with
      | c_capply c lv => if constructor_beq constructor_eq_dec c c' then unify_list s lv lp else None
      end
  end.Fixpoint unify_list_aux (s : list (variable * value)) (lv : list value) (lp : list pattern) :
  option (list (variable * value)) :=
  match lv, lp with
  | [], [] => Some s
  | v::lv', p::lp' => match unify s v p with
    | None => None
    | Some s' => unify_list_aux s' lv' lp'
    end
  | _, _ => None
  end.

Lemma unify_equation (s : list (variable * value)) (v : value) (p : pattern) : unify s v p = match p with | p_var x => match assoc (variable_beq variable_eq_dec) x s with | None => Some ((x, v) :: s) | Some v' => if value_beq constructor_eq_dec v v' then Some s else None end | p_capply c' lp => match v with | c_capply c lv => if constructor_beq constructor_eq_dec c c' then unify_list_aux s lv lp else None end end.