]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/cic_notation/cicNotationPres.ml
implemented transformations on top of notation code
[helm.git] / helm / ocaml / cic_notation / cicNotationPres.ml
index 395cab6c29927e01cc1760ec1ac6ccc28e6fd87f..e7afad578387bd64d156b89991e3b4ed7c2d99e5 100644 (file)
@@ -29,16 +29,8 @@ 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 keyword_attributes = [None, "mathcolor", "blue"]
 
-let to_unicode s =
-  try
-    if s.[0] = '\\' then
-      Utf8Macro.expand (String.sub s 1 (String.length s - 1))
-    else s
-  with Utf8Macro.Macro_not_found _ -> s
+let to_unicode = Utf8Macro.unicode_of_tex
 
 let rec make_attributes l1 = function
   | [] -> []
@@ -57,10 +49,27 @@ let mpres_of_box =
     Box.Object (_, mpres) -> mpres
   | box -> Mpresentation.Mobject ([], box)
 
-let genuine_math =
+let rec genuine_math =
   function
-  | Mpresentation.Mobject _ -> false
+  | Mpresentation.Mobject ([], obj) -> not (genuine_box obj)
   | _ -> true
+and genuine_box =
+  function
+  | Box.Object ([], mpres) -> not (genuine_math mpres)
+  | _ -> true
+
+let rec eligible_math =
+  function
+  | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> eligible_math mpres
+  | Mpresentation.Mobject ([], _) -> false
+  | _ -> true
+
+let rec promote_to_math =
+  function
+  | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> promote_to_math mpres
+  | math -> math
+
+let small_skip = Mpresentation.Mspace [None, "width", "0.5em"]
 
 let box_of mathonly spec attrs children =
   match children with
@@ -69,7 +78,7 @@ let box_of mathonly spec attrs children =
        let kind, spacing, indent = spec in
        let dress children =
          if spacing then
-           CicNotationUtil.dress (Mpresentation.Mtext ([], " ")) children
+           CicNotationUtil.dress small_skip children
          else
            children
        in
@@ -77,25 +86,39 @@ let box_of mathonly spec attrs children =
          else
             let attrs' =
              (if spacing then [None, "spacing", "0.5em"] else [])
-               @ (if indent then [None, "indent", "0em 0.5em"] else [])
-                 @ attrs
+              @ (if indent then [None, "indent", "0.5em"] else [])
+              @ attrs
             in
               match kind with
-               | CicNotationPt.H when List.for_all genuine_math children ->
-                   Mpresentation.Mrow (attrs', dress children)
-               | CicNotationPt.H ->
-                   mpres_of_box (Box.H (attrs', List.map box_of_mpres children))
+                | CicNotationPt.H ->
+                    if List.for_all eligible_math children then
+                      Mpresentation.Mrow (attrs',
+                        dress (List.map promote_to_math children))
+                    else
+                      mpres_of_box (Box.H (attrs',
+                        List.map box_of_mpres children))
+(*                 | CicNotationPt.H when List.for_all genuine_math children ->
+                    Mpresentation.Mrow (attrs', dress children) *)
                | CicNotationPt.V ->
-                   mpres_of_box (Box.V (attrs', List.map box_of_mpres children))
+                   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))
+                   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))
+                   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 ([], ")")
+let open_paren        = Mpresentation.Mo ([], "(")
+let closed_paren      = Mpresentation.Mo ([], ")")
+let open_brace        = Mpresentation.Mo ([], "{")
+let closed_brace      = Mpresentation.Mo ([], "}")
+let hidden_substs     = Mpresentation.Mtext ([], "{...}")
+let open_box_paren    = Box.Text ([], "(")
+let closed_box_paren  = Box.Text ([], ")")
+let semicolon         = Mpresentation.Mo ([], ";")
+let toggle_action children =
+  Mpresentation.Maction ([None, "actiontype", "toggle"], children)
 
 type child_pos = [ `None | `Left | `Right | `Inner ]
 
@@ -140,8 +163,6 @@ let is_atomic t =
   aux_mpres t
 
 let add_parens child_prec child_assoc child_pos curr_prec t =
-(*  prerr_endline (Printf.sprintf "add_parens %d %s %s %d" child_prec
-    (pp_assoc child_assoc) (pp_pos child_pos) (curr_prec)); *)
   if is_atomic t then t
   else if child_prec < curr_prec
     || (child_prec = curr_prec &&
@@ -180,31 +201,67 @@ let render ids_to_uris =
     @ 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
   (* when mathonly is true no boxes should be generated, only mrows *)
   let rec aux xmlattrs mathonly xref pos prec uris t =
     match t with
     | A.AttributedTerm (attr, t) ->
         aux_attribute xmlattrs mathonly xref pos prec uris t attr
-    | A.Ident (literal, _) ->
-        P.Mi (make_href xmlattrs xref [], to_unicode literal)
     | A.Num (literal, _) ->
-        P.Mn (make_href xmlattrs xref [], to_unicode literal)
+        let attrs =
+          (RenderingAttrs.number_attributes `MathML)
+          @ make_href xmlattrs xref uris
+        in
+        P.Mn (attrs, literal)
     | A.Symbol (literal, _) ->
-        P.Mo (make_href xmlattrs xref uris, to_unicode literal)
-    | A.Uri (literal, _) ->
-        P.Mi (make_href xmlattrs xref [], to_unicode literal)
+        let attrs =
+          (RenderingAttrs.symbol_attributes `MathML)
+          @ make_href xmlattrs xref uris
+        in
+        P.Mo (attrs, to_unicode literal)
+    | A.Ident (literal, subst)
+    | A.Uri (literal, subst) ->
+        let attrs =
+          (RenderingAttrs.ident_attributes `MathML)
+          @ make_href xmlattrs xref []
+        in
+        let name = P.Mi (attrs, to_unicode literal) in
+        (match subst with
+        | Some []
+        | None -> name
+        | Some substs ->
+            let substs' =
+              box_of mathonly (A.H, false, false) []
+                (open_brace
+                :: (CicNotationUtil.dress semicolon
+                    (List.map
+                      (fun (name, t) ->
+                        box_of mathonly (A.H, false, false) [] [
+                          P.Mi ([], name);
+                          P.Mo ([], to_unicode "\\def");
+                          aux [] mathonly xref pos prec uris t ])
+                      substs))
+                @ [ closed_brace ])
+(*                 (CicNotationUtil.dress semicolon
+                  (List.map
+                    (fun (var, t) ->
+                      let var_uri = UriManager.uri_of_string var in
+                      let var_name = UriManager.name_of_uri var_uri in
+                      let href_attr = Some "xlink", "href", var in
+                      box_of mathonly (A.H, false, false) [] [
+                        P.Mi ([href_attr], var_name);
+                        P.Mo ([], to_unicode "\\def");
+                        aux [] mathonly xref pos prec uris t ])
+                    substs)) *)
+            in
+            let substs_maction = toggle_action [ hidden_substs; substs' ] in
+            box_of mathonly (A.H, false, false) [] [ name; substs_maction ])
     | A.Literal l -> aux_literal xmlattrs xref prec uris l
+    | A.UserInput -> P.Mtext ([], "%")
     | A.Layout l -> aux_layout mathonly xref pos prec uris l
     | A.Magic _
     | A.Variable _ -> assert false  (* should have been instantiated *)
     | t ->
-        prerr_endline (CicNotationPp.pp_term t);
+        prerr_endline ("unexpected ast: " ^ CicNotationPp.pp_term t);
         assert false
   and aux_attribute xmlattrs mathonly xref pos prec uris t =
     function
@@ -218,10 +275,10 @@ let render ids_to_uris =
     | `XmlAttrs xmlattrs -> aux xmlattrs mathonly xref pos prec uris t
   and aux_literal xmlattrs xref prec uris l =
     let attrs = make_href xmlattrs xref uris in
-      match l with
-       | `Symbol s -> P.Mo (attrs, to_unicode s)
-       | `Keyword s -> P.Mo (attrs, to_unicode s)
-       | `Number s  -> P.Mn (attrs, to_unicode s)
+    (match l with
+    | `Symbol s -> P.Mo (attrs, to_unicode s)
+    | `Keyword s -> P.Mo (attrs, to_unicode s)
+    | `Number s  -> P.Mn (attrs, to_unicode s))
   and aux_layout mathonly xref pos prec uris l =
     let attrs = make_xref xref in
     let invoke' t = aux [] true None pos prec uris t in
@@ -237,11 +294,17 @@ let render ids_to_uris =
     | A.Sqrt t -> P.Msqrt (attrs, invoke' t)
     | A.Root (t1, t2) -> P.Mroot (attrs, invoke' t1, invoke' t2)
     | A.Box ((_, spacing, _) as kind, terms) ->
-        let children = aux_children mathonly spacing xref pos prec uris (CicNotationUtil.ungroup terms) in
-          box_of mathonly kind attrs children
+        let children =
+          aux_children mathonly spacing xref pos prec uris
+            (CicNotationUtil.ungroup terms)
+        in
+        box_of mathonly kind attrs children
     | A.Group terms ->
-       let children = aux_children false mathonly xref pos prec uris (CicNotationUtil.ungroup terms) in
-         box_of mathonly (A.H, false, false) attrs children
+       let children =
+          aux_children mathonly false xref pos prec uris
+            (CicNotationUtil.ungroup terms)
+        in
+        box_of mathonly (A.H, false, false) attrs children
     | A.Break -> assert false (* TODO? *)
   and aux_children mathonly spacing xref pos prec uris terms =
     let find_clusters =
@@ -249,8 +312,10 @@ let render ids_to_uris =
        function
            [] when acc = [] -> List.rev clusters
          | [] -> aux_list first (List.rev acc :: clusters) [] []
-         | (A.Layout A.Break) :: tl when acc = [] -> aux_list first clusters [] tl
-         | (A.Layout A.Break) :: tl -> aux_list first (List.rev acc :: clusters) [] tl
+         | (A.Layout A.Break) :: tl when acc = [] ->
+              aux_list first clusters [] tl
+         | (A.Layout A.Break) :: tl ->
+              aux_list first (List.rev acc :: clusters) [] tl
          | [hd] ->
              let pos' = 
                if first then
@@ -262,7 +327,8 @@ let render ids_to_uris =
                    | `Right -> `Right
                    | `Left -> `Inner
              in
-               aux_list false clusters (aux [] mathonly xref pos' prec uris hd :: acc) []
+               aux_list false clusters
+                  (aux [] mathonly xref pos' prec uris hd :: acc) []
          | hd :: tl ->
              let pos' =
                match pos, first with
@@ -273,7 +339,8 @@ let render ids_to_uris =
                  | `Right, _ -> `Inner
                  | `Inner, _ -> `Inner
              in
-               aux_list false clusters (aux [] mathonly xref pos' prec uris hd :: acc) tl
+               aux_list false clusters
+                  (aux [] mathonly xref pos' prec uris hd :: acc) tl
       in
        aux_list true [] []
     in
@@ -286,12 +353,12 @@ let render ids_to_uris =
   in
   aux [] false None `None 0 []
 
+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
+
 let render_to_boxml id_to_uri t =
-  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
   let xml_stream = print_box (box_of_mpres (render id_to_uri t)) in
-  Ast2pres.add_xml_declaration xml_stream
+  Xml.add_xml_declaration xml_stream