]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/helena/src/automath/autCrg.ml
new intermediate language complete_rg,
[helm.git] / helm / software / helena / src / automath / autCrg.ml
index dfd51aa47d0e3392c67a6802dcc1b2e091bdd7ae..025459d2e6df9c9595064ed356c585b5a6ccfce1 100644 (file)
@@ -44,12 +44,12 @@ let hcnt = K.create hcnt_size (* optimized context *)
 
 (* Internal functions *******************************************************)
 
-let empty_cnt = [], []
+let empty_cnt = D.ESort
 
-let add_abst (a, ws) id w = 
-   E.Name (id, true) :: a, w :: ws 
+let add_abst cnt id w = 
+   D.EBind (cnt, [E.Name (id, true)], D.Abst (N.infinite, w)) 
 
-let mk_lref f i j k = f (D.TLRef ([E.Apix k], i, j))
+let mk_lref f i = f (D.TLRef ([], i))
 
 let id_of_name (id, _, _) = id
 
@@ -82,7 +82,7 @@ let relax_opt_qid f st = function
    | Some qid -> let f qid = f (Some qid) in relax_qid f st qid
 
 let resolve_gref err f st qid =
-   try let cnt = K.find henv (uri_of_qid qid) in f qid cnt
+   try let age, cnt = K.find henv (uri_of_qid qid) in f qid age cnt
    with Not_found -> err qid 
 
 let resolve_gref_relaxed f st qid =
@@ -101,15 +101,16 @@ let get_cnt_relaxed f st =
    let rec err node = relax_opt_qid (get_cnt err f st) st node in
    get_cnt err f st st.node
 
-(****************************************************************************)
+let push_abst f a w lenv =
+   let bw = D.Abst (N.infinite, w) in
+   D.push_bind f a bw lenv
 
-let push_abst f lenv a w =
-   let bw = D.Abst (N.infinite, [w]) in
-   let f lenv = f lenv in
-   D.push_bind f lenv a bw
+let add_proj e t = match e with
+   | D.ESort                 -> t
+   | D.EBind (D.ESort, a, b) -> D.TBind (a, b, t) 
+   | _                       -> D.TProj ([], e, t)
 
-let lenv_of_cnt (a, ws) = 
-   D.push_bind C.start D.empty_lenv a (D.Abst (N.infinite, ws))
+let lenv_of_cnt cnt = cnt
 
 (* this is not tail recursive in the GRef branch *)
 let rec xlate_term f st lenv = function
@@ -117,36 +118,38 @@ let rec xlate_term f st lenv = function
       let f h = f (D.TSort ([], h)) in
       if s then f 0 else f 1
    | A.Appl (v, t)       ->
-      let f vv tt = f (D.TAppl ([], [vv], tt)) in
+      let f vv tt = f (D.TAppl ([], vv, tt)) in
       let f vv = xlate_term (f vv) st lenv t in
       xlate_term f st lenv v
    | A.Abst (name, w, t) ->
       let f ww = 
          let a = [E.Name (name, true)] in
         let f tt =
-           let b = D.Abst (N.infinite, [ww]) in
+           let b = D.Abst (N.infinite, ww) in
            f (D.TBind (a, b, tt))
         in
          let f lenv = xlate_term f st lenv t in
-        push_abst f lenv a ww
+        push_abst f a ww lenv
       in
       xlate_term f st lenv w
    | A.GRef (name, args) ->
-      let map1 f = function
-           | E.Name (id, _) -> f (A.GRef ((id, true, []), []))
-           | _              -> C.err ()
+      let map1 args a =
+         let f id _ = A.GRef ((id, true, []), []) :: args in
+         E.name C.err f a
       in
-      let map2 f t = xlate_term f st lenv t in
-      let g qid (a, _) =
-         let gref age = D.TGRef ([age], uri_of_qid qid) in
-        match args, a with
-           | [], [age]    -> f (gref age)
-           | _ , age :: a ->
-              let f args = f (D.TAppl ([], args, gref age)) in
-              let f args = C.list_rev_map f map2 args in
-              let f a = C.list_rev_map_append f map1 a ~tail:args in
-              C.list_sub_strict f a args
-            | _             -> assert false
+      let map2 f arg args = 
+         let f arg = f (D.EAppl (args, [], arg)) in 
+         xlate_term f st lenv arg
+      in
+      let g qid age cnt =
+         let gref = D.TGRef ([age], uri_of_qid qid) in
+        if cnt = D.ESort then f gref else
+        let f = function 
+            | D.EAppl (D.ESort, a, v) -> f (D.TAppl (a, v, gref))
+            | args                    -> f (D.TProj ([], args, gref))
+         in
+        let f args = C.list_fold_right f map2 args D.ESort in
+        D.sub_list_strict (D.fold_attrs f map1 args) cnt args
       in
       let g qid = resolve_gref_relaxed g st qid in
       let err () = complete_qid g st name in
@@ -169,28 +172,23 @@ let xlate_entity err f st = function
    | A.Block (name, w)         ->
       let f qid = 
          let f cnt =
-           let lenv = lenv_of_cnt cnt in
            let f ww = 
               K.add hcnt (uri_of_qid qid) (add_abst cnt name ww);
               err {st with node = Some qid}
            in
-           xlate_term f st lenv w
+           xlate_term f st cnt w
         in
          get_cnt_relaxed f st
       in
       complete_qid f st (name, true, [])
    | A.Decl (name, w)          ->
       let f cnt =
-         let a, ws = cnt in
          let lenv = lenv_of_cnt cnt in
         let f qid = 
             let f ww =
                let age = E.Apix st.line in
-               K.add henv (uri_of_qid qid) (age :: a, ws);
-              let t = match ws with
-                 | [] -> ww
-                 | _  -> D.TBind (a, D.Abst (N.infinite, ws), ww)
-              in
+               K.add henv (uri_of_qid qid) (age, cnt);
+              let t = add_proj lenv ww in
 (*
            print_newline (); CrgOutput.pp_term print_string t;
 *)
@@ -204,18 +202,14 @@ let xlate_entity err f st = function
       in
       get_cnt_relaxed f st
    | A.Def (name, w, trans, v) ->
-      let f cnt = 
-        let a, ws = cnt in
+      let f cnt =
         let lenv = lenv_of_cnt cnt in
          let f qid = 
             let f ww =
               let f vv =
                   let age = E.Apix st.line in
-                  K.add henv (uri_of_qid qid) (age :: a, ws);
-                  let t = match ws with
-                    | [] -> D.TCast ([], ww, vv)
-                    | _  -> D.TBind (a, D.Abst (N.infinite, ws), D.TCast ([], ww, vv))
-                 in
+                  K.add henv (uri_of_qid qid) (age, cnt);
+                  let t = add_proj lenv (D.TCast ([], ww, vv)) in
 (*
            print_newline (); CrgOutput.pp_term print_string t;
 *)