+and opt1_appl g es c t vs =
+ let g vs =
+ let g = function
+ | C.LetIn (mame, vv, tt) ->
+ let vs = List.map (S.lift 1) vs in
+ let x = C.LetIn (mame, vv, C.Appl (tt :: vs)) in
+ HLog.warn "Optimizer: swap 2"; opt1_proof g true c x
+ | C.Lambda (name, ww, tt) ->
+ let v, vs = List.hd vs, List.tl vs in
+ let x = C.Appl (C.LetIn (name, v, tt) :: vs) in
+ HLog.warn "Optimizer: remove 2"; opt1_proof g true c x
+ | C.Appl vvs ->
+ let x = C.Appl (vvs @ vs) in
+ HLog.warn "Optimizer: nested application"; opt1_proof g true c x
+ | t ->
+ let rec aux d rvs = function
+ | [], _ ->
+ let x = C.Appl (t :: List.rev rvs) in
+ if d then opt1_proof g true c x else g x
+ | v :: vs, (cc, bb) :: cs ->
+ if H.is_not_atomic v && I.S.mem 0 cc && bb then begin
+ HLog.warn "Optimizer: anticipate 1";
+ aux true (define v :: rvs) (vs, cs)
+ end else
+ aux d (v :: rvs) (vs, cs)
+ | _, [] -> assert false
+ in
+ let h () =
+ let classes, conclusion = Cl.classify c (H.get_type c t) in
+ let csno, vsno = List.length classes, List.length vs in
+ if csno < vsno then
+ let vvs, vs = HEL.split_nth csno vs in
+ let x = C.Appl (define (C.Appl (t :: vvs)) :: vs) in
+ HLog.warn "Optimizer: anticipate 2"; opt1_proof g true c x
+ else match conclusion, List.rev vs with
+ | Some _, rv :: rvs when csno = vsno && H.is_not_atomic rv ->
+ let x = C.Appl (t :: List.rev rvs @ [define rv]) in
+ HLog.warn "Optimizer: anticipate 3"; opt1_proof g true c x
+ | Some _, _ ->
+ g (C.Appl (t :: vs))
+ | None, _ ->
+ aux false [] (vs, classes)
+ in
+ let rec aux h prev = function
+ | C.LetIn (name, vv, tt) :: vs ->
+ let t = S.lift 1 t in
+ let prev = List.map (S.lift 1) prev in
+ let vs = List.map (S.lift 1) vs in
+ let y = C.Appl (t :: List.rev prev @ tt :: vs) in
+ let x = C.LetIn (name, vv, y) in
+ HLog.warn "Optimizer: swap 3"; opt1_proof g true c x
+ | v :: vs -> aux h (v :: prev) vs
+ | [] -> h ()
+ in
+ aux h [] vs
+ in
+ if es then opt1_proof g es c t else g t
+ in
+ if es then H.list_map_cps g (fun h -> opt1_term h es c) vs else g vs