]> matita.cs.unibo.it Git - helm.git/commitdiff
snapshot
authorStefano Zacchiroli <zack@upsilon.cc>
Tue, 5 Jul 2005 12:23:03 +0000 (12:23 +0000)
committerStefano Zacchiroli <zack@upsilon.cc>
Tue, 5 Jul 2005 12:23:03 +0000 (12:23 +0000)
- preliminary support for MathML markup generation

12 files changed:
helm/ocaml/cic_notation/.depend
helm/ocaml/cic_notation/Makefile
helm/ocaml/cic_notation/TODO
helm/ocaml/cic_notation/cicNotationParser.expanded.ml
helm/ocaml/cic_notation/cicNotationParser.ml
helm/ocaml/cic_notation/cicNotationPp.ml
helm/ocaml/cic_notation/cicNotationPres.ml [new file with mode: 0644]
helm/ocaml/cic_notation/cicNotationPres.mli [new file with mode: 0644]
helm/ocaml/cic_notation/cicNotationPt.ml
helm/ocaml/cic_notation/cicNotationUtil.ml
helm/ocaml/cic_notation/test_parser.conf.xml
helm/ocaml/cic_notation/test_parser.ml

index 6b0466b69086f192c9af00e8f522a53dc58ef836..808549afc122bf5ef1d9e509f293251c68fcef15 100644 (file)
@@ -6,6 +6,7 @@ cicNotationMatcher.cmi: cicNotationPt.cmo cicNotationEnv.cmi
 cicNotationFwd.cmi: cicNotationPt.cmo cicNotationEnv.cmi 
 cicNotationRew.cmi: cicNotationPt.cmo 
 cicNotationParser.cmi: cicNotationPt.cmo cicNotationEnv.cmi 
+cicNotationPres.cmi: cicNotationPt.cmo 
 cicNotationUtil.cmo: cicNotationPt.cmo cicNotationUtil.cmi 
 cicNotationUtil.cmx: cicNotationPt.cmx cicNotationUtil.cmi 
 cicNotationTag.cmo: cicNotationUtil.cmi cicNotationPt.cmo cicNotationTag.cmi 
@@ -36,3 +37,5 @@ cicNotationParser.cmo: cicNotationUtil.cmi cicNotationPt.cmo \
 cicNotationParser.cmx: cicNotationUtil.cmx cicNotationPt.cmx \
     cicNotationPp.cmx cicNotationLexer.cmx cicNotationEnv.cmx \
     cicNotationParser.cmi 
+cicNotationPres.cmo: cicNotationPt.cmo cicNotationPres.cmi 
+cicNotationPres.cmx: cicNotationPt.cmx cicNotationPres.cmi 
index 76048ac8070d8c7dab979a73812d5f50c0b139a9..92d6560fccb9c9a7aa4add2246acb805b50ddf97 100644 (file)
@@ -6,6 +6,7 @@ REQUIRES = \
        helm-utf8_macros        \
        camlp4.gramlib          \
        helm-cic_proof_checking \
+       helm-cic_transformations\
        ulex                    \
        $(NULL)
 INTERFACE_FILES = \
@@ -18,6 +19,7 @@ INTERFACE_FILES = \
        cicNotationFwd.mli      \
        cicNotationRew.mli      \
        cicNotationParser.mli   \
+       cicNotationPres.mli     \
        $(NULL)
 IMPLEMENTATION_FILES = \
        cicNotationPt.ml        \
@@ -40,6 +42,8 @@ cicNotationLexer.cmx: OCAMLOPT = $(OCAMLOPT_P4)
 cicNotationParser.cmx: OCAMLOPT = $(OCAMLOPT_P4)
 cicNotationParser.ml.annot: OCAMLC = $(OCAMLC_P4)
 cicNotationLexer.ml.annot: OCAMLC = $(OCAMLC_P4)
+cicNotationPres.cmi: OCAMLOPTIONS += -rectypes
+cicNotationPres.cmo: OCAMLOPTIONS += -rectypes
 
 clean: extra_clean
 distclean: extra_clean
index b7e910e687023c8a8f9412a50f954fbe3a923f5e..2ba0283ce6b9c4f9279804df58f7f3c171c4baf1 100644 (file)
@@ -15,7 +15,9 @@ TODO
   - studiare/implementare sintassi con ... per i magic fold
 * integrazione
   - porting della disambiguazione al nuovo ast
-  - apportare all'ast le modifiche di CicAst (cast, case)
+  - apportare all'ast le modifiche di CicAst (case, cast non come annotazione,
+    tipi opzionali nel let rec e nelle definizioni)
+* href multipli
 
 DONE
 
index 7b90be54a144ab610d6e2297c73c05a9b93000e2..0adf84723ce7587f88c66f5018a3d8320e373f68 100644 (file)
@@ -115,7 +115,7 @@ let extract_term_production pattern =
         [NoBinding, symbol "\\ROOT"] @ aux p2 @ [NoBinding, symbol "\\OF"] @
           aux p1
     | Sqrt p -> [NoBinding, symbol "\\SQRT"] @ aux p
-    | Break -> []
+(*     | Break -> [] *)
     | Box (_, pl) -> List.flatten (List.map aux pl)
   and aux_magic magic =
     match magic with
index 6bc494cdc5801be74a740972c16abf665dd60c9d..681dfd5e50b83b0369445b7e75280ad35620e38f 100644 (file)
@@ -144,7 +144,7 @@ let extract_term_production pattern =
         [NoBinding, symbol "\\ROOT"] @ aux p2 @ [NoBinding, symbol "\\OF"]
         @ aux p1
     | Sqrt p -> [NoBinding, symbol "\\SQRT"] @ aux p
-    | Break -> []
+(*     | Break -> [] *)
     | Box (_, pl) -> List.flatten (List.map aux pl)
   and aux_magic magic =
     match magic with
@@ -323,7 +323,11 @@ EXTEND
           return_term loc (Layout (Box (H, p)))
       | SYMBOL "\\VBOX"; DELIM "\\["; p = l1_pattern; DELIM "\\]" ->
           return_term loc (Layout (Box (V, p)))
-      | SYMBOL "\\BREAK" -> return_term loc (Layout Break)
+      | SYMBOL "\\HVBOX"; DELIM "\\["; p = l1_pattern; DELIM "\\]" ->
+          return_term loc (Layout (Box (HV, p)))
+      | SYMBOL "\\HOVBOX"; DELIM "\\["; p = l1_pattern; DELIM "\\]" ->
+          return_term loc (Layout (Box (HOV, p)))
+(*       | SYMBOL "\\BREAK" -> return_term loc (Layout Break) *)
       | DELIM "\\["; p = l1_pattern; DELIM "\\]" ->
           return_term loc (CicNotationUtil.boxify p)
       | p = SELF; SYMBOL "\\AS"; id = IDENT ->
index bc9f33f51963bacd821efb8e6e130844925f26c4..d815506378f90924fa8cd79eda5f4b2cbf09e5bb 100644 (file)
@@ -129,11 +129,15 @@ and pp_layout = function
   | Sqrt t -> sprintf "\\SQRT %s" (pp_term t)
   | Root (arg, index) ->
       sprintf "\\ROOT %s \\OF %s" (pp_term index) (pp_term arg)
-  | Break -> "\\BREAK"
+(*   | Break -> "\\BREAK" *)
   | Box (H, terms) ->
       sprintf "\\HBOX [%s]" (String.concat " " (List.map pp_term terms))
   | Box (V, terms) ->
       sprintf "\\VBOX [%s]" (String.concat " " (List.map pp_term terms))
+  | Box (HV, terms) ->
+      sprintf "\\HVBOX [%s]" (String.concat " " (List.map pp_term terms))
+  | Box (HOV, terms) ->
+      sprintf "\\HOVBOX [%s]" (String.concat " " (List.map pp_term terms))
 
 and pp_magic = function
   | List0 (t, sep_opt) ->
diff --git a/helm/ocaml/cic_notation/cicNotationPres.ml b/helm/ocaml/cic_notation/cicNotationPres.ml
new file mode 100644 (file)
index 0000000..ebe5bf6
--- /dev/null
@@ -0,0 +1,217 @@
+(* Copyright (C) 2004-2005, HELM Team.
+ * 
+ * This file is part of HELM, an Hypertextual, Electronic
+ * Library of Mathematics, developed at the Computer Science
+ * Department, University of Bologna, Italy.
+ * 
+ * HELM is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 
+ * HELM is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HELM; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA  02111-1307, USA.
+ * 
+ * For details, see the HELM World-Wide-Web page,
+ * http://helm.cs.unibo.it/
+ *)
+
+type mathml_markup = boxml_markup Mpresentation.mpres
+and boxml_markup = mathml_markup Box.box
+
+type markup = mathml_markup
+
+let atop_attributes = [None, "linethickness", "0pt"]
+let binder_attributes = [None, "mathcolor", "blue"]
+let indent_attributes = [None, "indent", "1em"]
+
+let mpres_arrow = Mpresentation.Mo (binder_attributes, "->")
+  (* TODO unicode symbol "to" *)
+let mpres_implicit = Mpresentation.Mtext ([], "?")
+
+let map_tex use_unicode texcmd =
+  let default_map_tex = Printf.sprintf "\\%s " in
+  if use_unicode then
+    try
+      Utf8Macro.expand texcmd
+    with Utf8Macro.Macro_not_found _ -> default_map_tex texcmd
+  else
+    default_map_tex texcmd
+
+let resolve_binder use_unicode = function
+  | `Lambda -> map_tex use_unicode "lambda"
+  | `Pi -> map_tex use_unicode "Pi"
+  | `Forall -> map_tex use_unicode "forall"
+  | `Exists -> map_tex use_unicode "exists"
+
+let rec make_attributes l1 = function
+  | [] -> []
+  | None :: tl -> make_attributes (List.tl l1) tl
+  | Some s :: tl ->
+      let p,n = List.hd l1 in
+      (p,n,s) :: make_attributes (List.tl l1) tl
+
+let box_of_mpres =
+  function
+    Mpresentation.Mobject (_, box) -> box
+  | mpres -> Box.Object ([], mpres)
+
+let mpres_of_box =
+  function
+    Box.Object (_, mpres) -> mpres
+  | box -> Mpresentation.Mobject ([], box)
+
+let genuine_math =
+  function
+  | Mpresentation.Mobject _ -> false
+  | _ -> true
+
+let box_of mathonly kind attrs children =
+  if mathonly then Mpresentation.Mrow (attrs, children)
+  else
+    match kind with
+    | CicNotationPt.H when List.for_all genuine_math children ->
+        Mpresentation.Mrow (attrs, children)
+    | CicNotationPt.H ->
+        mpres_of_box (Box.H (attrs, List.map box_of_mpres children))
+    | CicNotationPt.V ->
+        mpres_of_box (Box.V (attrs, List.map box_of_mpres children))
+    | CicNotationPt.HV ->
+        mpres_of_box (Box.HV (attrs, List.map box_of_mpres children))
+    | CicNotationPt.HOV ->
+        mpres_of_box (Box.HOV (attrs, List.map box_of_mpres children))
+
+let open_paren   = Mpresentation.Mo ([], "(")
+let closed_paren = Mpresentation.Mo ([], ")")
+let open_box_paren = Box.Text ([], "(")
+let closed_box_paren = Box.Text ([], ")")
+
+type child_pos = [ `Left | `Right | `Inner ]
+
+let add_parens child_prec child_assoc child_pos curr_prec t =
+  if child_prec < curr_prec
+    || (child_prec = curr_prec &&
+        child_assoc = Gramext.LeftA &&
+        child_pos <> `Left)
+    || (child_prec = curr_prec &&
+        child_assoc = Gramext.RightA &&
+        child_pos <> `Right)
+  then  (* parens should be added *)
+    match t with
+    | Mpresentation.Mobject (_, box) ->
+        mpres_of_box (Box.H ([], [ open_box_paren; box; closed_box_paren ]))
+    | mpres -> Mpresentation.Mrow ([], [open_paren; t; closed_paren])
+  else
+    t
+
+let render ids_to_uris t =
+  let module A = CicNotationPt in
+  let module P = Mpresentation in
+  let use_unicode = true in
+  let lookup_uri = function
+    | None -> None
+    | Some id -> (try Some (Hashtbl.find ids_to_uris id) with Not_found -> None)
+  in
+  let make_href xref =
+    let uri = lookup_uri xref in
+    make_attributes [Some "helm","xref"; Some "xlink","href"] [xref;uri]
+  in
+  let make_xref xref = make_attributes [Some "helm","xref"] [xref] in
+  let make_box = function
+    | P.Mobject (attrs, box) ->
+        assert (attrs = []);
+        box
+    | m -> Box.Object ([], m)
+  in
+  let make_hv xref children =
+    let attrs = indent_attributes @ make_href xref in
+    P.Mobject ([], Box.HV (indent_attributes, List.map make_box children))
+  in
+  let rec invoke mathonly xref prec assoc t =
+    fst (aux mathonly xref prec assoc t)
+  (* when mathonly is true no boxes should be generated, only mrows *)
+  and aux mathonly xref prec assoc t =
+    let return t = t, (prec, assoc) in
+    match t with
+    | A.AttributedTerm (`Loc _, t) -> return (invoke mathonly xref prec assoc t)
+    | A.AttributedTerm (`Level (prec, assoc), t) ->
+        return (invoke mathonly xref prec assoc t)
+    | A.AttributedTerm (`IdRef xref, t) ->
+        return (invoke mathonly (Some xref) prec assoc t)
+
+    | A.Ident (literal, None) -> return (P.Mi (make_href xref, literal))
+    | A.Num (literal, _)      -> return (P.Mn (make_href xref, literal))
+    | A.Symbol (literal, _)   -> return (P.Mo (make_href xref, literal))
+    | A.Uri (literal, None)   -> return (P.Mi (make_href xref, literal))
+
+    (* default pretty printing shant' be implemented here *)
+(*     | A.Appl terms ->
+        let children = aux_children mathonly xref prec assoc terms in
+        make_hv xref children
+    | A.Binder (`Pi, (A.Ident ("_", None), ty_opt), body)
+    | A.Binder (`Forall, (A.Ident ("_", None), ty_opt), body) ->
+        let ty' =
+          match ty_opt with
+          | None -> mpres_implicit
+          | Some ty -> invoke mathonly None prec assoc ty
+        in
+        let body' = invoke mathonly None prec assoc body in
+        return (make_hv xref [ty'; make_h None [mpres_arrow; body']]) *)
+
+    | A.Literal l -> aux_literal xref prec assoc l
+    | A.Layout l -> aux_layout mathonly xref prec assoc l
+    | A.Magic _
+    | A.Variable _ -> assert false  (* should have been instantiated *)
+
+    | _ -> assert false
+
+  and aux_literal xref prec assoc l =
+    let return t = t, (prec, assoc) in
+    let attrs = make_href xref in
+    match l with
+    | `Symbol s
+    | `Keyword s -> return (P.Mo (attrs, s))
+    | `Number s  -> return (P.Mn (attrs, s))
+  and aux_layout mathonly xref prec assoc l =
+    let return t = t, (prec, assoc) in
+    let attrs = make_xref xref in
+    let invoke' t = invoke true None prec assoc t in
+    match l with
+    | A.Sub (t1, t2) -> return (P.Msub (attrs, invoke' t1, invoke' t2))
+    | A.Sup (t1, t2) -> return (P.Msup (attrs, invoke' t1, invoke' t2))
+    | A.Below (t1, t2) -> return (P.Munder (attrs, invoke' t1, invoke' t2))
+    | A.Above (t1, t2) -> return (P.Mover (attrs, invoke' t1, invoke' t2))
+    | A.Frac (t1, t2)
+    | A.Over (t1, t2) -> return (P.Mfrac (attrs, invoke' t1, invoke' t2))
+    | A.Atop (t1, t2) ->
+        return (P.Mfrac (atop_attributes @ attrs, invoke' t1, invoke' t2))
+    | A.Sqrt t -> return (P.Msqrt (attrs, invoke' t))
+    | A.Root (t1, t2) -> return (P.Mroot (attrs, invoke' t1, invoke' t2))
+    | A.Box (kind, terms) ->
+        let children = aux_children mathonly xref prec assoc terms in
+        return (box_of mathonly kind attrs children)
+  and aux_children mathonly xref prec assoc terms =
+    let rec aux_list first =
+      function
+        [] -> []
+      | [t] ->
+          let t', (child_prec, child_assoc) = aux mathonly xref prec assoc t in
+            [add_parens child_prec child_assoc `Right prec t']
+      | t :: tl ->
+          let t', (child_prec, child_assoc) = aux mathonly xref prec assoc t in
+          let child_pos = if first then `Left else `Inner in
+            add_parens child_prec child_assoc child_pos prec t' :: aux_list false tl
+    in
+      match terms with
+        [t] -> [invoke mathonly xref prec assoc t]
+      | tl -> aux_list true tl
+  in
+  fst (aux false None 0 Gramext.NonA t)
+
diff --git a/helm/ocaml/cic_notation/cicNotationPres.mli b/helm/ocaml/cic_notation/cicNotationPres.mli
new file mode 100644 (file)
index 0000000..3181ec0
--- /dev/null
@@ -0,0 +1,33 @@
+(* Copyright (C) 2004-2005, HELM Team.
+ * 
+ * This file is part of HELM, an Hypertextual, Electronic
+ * Library of Mathematics, developed at the Computer Science
+ * Department, University of Bologna, Italy.
+ * 
+ * HELM is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 
+ * HELM is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HELM; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA  02111-1307, USA.
+ * 
+ * For details, see the HELM World-Wide-Web page,
+ * http://helm.cs.unibo.it/
+ *)
+
+type mathml_markup = boxml_markup Mpresentation.mpres
+and boxml_markup = mathml_markup Box.box
+
+type markup = mathml_markup
+
+(** @param ids_to_uris mapping id -> uri for hyperlinking *)
+val render: (Cic.id, string) Hashtbl.t -> CicNotationPt.term -> markup
+
index d1a54bcf2e3f3e2e8713bc1a54df8102a721d8e1..15b9a21ad4398fe68db30fe55ecf13d7f64b09b0 100644 (file)
@@ -38,8 +38,9 @@ let loc_of_floc = function
 
 
 type term_attribute =
-  [ `Loc of location  (* source file location *)
-  | `IdRef of string  (* ACic pointer *)
+  [ `Loc of location                  (* source file location *)
+  | `IdRef of string                  (* ACic pointer *)
+  | `Level of int * Gramext.g_assoc   (* precedence, associativity *)
   ]
 
 type literal =
@@ -88,7 +89,7 @@ and meta_subst = term option
 and subst = string * term
 and case_pattern = string * capture_variable list
 
-and box_kind = H | V
+and box_kind = H | V | HV | HOV
 
 and layout_pattern =
   | Sub of term * term
@@ -102,7 +103,7 @@ and layout_pattern =
       |+ column separator, row separator +| *)
   | Sqrt of term
   | Root of term * term (* argument, index *)
-  | Break
+(*   | Break *)
   | Box of box_kind * term list
 
 and magic_term =
index e09ac345b779fc2c9daf39376667a12e4b8fc19a..e701c5049daaff92c9e70ae58607862a2c818b67 100644 (file)
@@ -161,7 +161,7 @@ let visit_layout k = function
   | Frac (t1, t2) -> Frac (k t1, k t2)
   | Sqrt t -> Sqrt (k t)
   | Root (arg, index) -> Root (k arg, k index)
-  | Break -> Break
+(*   | Break -> Break *)
   | Box (kind, terms) -> Box (kind, List.map k terms)
 
 let visit_magic k = function
index ed7b1152e055abd4414367e3c7555a5500071e34..119a45f57128f71988f1e0ac574ed08b81517625 100644 (file)
@@ -1,17 +1,8 @@
 <helm_registry>
   <section name="getter">
-    <key name="mode">remote</key>
-    <key name="url">http://mowgli.cs.unibo.it:58081/</key>
-  </section>
-<!--
-  <section name="getter">
-    <key name="prefetch">false</key>
-    <key name="servers">
-      file:///projects/helm/library/coq_contribs
+    <key name="prefix">
+      cic:/
+      file:///projects/helm/library/coq_contribs/
     </key>
-    <key name="cache_dir">/tmp/zack/cache</key>
-    <key name="maps_dir">/projects/helm/var</key>
-    <key name="dtd_dir">/projects/helm/xml/dtd</key>
   </section>
--->
 </helm_registry>
index 58880b9d5b781279600bda07825dbb2cb652742d..0ff7c558e119e3435d461e382998e727d5cbe1e0 100644 (file)
@@ -29,6 +29,22 @@ let _ =
   Helm_registry.load_from "test_parser.conf.xml"
 (*   Http_getter.init () *)
 
+let xml_stream_of_markup =
+  let rec print_box (t: CicNotationPres.boxml_markup) =
+    Box.box2xml print_mpres t
+  and print_mpres (t: CicNotationPres.mathml_markup) =
+    Mpresentation.print_mpres print_box t
+  in
+  print_mpres
+
+let dump_xml t id_to_uri fname =
+  print_endline (sprintf "dumping MathML to %s ..." fname);
+  flush stdout;
+  let oc = open_out fname in
+  let markup = CicNotationPres.render id_to_uri t in
+  Xml.pp_to_outchan (xml_stream_of_markup markup) oc;
+  close_out oc
+
 let _ =
   let module P = CicNotationPt in
   let level = ref ~-1 in
@@ -49,7 +65,9 @@ let _ =
                 prerr_endline "====";
                 print_endline (CicNotationPp.pp_term t); flush stdout;
                 let t' = CicNotationRew.pp_ast t in
-                print_endline (CicNotationPp.pp_term t'); flush stdout
+                print_endline (CicNotationPp.pp_term t'); flush stdout;
+                let tbl = Hashtbl.create 0 in
+                dump_xml t' tbl "out.xml"
             | P.Notation (l1, associativity, precedence, l2) ->
                 print_endline "Extending parser ..."; flush stdout;
                 let time1 = Unix.gettimeofday () in
@@ -98,6 +116,7 @@ let _ =
                 let t' = CicNotationRew.pp_ast t in
                 let time4 = Unix.gettimeofday () in
                 printf "pretty printing took %f seconds\n" (time4 -. time3);
+                dump_xml t id_to_uri "out.xml";
                 print_endline (CicNotationPp.pp_term t'); flush stdout
                 )
 (*                 CicNotationParser.print_l2_pattern ()) *)