]> matita.cs.unibo.it Git - helm.git/blobdiff - matitaB/components/grafite_engine/grafiteEngine.ml
Matitaweb:
[helm.git] / matitaB / components / grafite_engine / grafiteEngine.ml
index cbaa4d93a8f38f8b845476910d89ca51cb8422d9..02dfb00127adddb88455891d20ff78e87f82715e 100644 (file)
@@ -34,6 +34,8 @@ type options = {
   do_heavy_checks: bool ; 
 }
 
+let prerr_endline _ = () 
+
 let basic_eval_unification_hint (t,n) status =
  NCicUnifHint.add_user_provided_hint status t n
 ;;
@@ -134,12 +136,13 @@ let eval_alias status data=
 let basic_eval_input_notation (l1,l2) status =
   GrafiteParser.extend status l1 
    (fun env loc ->
-     let rec get_disamb = function
+     (* let rec get_disamb = function
      | [] -> Stdpp.dummy_loc,None,None
-     | (_,(NotationEnv.NoType,NotationEnv.DisambiguationValue dv))::_ -> dv
+     | (_,(csym,NotationEnv.NoType,NotationEnv.DisambiguationValue dv))::_ -> dv
      | _::tl -> get_disamb tl
-     in
-     let l2' = TermContentPres.instantiate_level2 status env (get_disamb env) l2 in
+     in *)
+     let l2' = 
+       TermContentPres.instantiate_level2 status env (*(get_disamb env)*) l2 in
      prerr_endline ("new l2 ast : " ^ (NotationPp.pp_term status l2'));
      NotationPt.AttributedTerm (`Loc loc,l2')) 
 ;;
@@ -354,7 +357,7 @@ let eval_ng_tac tac =
        ) seqs)
   | GrafiteAst.NAuto (_loc, (None,a)) -> 
       NnAuto.auto_tac ~params:(None,a) ?trace_ref:None
-  | GrafiteAst.NAuto (_loc, (Some l,a)) ->
+  | GrafiteAst.NAuto (_loc, (Some (_,l),a)) ->
       NnAuto.auto_tac
        ~params:(Some List.map (fun x -> "",0,x) l,a) ?trace_ref:None
   | GrafiteAst.NBranch _ -> NTactics.branch_tac ~force:false
@@ -493,13 +496,40 @@ let rec eval_ncommand ~include_paths opts status (text,prefix_len,cmd) =
       GrafiteTypes.Serializer.require
        ~alias_only ~baseuri ~fname:fullpath status
   | GrafiteAst.UnificationHint (loc, t, n) -> eval_unification_hint status t n
-  | GrafiteAst.NCoercion (loc, name, t, ty, source, target) ->
+  | GrafiteAst.NCoercion (loc, name, compose, None) ->
+     let status, t, ty, source, target =
+       let o_t = NotationPt.Ident (name,`Ambiguous) in
+       let metasenv,subst, status,t =
+         GrafiteDisambiguate.disambiguate_nterm 
+           status None [] [] [] ("",0,o_t) in
+       assert( metasenv = [] && subst = []);
+       let ty = NCicTypeChecker.typeof status [] [] [] t in
+       let source, target =
+         let clean = function
+         | NCic.Appl (NCic.Const _ as r :: l) -> 
+             NotationPt.Appl (NotationPt.NCic r ::
+               List.map (fun _ -> NotationPt.Implicit `JustOne)l)
+         | NCic.Const _ as r -> NotationPt.NCic r
+         | _ -> assert false in
+         let rec aux = function
+         | NCic.Prod (_,_, (NCic.Prod _ as rest)) -> aux rest
+         | NCic.Prod (name, src, tgt) -> (name, clean src), clean tgt
+         | _ -> assert false in aux ty in
+       status, o_t, NotationPt.NCic ty, source, target in
      let status, composites =
-      NCicCoercDeclaration.eval_ncoercion status name t ty source target in
+      NCicCoercDeclaration.eval_ncoercion status name compose t ty source
+       target in
      let mode = GrafiteAst.WithPreferences in (* MATITA 1.0: fixme *)
      let aliases = GrafiteDisambiguate.aliases_for_objs status composites in
       eval_alias status (mode,aliases)
-  | GrafiteAst.NQed loc ->
+  | GrafiteAst.NCoercion (loc, name, compose, Some (t, ty, source, target)) ->
+     let status, composites =
+      NCicCoercDeclaration.eval_ncoercion status name compose t ty source
+       target in
+     let mode = GrafiteAst.WithPreferences in (* MATITA 1.0: fixme *)
+     let aliases = GrafiteDisambiguate.aliases_for_objs status composites in
+      eval_alias status (mode,aliases)
+  | GrafiteAst.NQed (loc,index) ->
      if status#ng_mode <> `ProofMode then
       raise (GrafiteTypes.Command_error "Not in proof mode")
      else
@@ -519,7 +549,7 @@ let rec eval_ncommand ~include_paths opts status (text,prefix_len,cmd) =
               (match spec with
               | NReference.Def _ -> NReference.Def height
               | NReference.Fix (i,j,_) -> NReference.Fix(i,j,height)
-              | NReference.CoFix _ -> NReference.CoFix height
+              | NReference.CoFix i -> NReference.CoFix i
               | NReference.Ind _ | NReference.Con _
               | NReference.Decl as s -> s))
           | t -> NCicUtils.map status (fun _ () -> ()) () fix t
@@ -534,7 +564,7 @@ let rec eval_ncommand ~include_paths opts status (text,prefix_len,cmd) =
         let obj = uri,height,[],[],obj_kind in
         let old_status = status in
         let status = NCicLibrary.add_obj status obj in
-        let index_obj =
+        let index_obj = index &&
          match obj_kind with
             NCic.Constant (_,_,_,_,(_,`Example,_))
           | NCic.Fixpoint (_,_,(_,`Example,_)) -> false
@@ -568,7 +598,7 @@ let rec eval_ncommand ~include_paths opts status (text,prefix_len,cmd) =
                try
                 let nstatus =
                  eval_ncommand ~include_paths opts status
-                  ("",0,GrafiteAst.NObj (HExtlib.dummy_floc,boxml))
+                  ("",0,GrafiteAst.NObj (HExtlib.dummy_floc,boxml,true))
                 in
                 if nstatus#ng_mode <> `CommandMode then
                   begin
@@ -579,16 +609,17 @@ let rec eval_ncommand ~include_paths opts status (text,prefix_len,cmd) =
                 else
                   nstatus
                with
-               | MultiPassDisambiguator.DisambiguationError _
+               | GrafiteDisambiguate.Error _
                | NCicTypeChecker.TypeCheckerFailure _ ->
                   (*HLog.warn "error in generating projection/eliminator";*)
                   status
              ) status boxml in             
            let _,_,_,_,nobj = obj in 
            let status = match nobj with
-               NCic.Inductive (is_ind,leftno,[it],_) ->
-                 let _,ind_name,ty,cl = it in
-                 List.fold_left 
+               NCic.Inductive (is_ind,leftno,itl,_) ->
+                 List.fold_left (fun status it ->      
+                 (let _,ind_name,ty,cl = it in
+                  List.fold_left 
                    (fun status outsort ->
                       let status = status#set_ng_mode `ProofMode in
                       try
@@ -600,7 +631,7 @@ let rec eval_ncommand ~include_paths opts status (text,prefix_len,cmd) =
                        let _,_,menv,_,_ = invobj in
                         (match menv with
                              [] -> eval_ncommand ~include_paths opts status
-                                    ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
+                                    ("",0,GrafiteAst.NQed (Stdpp.dummy_loc,false))
                            | _ -> status))
                        (* XXX *)
                       with _ -> (*HLog.warn "error in generating inversion principle"; *)
@@ -608,7 +639,66 @@ let rec eval_ncommand ~include_paths opts status (text,prefix_len,cmd) =
                   status
                   (NCic.Prop::
                     List.map (fun s -> NCic.Type s)
-                    (NCicEnvironment.get_universes status))
+                    (NCicEnvironment.get_universes status)))) status itl
+              | _ -> status
+           in
+           let status = match nobj with
+               NCic.Inductive (is_ind,leftno,itl,_) ->
+                 (* first leibniz *)
+                 let status' = List.fold_left
+                   (fun status it ->
+                      let _,ind_name,ty,cl = it in
+                      let status = status#set_ng_mode `ProofMode in
+                      try
+                       prerr_endline ("creazione discriminatore per " ^ ind_name);
+                       (let status,invobj =
+                         NDestructTac.mk_discriminator ~use_jmeq:false
+                          (ind_name ^ "_discr")
+                          it leftno status status#baseuri in
+                       let _,_,menv,_,_ = invobj in
+                        (match menv with
+                             [] -> eval_ncommand ~include_paths opts status
+                                    ("",0,GrafiteAst.NQed(Stdpp.dummy_loc,false))
+                           | _ -> status))
+                       (* XXX *)
+                      with 
+                      | NDestructTac.ConstructorTooBig k -> 
+                          prerr_endline (Printf.sprintf 
+                           "unable to generate leibniz discrimination principle (constructor %s too big)"
+                           k);
+                          HLog.warn (Printf.sprintf 
+                           "unable to generate leibniz discrimination principle (constructor %s too big)"
+                           k);
+                           let status = status#set_ng_mode `CommandMode in status
+                      | _ -> 
+                          prerr_endline "error in generating discrimination principle";
+                          (*HLog.warn "error in generating discrimination principle"; *)
+                                let status = status#set_ng_mode `CommandMode in
+                                status)
+                  status itl
+                in
+                (* then JMeq *)
+                List.fold_left
+                   (fun status it ->
+                      let _,ind_name,ty,cl = it in
+                      let status = status#set_ng_mode `ProofMode in
+                      try
+                       prerr_endline ("creazione discriminatore per " ^ ind_name);
+                       (let status,invobj =
+                         NDestructTac.mk_discriminator ~use_jmeq:true
+                          (ind_name ^ "_jmdiscr")
+                          it leftno status status#baseuri in
+                       let _,_,menv,_,_ = invobj in
+                        (match menv with
+                             [] -> eval_ncommand ~include_paths opts status
+                                    ("",0,GrafiteAst.NQed(Stdpp.dummy_loc,false))
+                           | _ -> status))
+                       (* XXX *)
+                      with _ -> (*HLog.warn "error in generating discrimination principle"; *)
+                          prerr_endline "error in generating discrimination principle";
+                                let status = status#set_ng_mode `CommandMode in
+                                status)
+                  status' itl
               | _ -> status
            in
            let coercions =
@@ -631,10 +721,11 @@ let rec eval_ncommand ~include_paths opts status (text,prefix_len,cmd) =
                  let status, nuris = 
                    NCicCoercDeclaration.
                      basic_eval_and_record_ncoercion_from_t_cpos_arity 
-                      status (name,t,cpos,arity) in
+                      
+                     status (name,true,t,cpos,arity) in
                  let aliases = GrafiteDisambiguate.aliases_for_objs status nuris in
                   eval_alias status (mode,aliases)
-               with MultiPassDisambiguator.DisambiguationError _-> 
+               with GrafiteDisambiguate.Error _ -> 
                  HLog.warn ("error in generating coercion: "^name);
                  status) 
              status coercions
@@ -674,8 +765,8 @@ let rec eval_ncommand ~include_paths opts status (text,prefix_len,cmd) =
        let status = status#set_stack ninitial_stack in
        let status = subst_metasenv_and_fix_names status in
        let status = status#set_ng_mode `ProofMode in
-       eval_ncommand ~include_paths opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
-  | GrafiteAst.NObj (loc,obj) ->
+       eval_ncommand ~include_paths opts status ("",0,GrafiteAst.NQed (Stdpp.dummy_loc,false))
+  | GrafiteAst.NObj (loc,obj,index) ->
      if status#ng_mode <> `CommandMode then
       raise (GrafiteTypes.Command_error "Not in command mode")
      else
@@ -690,26 +781,30 @@ let rec eval_ncommand ~include_paths opts status (text,prefix_len,cmd) =
       let status = status#set_ng_mode `ProofMode in
       (match nmenv with
           [] ->
-           eval_ncommand ~include_paths opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
+           eval_ncommand ~include_paths opts status ("",0,GrafiteAst.NQed
+           (Stdpp.dummy_loc,index))
         | _ -> status)
-  | GrafiteAst.NDiscriminator (_,_) -> assert false (*(loc, indty) ->
+  | GrafiteAst.NDiscriminator (loc, indty) -> 
       if status#ng_mode <> `CommandMode then
         raise (GrafiteTypes.Command_error "Not in command mode")
       else
         let status = status#set_ng_mode `ProofMode in
         let metasenv,subst,status,indty =
-          GrafiteDisambiguate.disambiguate_nterm None status [] [] [] (text,prefix_len,indty) in
-        let indtyno, (_,_,tys,_,_) = match indty with
-            NCic.Const ((NReference.Ref (_,NReference.Ind (_,indtyno,_))) as r) ->
-              indtyno, NCicEnvironment.get_checked_indtys r
+          GrafiteDisambiguate.disambiguate_nterm status None [] [] [] (text,prefix_len,indty) in
+        let indtyno, (_,_,tys,_,_), leftno = match indty with
+            NCic.Const ((NReference.Ref (_,NReference.Ind (_,indtyno,leftno))) as r) ->
+              indtyno, NCicEnvironment.get_checked_indtys status r, leftno
           | _ -> prerr_endline ("engine: indty expected... (fix this error message)"); assert false in
-        let it = List.nth tys indtyno in
-        let status,obj =  NDestructTac.mk_discriminator it status in
+        let (_,ind_name,_,_ as it) = List.nth tys indtyno in
+        let status,obj =
+          NDestructTac.mk_discriminator ~use_jmeq:true (ind_name ^ "_jmdiscr")
+           it leftno status status#baseuri in
         let _,_,menv,_,_ = obj in
           (match menv with
-               [] -> eval_ncommand ~include_paths opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
+               [] -> eval_ncommand ~include_paths opts status
+                 ("",0,GrafiteAst.NQed(Stdpp.dummy_loc,false))
              | _ -> prerr_endline ("Discriminator: non empty metasenv");
-                    status, []) *)
+                    status)
   | GrafiteAst.NInverter (loc, name, indty, selection, sort) ->
      if status#ng_mode <> `CommandMode then
       raise (GrafiteTypes.Command_error "Not in command mode")
@@ -751,7 +846,7 @@ let rec eval_ncommand ~include_paths opts status (text,prefix_len,cmd) =
        (match menv with
           [] ->
             eval_ncommand ~include_paths opts status
-             ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
+             ("",0,GrafiteAst.NQed(Stdpp.dummy_loc,false))
         | _ -> assert false)
   | GrafiteAst.NUnivConstraint (loc,u1,u2) ->
       eval_add_constraint status [`Type,u1] [`Type,u2]
@@ -799,12 +894,14 @@ let rec eval_executable ~include_paths opts status (text,prefix_len,ex) =
   match ex with
   | GrafiteAst.NTactic (_(*loc*), tacl) ->
       if status#ng_mode <> `ProofMode then
-       raise (GrafiteTypes.Command_error "Not in proof mode")
+       (prerr_endline ("not in proof mode 2: " ^ GrafiteAstPp.pp_executable status ~map_unicode_to_tex:false ex);
+       raise (GrafiteTypes.Command_error "Not in proof mode"))
       else
        let status =
         List.fold_left 
           (fun status tac ->
             let status = eval_ng_tac (text,prefix_len,tac) status in
+            prerr_endline "prima di subst_metasenv_and_fix_names";
             subst_metasenv_and_fix_names status)
           status tacl
        in