From: Stefano Zacchiroli Date: Fri, 8 Jul 2005 14:00:05 +0000 (+0000) Subject: snapshot X-Git-Tag: pre_notation~70 X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=commitdiff_plain;h=d25c0d1fd0dee1036df6f3bbc62c75c55c3bd314;p=helm.git snapshot - fixed priority and associativity - code cleanup --- diff --git a/helm/ocaml/cic_notation/cicNotationPres.ml b/helm/ocaml/cic_notation/cicNotationPres.ml index b898f6d1f..97659b763 100644 --- a/helm/ocaml/cic_notation/cicNotationPres.ml +++ b/helm/ocaml/cic_notation/cicNotationPres.ml @@ -99,7 +99,7 @@ let closed_paren = Mpresentation.Mo ([], ")") let open_box_paren = Box.Text ([], "(") let closed_box_paren = Box.Text ([], ")") -type child_pos = [ `Left | `Right | `Inner ] +type child_pos = [ `None | `Left | `Right | `Inner ] let pp_assoc = function @@ -109,9 +109,10 @@ let pp_assoc = let pp_pos = function - | `Left -> "`Left" - | `Right -> "`Right" - | `Inner -> "`Inner" + `None -> "`None" + | `Left -> "`Left" + | `Right -> "`Right" + | `Inner -> "`Inner" let is_atomic t = let module P = Mpresentation in @@ -159,7 +160,7 @@ let add_parens child_prec child_assoc child_pos curr_prec t = else t -let render ids_to_uris t = +let render ids_to_uris = let module A = CicNotationPt in let module P = Mpresentation in let use_unicode = true in @@ -182,91 +183,79 @@ let render ids_to_uris t = 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 + let rec aux mathonly xref pos prec t = 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, _) -> return (P.Mi (make_href xref, to_unicode literal)) - | A.Num (literal, _) -> return (P.Mn (make_href xref, to_unicode literal)) - | A.Symbol (literal, _) -> return (P.Mo (make_href xref,to_unicode literal)) - | A.Uri (literal, _) -> return (P.Mi (make_href xref, to_unicode 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.AttributedTerm (`Loc _, t) -> aux mathonly xref pos prec t + | A.AttributedTerm (`Level (child_prec, child_assoc), t) -> + let t' = aux mathonly xref pos child_prec t in + add_parens child_prec child_assoc pos prec t' + | A.AttributedTerm (`IdRef xref, t) -> aux mathonly (Some xref) pos prec t + | A.Ident (literal, _) -> P.Mi (make_href xref, to_unicode literal) + | A.Num (literal, _) -> P.Mn (make_href xref, to_unicode literal) + | A.Symbol (literal, _) -> P.Mo (make_href xref,to_unicode literal) + | A.Uri (literal, _) -> P.Mi (make_href xref, to_unicode literal) + | A.Literal l -> aux_literal xref prec l + | A.Layout l -> aux_layout mathonly xref pos prec l | A.Magic _ | A.Variable _ -> assert false (* should have been instantiated *) - | t -> prerr_endline (CicNotationPp.pp_term t); assert false - - and aux_literal xref prec assoc l = - let return t = t, (prec, assoc) in + and aux_literal xref prec l = let attrs = make_href xref in - match l with - | `Symbol s - | `Keyword s -> return (P.Mo (attrs, to_unicode s)) - | `Number s -> return (P.Mn (attrs, to_unicode s)) - and aux_layout mathonly xref prec assoc l = - let return t = t, (prec, assoc) in + match l with + | `Symbol s + | `Keyword s -> P.Mo (attrs, to_unicode s) + | `Number s -> P.Mn (attrs, to_unicode s) + and aux_layout mathonly xref pos prec l = let attrs = make_xref xref in - let invoke' t = invoke true None prec assoc t in + let invoke' t = aux true None pos prec 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.Sub (t1, t2) -> P.Msub (attrs, invoke' t1, invoke' t2) + | A.Sup (t1, t2) -> P.Msup (attrs, invoke' t1, invoke' t2) + | A.Below (t1, t2) -> P.Munder (attrs, invoke' t1, invoke' t2) + | A.Above (t1, t2) -> P.Mover (attrs, invoke' t1, invoke' t2) | A.Frac (t1, t2) - | A.Over (t1, t2) -> return (P.Mfrac (attrs, invoke' t1, invoke' t2)) + | A.Over (t1, t2) -> 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)) + P.Mfrac (atop_attributes @ attrs, invoke' t1, invoke' t2) + | A.Sqrt t -> P.Msqrt (attrs, invoke' t) + | A.Root (t1, t2) -> 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 children = aux_children mathonly xref pos prec terms in + box_of mathonly kind attrs children + and aux_children mathonly xref pos prec terms = let rec aux_list first = function [] -> [] | [t] -> - let t', (child_prec, child_assoc) = aux mathonly xref prec assoc t in - prerr_endline ("T " ^ CicNotationPp.pp_term t); - [add_parens child_prec child_assoc `Right prec t'] + assert (not first); + let pos' = + match pos with + `None -> `Right + | `Inner -> `Inner + | `Right -> `Right + | `Left -> `Inner + in + [aux mathonly xref pos' prec t] | t :: tl -> - let t', (child_prec, child_assoc) = aux mathonly xref prec assoc t in - prerr_endline ( "T " ^ CicNotationPp.pp_term t); - let child_pos = if first then `Left else `Inner in - let hd = add_parens child_prec child_assoc child_pos prec t' in - hd :: aux_list false tl + let pos' = + match pos, first with + `None, true -> `Left + | `None, false -> `Inner + | `Left, true -> `Left + | `Left, false -> `Inner + | `Right, _ -> `Inner + | `Inner, _ -> `Inner + in + (aux mathonly xref pos' prec t) :: aux_list false tl in match terms with - [t] -> [invoke mathonly xref prec assoc t] + [t] -> [aux mathonly xref pos prec t] | tl -> aux_list true tl in - fst (aux false None 0 Gramext.NonA t) + aux false None `None 0 let render_to_boxml id_to_uri t = let rec print_box (t: CicNotationPres.boxml_markup) = diff --git a/helm/ocaml/cic_notation/cicNotationRew.ml b/helm/ocaml/cic_notation/cicNotationRew.ml index 2f95a5635..3af28fddd 100644 --- a/helm/ocaml/cic_notation/cicNotationRew.ml +++ b/helm/ocaml/cic_notation/cicNotationRew.ml @@ -29,6 +29,9 @@ type pattern_id = int type interpretation_id = pattern_id type pretty_printer_id = pattern_id +let default_prec = 50 +let default_assoc = Gramext.NonA + type term_info = { sort: (Cic.id, CicNotationPt.sort_kind) Hashtbl.t; uri: (Cic.id, string) Hashtbl.t; @@ -252,7 +255,7 @@ let get_compiled32 () = let set_compiled21 f = compiled21 := Some f let set_compiled32 f = compiled32 := Some f -let instantiate21 env precedence associativity l1 = +let instantiate21 env (* precedence associativity *) l1 = let rec subst_singleton env t = CicNotationUtil.boxify (subst env t) and subst env = function @@ -345,7 +348,8 @@ let rec pp_ast1 term = Hashtbl.find level1_patterns21 pid with Not_found -> assert false in - instantiate21 (ast_env_of_env env) precedence associativity l1 + Ast.AttributedTerm (`Level (precedence, associativity), + (instantiate21 (ast_env_of_env env) (* precedence associativity *) l1)) let instantiate32 term_info env symbol args = let rec instantiate_arg = function @@ -408,7 +412,9 @@ let add_interpretation (symbol, args) appl_pattern = load_patterns32 !pattern32_matrix; id -let add_pretty_printer ?precedence ?associativity l2 l1 = +let add_pretty_printer + ?(precedence = default_prec) ?(associativity = default_assoc) l2 l1 += let id = fresh_id () in let l2' = CicNotationUtil.strip_attributes l2 in Hashtbl.add level1_patterns21 id (precedence, associativity, l1); diff --git a/helm/ocaml/cic_notation/doc/samples.ma b/helm/ocaml/cic_notation/doc/samples.ma index f0a75667c..abd8b7866 100644 --- a/helm/ocaml/cic_notation/doc/samples.ma +++ b/helm/ocaml/cic_notation/doc/samples.ma @@ -35,8 +35,8 @@ print 1 + 2. interpretation 'plus x y = (cic:/Coq/Init/Peano/plus.con x y). render cic:/Coq/Arith/Plus/plus_comm.con. -notation \[ \TERM a + \TERM b \] for 'plus \TERM a \TERM b. -notation \[ \TERM a * \TERM b \] for 'mult \TERM a \TERM b. +notation \[ \TERM a + \TERM b \] left associative with precedence 50 for 'plus \TERM a \TERM b. +notation \[ \TERM a * \TERM b \] left associative with precedence 60 for 'mult \TERM a \TERM b. interpretation 'plus x y = (cic:/Coq/Init/Peano/plus.con x y). interpretation 'mult x y = (cic:/Coq/Init/Peano/mult.con x y). render cic:/Coq/Arith/Mult/mult_plus_distr_r.con.