(** For a dependent function, the previous solution is reused thanks to a temporary hiding of the dependency in a "container" [memo_val]. *)
#[universes(template)]
Inductive memo_val {A : nat -> Type} : Type :=
memo_mval: forall n, A n -> memo_val.
Arguments memo_val : clear implicits.
Section DependentMemoFunction.
Variable A: nat -> Type.
Variable f: forall n, A n.
Notation memo_val := (memo_val A).
Fixpoint is_eq (n m : nat) : {n = m} + {True} :=
match n, m return {n = m} + {True} with
| 0, 0 =>left True (eq_refl 0)
| 0, S m1 => right (0 = S m1) I
| S n1, 0 => right (S n1 = 0) I
| S n1, S m1 =>
match is_eq n1 m1 with
| left H => left True (f_equal S H)
| right _ => right (S n1 = S m1) I
end
end.
Definition memo_get_val n (v: memo_val): A n :=
match v with
| memo_mval m x =>
match is_eq n m with
| left H =>
match H in (eq _ y) return (A y -> A n) with
| eq_refl => fun v1 : A n => v1
end
| right _ => fun _ : A m => f n
end x
end.
Let mf n :=  memo_mval n (f n).
Definition dmemo_list := memo_list _ mf.
Definition dmemo_get n l := memo_get_val n (memo_get _ n l).

Theorem dmemo_get_correct: forall n, dmemo_get n dmemo_list = f n.
Variable g: forall n, A n -> A (S n).