* http://helm.cs.unibo.it/
*)
-open CicNotationPt
-
- (* TODO ensure that names generated by fresh_var do not clash with user's *)
-let fresh_name =
- let index = ref ~-1 in
- fun () ->
- incr index;
- "fresh" ^ string_of_int !index
-
-(* let meta_names_of term = *)
-(* let rec names = ref [] in *)
-(* let add_name n = *)
-(* if List.mem n !names then () *)
-(* else names := n :: !names *)
-(* in *)
-(* let rec aux = function *)
-(* | AttributedTerm (_, term) -> aux term *)
-(* | Appl terms -> List.iter aux terms *)
-(* | Binder (_, _, body) -> aux body *)
-(* | Case (term, indty, outty_opt, patterns) -> *)
-(* aux term ; *)
-(* aux_opt outty_opt ; *)
-(* List.iter aux_branch patterns *)
-(* | LetIn (_, t1, t2) -> *)
-(* aux t1 ; *)
-(* aux t2 *)
-(* | LetRec (_, definitions, body) -> *)
-(* List.iter aux_definition definitions ; *)
-(* aux body *)
-(* | Uri (_, Some substs) -> aux_substs substs *)
-(* | Ident (_, Some substs) -> aux_substs substs *)
-(* | Meta (_, substs) -> aux_meta_substs substs *)
-
-(* | Implicit *)
-(* | Ident _ *)
-(* | Num _ *)
-(* | Sort _ *)
-(* | Symbol _ *)
-(* | Uri _ *)
-(* | UserInput -> () *)
-
-(* | Magic magic -> aux_magic magic *)
-(* | Variable var -> aux_variable var *)
-
-(* | _ -> assert false *)
-(* and aux_opt = function *)
-(* | Some term -> aux term *)
-(* | None -> () *)
-(* and aux_capture_var (_, ty_opt) = aux_opt ty_opt *)
-(* and aux_branch (pattern, term) = *)
-(* aux_pattern pattern ; *)
-(* aux term *)
-(* and aux_pattern (head, vars) = *)
-(* List.iter aux_capture_var vars *)
-(* and aux_definition (var, term, i) = *)
-(* aux_capture_var var ; *)
-(* aux term *)
-(* and aux_substs substs = List.iter (fun (_, term) -> aux term) substs *)
-(* and aux_meta_substs meta_substs = List.iter aux_opt meta_substs *)
-(* and aux_variable = function *)
-(* | NumVar name -> add_name name *)
-(* | IdentVar name -> add_name name *)
-(* | TermVar name -> add_name name *)
-(* | FreshVar _ -> () *)
-(* | Ascription _ -> assert false *)
-(* and aux_magic = function *)
-(* | Default (t1, t2) *)
-(* | Fold (_, t1, _, t2) -> *)
-(* aux t1 ; *)
-(* aux t2 *)
-(* | _ -> assert false *)
-(* in *)
-(* aux term ; *)
-(* !names *)
+module Ast = CicNotationPt
let visit_ast ?(special_k = fun _ -> assert false) k =
let rec aux = function
- | Appl terms -> Appl (List.map k terms)
- | Binder (kind, var, body) ->
- Binder (kind, aux_capture_variable var, k body)
- | Case (term, indtype, typ, patterns) ->
- Case (k term, indtype, aux_opt typ, aux_patterns patterns)
- | LetIn (var, t1, t2) -> LetIn (aux_capture_variable var, k t1, k t2)
- | LetRec (kind, definitions, term) ->
+ | Ast.Appl terms -> Ast.Appl (List.map k terms)
+ | Ast.Binder (kind, var, body) ->
+ Ast.Binder (kind, aux_capture_variable var, k body)
+ | Ast.Case (term, indtype, typ, patterns) ->
+ Ast.Case (k term, indtype, aux_opt typ, aux_patterns patterns)
+ | Ast.Cast (t1, t2) -> Ast.Cast (k t1, k t2)
+ | Ast.LetIn (var, t1, t2) ->
+ Ast.LetIn (aux_capture_variable var, k t1, k t2)
+ | Ast.LetRec (kind, definitions, term) ->
let definitions =
List.map
(fun (var, ty, n) -> aux_capture_variable var, k ty, n)
definitions
in
- LetRec (kind, definitions, k term)
- | Ident (name, Some substs) -> Ident (name, Some (aux_substs substs))
- | Uri (name, Some substs) -> Uri (name, Some (aux_substs substs))
- | Meta (index, substs) -> Meta (index, List.map aux_opt substs)
- | (AttributedTerm _
- | Layout _
- | Literal _
- | Magic _
- | Variable _) as t -> special_k t
- | (Ident _
- | Implicit
- | Num _
- | Sort _
- | Symbol _
- | Uri _
- | UserInput) as t -> t
+ Ast.LetRec (kind, definitions, k term)
+ | Ast.Ident (name, Some substs) ->
+ Ast.Ident (name, Some (aux_substs substs))
+ | Ast.Uri (name, Some substs) -> Ast.Uri (name, Some (aux_substs substs))
+ | Ast.Meta (index, substs) -> Ast.Meta (index, List.map aux_opt substs)
+ | (Ast.AttributedTerm _
+ | Ast.Layout _
+ | Ast.Literal _
+ | Ast.Magic _
+ | Ast.Variable _) as t -> special_k t
+ | (Ast.Ident _
+ | Ast.Implicit
+ | Ast.Num _
+ | Ast.Sort _
+ | Ast.Symbol _
+ | Ast.Uri _
+ | Ast.UserInput) as t -> t
and aux_opt = function
| None -> None
| Some term -> Some (k term)
and aux_capture_variable (term, typ_opt) = k term, aux_opt typ_opt
and aux_patterns patterns = List.map aux_pattern patterns
- and aux_pattern ((head, vars), term) =
- ((head, List.map aux_capture_variable vars), k term)
+ and aux_pattern ((head, hrefs, vars), term) =
+ ((head, hrefs, List.map aux_capture_variable vars), k term)
and aux_subst (name, term) = (name, k term)
and aux_substs substs = List.map aux_subst substs
in
aux
let visit_layout k = function
- | Sub (t1, t2) -> Sub (k t1, k t2)
- | Sup (t1, t2) -> Sup (k t1, k t2)
- | Below (t1, t2) -> Below (k t1, k t2)
- | Above (t1, t2) -> Above (k t1, k t2)
- | Over (t1, t2) -> Over (k t1, k t2)
- | Atop (t1, t2) -> Atop (k t1, k t2)
- | Frac (t1, t2) -> Frac (k t1, k t2)
- | Sqrt t -> Sqrt (k t)
- | Root (arg, index) -> Root (k arg, k index)
- | Break -> Break
- | Box (kind, terms) -> Box (kind, List.map k terms)
- | Group terms -> Group (List.map k terms)
+ | Ast.Sub (t1, t2) -> Ast.Sub (k t1, k t2)
+ | Ast.Sup (t1, t2) -> Ast.Sup (k t1, k t2)
+ | Ast.Below (t1, t2) -> Ast.Below (k t1, k t2)
+ | Ast.Above (t1, t2) -> Ast.Above (k t1, k t2)
+ | Ast.Over (t1, t2) -> Ast.Over (k t1, k t2)
+ | Ast.Atop (t1, t2) -> Ast.Atop (k t1, k t2)
+ | Ast.Frac (t1, t2) -> Ast.Frac (k t1, k t2)
+ | Ast.Sqrt t -> Ast.Sqrt (k t)
+ | Ast.Root (arg, index) -> Ast.Root (k arg, k index)
+ | Ast.Break -> Ast.Break
+ | Ast.Box (kind, terms) -> Ast.Box (kind, List.map k terms)
+ | Ast.Group terms -> Ast.Group (List.map k terms)
let visit_magic k = function
- | List0 (t, l) -> List0 (k t, l)
- | List1 (t, l) -> List1 (k t, l)
- | Opt t -> Opt (k t)
- | Fold (kind, t1, names, t2) -> Fold (kind, k t1, names, k t2)
- | Default (t1, t2) -> Default (k t1, k t2)
- | If (t1, t2, t3) -> If (k t1, k t2, k t3)
- | Fail -> Fail
+ | Ast.List0 (t, l) -> Ast.List0 (k t, l)
+ | Ast.List1 (t, l) -> Ast.List1 (k t, l)
+ | Ast.Opt t -> Ast.Opt (k t)
+ | Ast.Fold (kind, t1, names, t2) -> Ast.Fold (kind, k t1, names, k t2)
+ | Ast.Default (t1, t2) -> Ast.Default (k t1, k t2)
+ | Ast.If (t1, t2, t3) -> Ast.If (k t1, k t2, k t3)
+ | Ast.Fail -> Ast.Fail
+
+let visit_variable k = function
+ | Ast.NumVar _
+ | Ast.IdentVar _
+ | Ast.TermVar _
+ | Ast.FreshVar _ as t -> t
+ | Ast.Ascription (t, s) -> Ast.Ascription (k t, s)
let variables_of_term t =
let rec vars = ref [] in
else vars := v :: !vars
in
let rec aux = function
- | Magic m -> Magic (visit_magic aux m)
- | Layout l -> Layout (visit_layout aux l)
- | Variable v -> Variable (aux_variable v)
- | Literal _ as t -> t
- | AttributedTerm (_, t) -> aux t
+ | Ast.Magic m -> Ast.Magic (visit_magic aux m)
+ | Ast.Layout l -> Ast.Layout (visit_layout aux l)
+ | Ast.Variable v -> Ast.Variable (aux_variable v)
+ | Ast.Literal _ as t -> t
+ | Ast.AttributedTerm (_, t) -> aux t
| t -> visit_ast aux t
and aux_variable = function
- | (NumVar _
- | IdentVar _
- | TermVar _) as t ->
+ | (Ast.NumVar _
+ | Ast.IdentVar _
+ | Ast.TermVar _) as t ->
add_variable t ;
t
- | FreshVar _ as t -> t
- | Ascription _ -> assert false
+ | Ast.FreshVar _ as t -> t
+ | Ast.Ascription _ -> assert false
in
ignore (aux t) ;
!vars
let names_of_term t =
let aux = function
- | NumVar s
- | IdentVar s
- | TermVar s -> s
+ | Ast.NumVar s
+ | Ast.IdentVar s
+ | Ast.TermVar s -> s
| _ -> assert false
in
List.map aux (variables_of_term t)
let rec keywords = ref [] in
let add_keyword k = keywords := k :: !keywords in
let rec aux = function
- | AttributedTerm (_, t) -> aux t
- | Layout l -> Layout (visit_layout aux l)
- | Literal (`Keyword k) as t ->
+ | Ast.AttributedTerm (_, t) -> aux t
+ | Ast.Layout l -> Ast.Layout (visit_layout aux l)
+ | Ast.Literal (`Keyword k) as t ->
add_keyword k;
t
- | Literal _ as t -> t
- | Magic m -> Magic (visit_magic aux m)
- | Variable _ as v -> v
+ | Ast.Literal _ as t -> t
+ | Ast.Magic m -> Ast.Magic (visit_magic aux m)
+ | Ast.Variable _ as v -> v
| t -> visit_ast aux t
in
ignore (aux t) ;
let rec strip_attributes t =
let special_k = function
- | AttributedTerm (_, term) -> strip_attributes term
- | Magic m -> Magic (visit_magic strip_attributes m)
- | Variable _ as t -> t
+ | Ast.AttributedTerm (_, term) -> strip_attributes term
+ | Ast.Magic m -> Ast.Magic (visit_magic strip_attributes m)
+ | Ast.Variable _ as t -> t
| t -> assert false
in
visit_ast ~special_k strip_attributes t
else names := n :: !names
in
let rec aux = function
- | AttributedTerm (_, term) -> aux term
- | Appl terms -> List.iter aux terms
- | Binder (_, _, body) -> aux body
- | Case (term, indty, outty_opt, patterns) ->
+ | Ast.AttributedTerm (_, term) -> aux term
+ | Ast.Appl terms -> List.iter aux terms
+ | Ast.Binder (_, _, body) -> aux body
+ | Ast.Case (term, indty, outty_opt, patterns) ->
aux term ;
aux_opt outty_opt ;
List.iter aux_branch patterns
- | LetIn (_, t1, t2) ->
+ | Ast.LetIn (_, t1, t2) ->
aux t1 ;
aux t2
- | LetRec (_, definitions, body) ->
+ | Ast.LetRec (_, definitions, body) ->
List.iter aux_definition definitions ;
aux body
- | Uri (_, Some substs) -> aux_substs substs
- | Ident (_, Some substs) -> aux_substs substs
- | Meta (_, substs) -> aux_meta_substs substs
+ | Ast.Uri (_, Some substs) -> aux_substs substs
+ | Ast.Ident (_, Some substs) -> aux_substs substs
+ | Ast.Meta (_, substs) -> aux_meta_substs substs
- | Implicit
- | Ident _
- | Num _
- | Sort _
- | Symbol _
- | Uri _
- | UserInput -> ()
+ | Ast.Implicit
+ | Ast.Ident _
+ | Ast.Num _
+ | Ast.Sort _
+ | Ast.Symbol _
+ | Ast.Uri _
+ | Ast.UserInput -> ()
- | Magic magic -> aux_magic magic
- | Variable var -> aux_variable var
+ | Ast.Magic magic -> aux_magic magic
+ | Ast.Variable var -> aux_variable var
| _ -> assert false
and aux_opt = function
and aux_branch (pattern, term) =
aux_pattern pattern ;
aux term
- and aux_pattern (head, vars) =
+ and aux_pattern (head, _, vars) =
List.iter aux_capture_var vars
and aux_definition (var, term, i) =
aux_capture_var var ;
and aux_substs substs = List.iter (fun (_, term) -> aux term) substs
and aux_meta_substs meta_substs = List.iter aux_opt meta_substs
and aux_variable = function
- | NumVar name -> add_name name
- | IdentVar name -> add_name name
- | TermVar name -> add_name name
- | FreshVar _ -> ()
- | Ascription _ -> assert false
+ | Ast.NumVar name -> add_name name
+ | Ast.IdentVar name -> add_name name
+ | Ast.TermVar name -> add_name name
+ | Ast.FreshVar _ -> ()
+ | Ast.Ascription _ -> assert false
and aux_magic = function
- | Default (t1, t2)
- | Fold (_, t1, _, t2) ->
+ | Ast.Default (t1, t2)
+ | Ast.Fold (_, t1, _, t2) ->
aux t1 ;
aux t2
- | If (t1, t2, t3) ->
+ | Ast.If (t1, t2, t3) ->
aux t1 ;
aux t2 ;
aux t3
- | Fail -> ()
+ | Ast.Fail -> ()
| _ -> assert false
in
aux term ;
let boxify = function
| [ a ] -> a
- | l -> Layout (Box ((H, false, false), l))
+ | l -> Ast.Layout (Ast.Box ((Ast.H, false, false), l))
+
+let unboxify = function
+ | Ast.Layout (Ast.Box ((Ast.H, false, false), [ a ])) -> a
+ | l -> l
let group = function
| [ a ] -> a
- | l -> Layout (Group l)
+ | l -> Ast.Layout (Ast.Group l)
let ungroup =
let rec aux acc =
function
[] -> List.rev acc
- | Layout (Group terms) :: terms' -> aux acc (terms @ terms')
+ | Ast.Layout (Ast.Group terms) :: terms' -> aux acc (terms @ terms')
| term :: terms -> aux (term :: acc) terms
in
aux []
-let dress sauce =
+let dress ~sep:sauce =
let rec aux =
function
| [] -> []
in
aux
+let dressn ~sep:sauces =
+ let rec aux =
+ function
+ | [] -> []
+ | [hd] -> [hd]
+ | hd :: tl -> hd :: sauces @ aux tl
+ in
+ aux
+
let find_appl_pattern_uris ap =
let rec aux acc =
function
- | UriPattern uri ->
+ | Ast.UriPattern uri ->
(try
ignore (List.find (fun uri' -> UriManager.eq uri uri') acc);
acc
with Not_found -> uri :: acc)
- | VarPattern _ -> acc
- | ApplPattern apl -> List.fold_left aux acc apl
+ | Ast.ImplicitPattern
+ | Ast.VarPattern _ -> acc
+ | Ast.ApplPattern apl -> List.fold_left aux acc apl
in
aux [] ap
let rec find_branch =
function
- Magic (If (_, Magic Fail, t)) -> find_branch t
- | Magic (If (_, t, _)) -> find_branch t
+ Ast.Magic (Ast.If (_, Ast.Magic Ast.Fail, t)) -> find_branch t
+ | Ast.Magic (Ast.If (_, t, _)) -> find_branch t
| t -> t
+
+let cic_name_of_name = function
+ | Ast.Ident ("_", None) -> Cic.Anonymous
+ | Ast.Ident (name, None) -> Cic.Name name
+ | _ -> assert false
+
+let name_of_cic_name =
+(* let add_dummy_xref t = Ast.AttributedTerm (`IdRef "", t) in *)
+ (* ZACK why we used to generate dummy xrefs? *)
+ let add_dummy_xref t = t in
+ function
+ | Cic.Name s -> add_dummy_xref (Ast.Ident (s, None))
+ | Cic.Anonymous -> add_dummy_xref (Ast.Ident ("_", None))
+
+let fresh_index = ref ~-1
+
+type notation_id = int
+
+let fresh_id () =
+ incr fresh_index;
+ !fresh_index
+
+ (* TODO ensure that names generated by fresh_var do not clash with user's *)
+let fresh_name () = "fresh" ^ string_of_int (fresh_id ())
+
+let rec freshen_term ?(index = ref 0) term =
+ let freshen_term = freshen_term ~index in
+ let fresh_instance () = incr index; !index in
+ let special_k = function
+ | Ast.AttributedTerm (attr, t) -> Ast.AttributedTerm (attr, freshen_term t)
+ | Ast.Layout l -> Ast.Layout (visit_layout freshen_term l)
+ | Ast.Magic m -> Ast.Magic (visit_magic freshen_term m)
+ | Ast.Variable v -> Ast.Variable (visit_variable freshen_term v)
+ | Ast.Literal _ as t -> t
+ | _ -> assert false
+ in
+ match term with
+ | Ast.Symbol (s, instance) -> Ast.Symbol (s, fresh_instance ())
+ | Ast.Num (s, instance) -> Ast.Num (s, fresh_instance ())
+ | t -> visit_ast ~special_k freshen_term t
+
+let freshen_obj obj =
+ let index = ref 0 in
+ let freshen_term = freshen_term ~index in
+ let freshen_name_ty = List.map (fun (n, t) -> (n, freshen_term t)) in
+ match obj with
+ | GrafiteAst.Inductive (params, indtypes) ->
+ let indtypes =
+ List.map
+ (fun (n, co, ty, ctors) -> (n, co, ty, freshen_name_ty ctors))
+ indtypes
+ in
+ GrafiteAst.Inductive (freshen_name_ty params, indtypes)
+ | GrafiteAst.Theorem (flav, n, t, ty_opt) ->
+ let ty_opt =
+ match ty_opt with None -> None | Some ty -> Some (freshen_term ty)
+ in
+ GrafiteAst.Theorem (flav, n, freshen_term t, ty_opt)
+ | GrafiteAst.Record (params, n, ty, fields) ->
+ GrafiteAst.Record (freshen_name_ty params, n, freshen_term ty,
+ freshen_name_ty fields)
+
+let freshen_term = freshen_term ?index:None
+