(* the integer is an offset to be added to each location *)
exception NoWellTypedInterpretation of
int *
- ((Token.flocation list * string * string) list *
+ ((Stdpp.location list * string * string) list *
(DisambiguateTypes.domain_item * DisambiguateTypes.codomain_item) list *
- Token.flocation option * string Lazy.t * bool) list
+ Stdpp.location option * string Lazy.t * bool) list
exception PathNotWellFormed
(** raised when an environment is not enough informative to decide *)
type 'a disambiguator_input = string * int * 'a
type domain = domain_tree list
-and domain_tree = Node of Token.flocation list * domain_item * domain
+and domain_tree = Node of Stdpp.location list * domain_item * domain
let rec string_of_domain =
function
type 'a test_result =
| Ok of 'a * Cic.metasenv
- | Ko of Token.flocation option * string Lazy.t
- | Uncertain of Token.flocation option * string Lazy.t
+ | Ko of Stdpp.location option * string Lazy.t
+ | Uncertain of Stdpp.location option * string Lazy.t
let refine_term metasenv context uri term ugraph ~localization_tbl =
(* if benchmark then incr actual_refinements; *)
in
aux 1 context
-let interpretate_term ~(context: Cic.name list) ~env ~uri ~is_path ast
+let interpretate_term ?(create_dummy_ids=false) ~(context: Cic.name list) ~env ~uri ~is_path ast
~localization_tbl
=
+ (* create_dummy_ids shouldbe used only for interpretating patterns *)
assert (uri = None);
let rec aux ~localize loc (context: Cic.name list) = function
| CicNotationPt.AttributedTerm (`Loc loc, term) ->
let cic_term = aux ~localize loc context term in
let cic_outtype = aux_option ~localize loc context None outtype in
let do_branch ((head, _, args), term) =
- let rec do_branch' context = function
- | [] -> aux ~localize loc context term
- | (name, typ) :: tl ->
- let cic_name = CicNotationUtil.cic_name_of_name name in
- let cic_body = do_branch' (cic_name :: context) tl in
- let typ =
- match typ with
- | None -> Cic.Implicit (Some `Type)
- | Some typ -> aux ~localize loc context typ
- in
- Cic.Lambda (cic_name, typ, cic_body)
- in
+ let rec do_branch' context = function
+ | [] -> aux ~localize loc context term
+ | (name, typ) :: tl ->
+ let cic_name = CicNotationUtil.cic_name_of_name name in
+ let cic_body = do_branch' (cic_name :: context) tl in
+ let typ =
+ match typ with
+ | None -> Cic.Implicit (Some `Type)
+ | Some typ -> aux ~localize loc context typ
+ in
+ Cic.Lambda (cic_name, typ, cic_body)
+ in
do_branch' context args
in
- let (indtype_uri, indtype_no) =
+ let indtype_uri, indtype_no =
+ if create_dummy_ids then
+ (UriManager.uri_of_string "cic:/fake_indty.con", 0)
+ else
match indty_ident with
| Some (indty_ident, _) ->
(match resolve env (Id indty_ident) () with
raise (Try_again (lazy "The type of the term to be matched
is still unknown"))
| _ ->
- raise (Invalid_choice (lazy "The type of the term to be matched is not (co)inductive!")))
+ raise (Invalid_choice (Some loc, lazy "The type of the term to be matched is not (co)inductive!")))
| None ->
- let fst_constructor =
- match branches with
- | ((head, _, _), _) :: _ -> head
- | [] -> raise (Invalid_choice (lazy "The type of the term to be matched is an inductive type without constructors that cannot be determined"))
+ let rec fst_constructor =
+ function
+ (Ast.Pattern (head, _, _), _) :: _ -> head
+ | (Ast.Wildcard, _) :: tl -> fst_constructor tl
+ | [] -> raise (Invalid_choice (Some loc, lazy "The type of the term to be matched cannot be determined because it is an inductive type without constructors or because all patterns use wildcards"))
in
- (match resolve env (Id fst_constructor) () with
+ (match resolve env (Id (fst_constructor branches)) () with
| Cic.MutConstruct (indtype_uri, indtype_no, _, _) ->
(indtype_uri, indtype_no)
| Cic.Implicit _ ->
raise (Try_again (lazy "The type of the term to be matched
is still unknown"))
| _ ->
- raise (Invalid_choice (lazy "The type of the term to be matched is not (co)inductive!")))
+ raise (Invalid_choice (Some loc, lazy "The type of the term to be matched is not (co)inductive!")))
+ in
+ let branches =
+ if create_dummy_ids then
+ List.map
+ (function
+ Ast.Wildcard,term -> ("wildcard",None,[]), term
+ | Ast.Pattern _,_ ->
+ raise (Invalid_choice (Some loc, lazy "Syntax error: the left hand side of a branch patterns must be \"_\""))
+ ) branches
+ else
+ match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph indtype_uri) with
+ Cic.InductiveDefinition (il,_,leftsno,_) ->
+ let _,_,_,cl =
+ try
+ List.nth il indtype_no
+ with _ -> assert false
+ in
+ let rec count_prod t =
+ match CicReduction.whd [] t with
+ Cic.Prod (_, _, t) -> 1 + (count_prod t)
+ | _ -> 0
+ in
+ let rec sort branches cl =
+ match cl with
+ [] ->
+ let rec analyze unused unrecognized useless =
+ function
+ [] ->
+ if unrecognized != [] then
+ raise (Invalid_choice
+ (Some loc,
+ lazy
+ ("Unrecognized constructors: " ^
+ String.concat " " unrecognized)))
+ else if useless > 0 then
+ raise (Invalid_choice
+ (Some loc,
+ lazy
+ ("The last " ^ string_of_int useless ^
+ "case" ^ if useless > 1 then "s are" else " is" ^
+ " unused")))
+ else
+ []
+ | (Ast.Wildcard,_)::tl when not unused ->
+ analyze true unrecognized useless tl
+ | (Ast.Pattern (head,_,_),_)::tl when not unused ->
+ analyze unused (head::unrecognized) useless tl
+ | _::tl -> analyze unused unrecognized (useless + 1) tl
+ in
+ analyze false [] 0 branches
+ | (name,ty)::cltl ->
+ let rec find_and_remove =
+ function
+ [] ->
+ raise
+ (Invalid_choice
+ (Some loc, lazy ("Missing case: " ^ name)))
+ | ((Ast.Wildcard, _) as branch :: _) as branches ->
+ branch, branches
+ | (Ast.Pattern (name',_,_),_) as branch :: tl
+ when name = name' ->
+ branch,tl
+ | branch::tl ->
+ let found,rest = find_and_remove tl in
+ found, branch::rest
+ in
+ let branch,tl = find_and_remove branches in
+ match branch with
+ Ast.Pattern (name,y,args),term ->
+ if List.length args = count_prod ty - leftsno then
+ ((name,y,args),term)::sort tl cltl
+ else
+ raise
+ (Invalid_choice
+ (Some loc,
+ lazy ("Wrong number of arguments for " ^ name)))
+ | Ast.Wildcard,term ->
+ let rec mk_lambdas =
+ function
+ 0 -> term
+ | n ->
+ CicNotationPt.Binder
+ (`Lambda, (CicNotationPt.Ident ("_", None), None),
+ mk_lambdas (n - 1))
+ in
+ (("wildcard",None,[]),
+ mk_lambdas (count_prod ty - leftsno)) :: sort tl cltl
+ in
+ sort branches cl
+ | _ -> assert false
in
Cic.MutCase (indtype_uri, indtype_no, cic_outtype, cic_term,
(List.map do_branch branches))
| CicNotationPt.LetIn ((name, typ), def, body) ->
let cic_def = aux ~localize loc context def in
let cic_name = CicNotationUtil.cic_name_of_name name in
- let cic_def =
+ let cic_typ =
match typ with
- | None -> cic_def
- | Some t -> Cic.Cast (cic_def, aux ~localize loc context t)
+ | None -> Cic.Implicit (Some `Type)
+ | Some t -> aux ~localize loc context t
in
let cic_body = aux ~localize loc (cic_name :: context) body in
- Cic.LetIn (cic_name, cic_def, cic_body)
+ Cic.LetIn (cic_name, cic_def, cic_typ, cic_body)
| CicNotationPt.LetRec (kind, defs, body) ->
let context' =
List.fold_left
let cic_body =
let unlocalized_body = aux ~localize:false loc context' body in
match unlocalized_body with
- Cic.Rel 1 -> `AvoidLetInNoAppl
- | Cic.Appl (Cic.Rel 1::l) ->
+ Cic.Rel n when n <= List.length defs -> `AvoidLetInNoAppl n
+ | Cic.Appl (Cic.Rel n::l) when n <= List.length defs ->
(try
let l' =
List.map
(function t ->
let t',subst,metasenv =
- CicMetaSubst.delift_rels [] [] 1 t
+ CicMetaSubst.delift_rels [] [] (List.length defs) t
in
assert (subst=[]);
assert (metasenv=[]);
if localize then
match body with
CicNotationPt.AttributedTerm (_,CicNotationPt.Appl(_::l)) ->
- let l' = List.map (aux ~localize loc context) l in
- `AvoidLetIn l'
+ let l' = List.map (aux ~localize loc context') l in
+ `AvoidLetIn (n,l')
| _ -> assert false
else
- `AvoidLetIn l'
+ `AvoidLetIn (n,l')
with
CicMetaSubst.DeliftingARelWouldCaptureAFreeVariable ->
if localize then
(name, decr_idx, cic_type, cic_body))
defs
in
- let counter = ref ~-1 in
- let build_term funs =
- (* this is the body of the fold_right function below. Rationale: Fix
- * and CoFix cases differs only in an additional index in the
- * inductiveFun list, see Cic.term *)
- match kind with
- | `Inductive ->
- (fun (var, _, _, _) cic ->
- incr counter;
- let fix = Cic.Fix (!counter,funs) in
- match cic with
- `Recipe (`AddLetIn cic) ->
- `Term (Cic.LetIn (Cic.Name var, fix, cic))
- | `Recipe (`AvoidLetIn l) -> `Term (Cic.Appl (fix::l))
- | `Recipe `AvoidLetInNoAppl -> `Term fix
- | `Term t -> `Term (Cic.LetIn (Cic.Name var, fix, t)))
+ let fix_or_cofix n =
+ match kind with
+ `Inductive -> Cic.Fix (n,inductiveFuns)
| `CoInductive ->
- let funs =
- List.map (fun (name, _, typ, body) -> (name, typ, body)) funs
+ let coinductiveFuns =
+ List.map
+ (fun (name, _, typ, body) -> name, typ, body)
+ inductiveFuns
in
- (fun (var, _, _, _) cic ->
- incr counter;
- let cofix = Cic.CoFix (!counter,funs) in
- match cic with
- `Recipe (`AddLetIn cic) ->
- `Term (Cic.LetIn (Cic.Name var, cofix, cic))
- | `Recipe (`AvoidLetIn l) -> `Term (Cic.Appl (cofix::l))
- | `Recipe `AvoidLetInNoAppl -> `Term cofix
- | `Term t -> `Term (Cic.LetIn (Cic.Name var, cofix, t)))
+ Cic.CoFix (n,coinductiveFuns)
in
- (match
- List.fold_right (build_term inductiveFuns) inductiveFuns
- (`Recipe cic_body)
- with
- `Recipe _ -> assert false
- | `Term t -> t)
+ let counter = ref ~-1 in
+ let build_term funs (var,_,ty,_) t =
+ incr counter;
+ Cic.LetIn (Cic.Name var, fix_or_cofix !counter, ty, t)
+ in
+ (match cic_body with
+ `AvoidLetInNoAppl n ->
+ let n' = List.length inductiveFuns - n in
+ fix_or_cofix n'
+ | `AvoidLetIn (n,l) ->
+ let n' = List.length inductiveFuns - n in
+ Cic.Appl (fix_or_cofix n'::l)
+ | `AddLetIn cic_body ->
+ List.fold_right (build_term inductiveFuns) inductiveFuns
+ cic_body)
| CicNotationPt.Ident _
| CicNotationPt.Uri _ when is_path -> raise PathNotWellFormed
| CicNotationPt.Ident (name, subst)
(try
List.assoc s ids_to_uris, aux ~localize loc context term
with Not_found ->
- raise (Invalid_choice (lazy "The provided explicit named substitution is trying to instantiate a named variable the object is not abstracted on"))))
+ raise (Invalid_choice (Some loc, lazy "The provided explicit named substitution is trying to instantiate a named variable the object is not abstracted on"))))
subst
| None -> List.map (fun uri -> uri, Cic.Implicit None) uris)
in
(try
match cic with
| Cic.Const (uri, []) ->
- let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
+ let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph uri in
let uris = CicUtil.params_of_obj o in
Cic.Const (uri, mk_subst uris)
| Cic.Var (uri, []) ->
- let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
+ let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph uri in
let uris = CicUtil.params_of_obj o in
Cic.Var (uri, mk_subst uris)
| Cic.MutInd (uri, i, []) ->
(try
- let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
+ let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph uri in
let uris = CicUtil.params_of_obj o in
Cic.MutInd (uri, i, mk_subst uris)
with
(*here the explicit_named_substituion is assumed to be of length 0 *)
Cic.MutInd (uri,i,[]))
| Cic.MutConstruct (uri, i, j, []) ->
- let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
+ let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph uri in
let uris = CicUtil.params_of_obj o in
Cic.MutConstruct (uri, i, j, mk_subst uris)
| Cic.Meta _ | Cic.Implicit _ as t ->
*)
t
| _ ->
- raise (Invalid_choice (lazy "??? Can this happen?"))
+ raise (Invalid_choice (Some loc, lazy "??? Can this happen?"))
with
CicEnvironment.CircularDependency _ ->
- raise (Invalid_choice (lazy "Circular dependency in the environment"))))
+ raise (Invalid_choice (None, lazy "Circular dependency in the environment"))))
| CicNotationPt.Implicit -> Cic.Implicit None
| CicNotationPt.UserInput -> Cic.Implicit (Some `Hole)
| CicNotationPt.Num (num, i) -> resolve env (Num i) ~num ()
let localization_tbl = Cic.CicHash.create 23 in
(* here we are throwing away useful localization informations!!! *)
fst (
- interpretate_term ~context ~env:Environment.empty ~uri:None ~is_path:true
+ interpretate_term ~create_dummy_ids:true
+ ~context ~env:Environment.empty ~uri:None ~is_path:true
path ~localization_tbl, localization_tbl)
let interpretate_obj ~context ~env ~uri ~is_path obj ~localization_tbl =
| Ast.Case (term, indty_ident, outtype, branches) ->
let term_dom = domain_of_term ~loc ~context term in
let outtype_dom = domain_of_term_option ~loc ~context outtype in
- let get_first_constructor = function
+ let rec get_first_constructor = function
| [] -> []
- | ((head, _, _), _) :: _ -> [ Node ([loc], Id head, []) ] in
- let do_branch ((head, _, args), term) =
- let (term_context, args_domain) =
- List.fold_left
- (fun (cont, dom) (name, typ) ->
- (CicNotationUtil.cic_name_of_name name :: cont,
- (match typ with
- | None -> dom
- | Some typ -> dom @ domain_of_term ~loc ~context:cont typ)))
- (context, []) args
- in
- domain_of_term ~loc ~context:term_context term @ args_domain
+ | (Ast.Pattern (head, _, _), _) :: _ -> [ Node ([loc], Id head, []) ]
+ | _ :: tl -> get_first_constructor tl in
+ let do_branch =
+ function
+ Ast.Pattern (head, _, args), term ->
+ let (term_context, args_domain) =
+ List.fold_left
+ (fun (cont, dom) (name, typ) ->
+ (CicNotationUtil.cic_name_of_name name :: cont,
+ (match typ with
+ | None -> dom
+ | Some typ -> dom @ domain_of_term ~loc ~context:cont typ)))
+ (context, []) args
+ in
+ domain_of_term ~loc ~context:term_context term @ args_domain
+ | Ast.Wildcard, term ->
+ domain_of_term ~loc ~context term
in
let branches_dom =
List.fold_left (fun dom branch -> dom @ do_branch branch) [] branches in
CicNotationUtil.cic_name_of_name var :: context,
domain_of_term_option ~loc ~context ty @ res)
(add_defs context,[]) params))
- @ domain_of_term_option ~loc ~context typ
+ @ dom
+ @ domain_of_term_option ~loc ~context:context' typ
@ domain_of_term ~loc ~context:context' body
) [] defs
in
sig
val disambiguate_term :
?fresh_instances:bool ->
- dbd:HMysql.dbd ->
+ dbd:HSql.dbd ->
context:Cic.context ->
metasenv:Cic.metasenv ->
?initial_ugraph:CicUniv.universe_graph ->
val disambiguate_obj :
?fresh_instances:bool ->
- dbd:HMysql.dbd ->
+ dbd:HSql.dbd ->
aliases:DisambiguateTypes.environment ->(* previous interpretation status *)
universe:DisambiguateTypes.multiple_environment option ->
uri:UriManager.uri option -> (* required only for inductive types *)
- CicNotationPt.obj disambiguator_input ->
+ CicNotationPt.term CicNotationPt.obj disambiguator_input ->
((DisambiguateTypes.domain_item * DisambiguateTypes.codomain_item) list *
Cic.metasenv * (* new metasenv *)
Cic.obj *
let refine_profiler = HExtlib.profile "disambiguate_thing.refine_thing"
let disambiguate_thing ~dbd ~context ~metasenv
- ?(initial_ugraph = CicUniv.empty_ugraph) ~aliases ~universe
+ ?(initial_ugraph = CicUniv.oblivion_ugraph) ~aliases ~universe
~uri ~pp_thing ~domain_of_thing ~interpretate_thing ~refine_thing
(thing_txt,thing_txt_prefix_len,thing)
=
in refine_profiler.HExtlib.profile foo ()
with
| Try_again msg -> Uncertain (None,msg), ugraph
- | Invalid_choice msg -> Ko (None,msg), ugraph
+ | Invalid_choice (loc,msg) -> Ko (loc,msg), ugraph
in
(* (4) build all possible interpretations *)
let (@@) (l1,l2,l3) (l1',l2',l3') = l1@l1', l2@l2', l3@l3' in
failwith "Disambiguate: circular dependency"
let disambiguate_term ?(fresh_instances=false) ~dbd ~context ~metasenv
- ?(initial_ugraph = CicUniv.empty_ugraph) ~aliases ~universe
+ ?(initial_ugraph = CicUniv.oblivion_ugraph) ~aliases ~universe
(text,prefix_len,term)
=
let term =
disambiguate_thing ~dbd ~context ~metasenv ~initial_ugraph ~aliases
~universe ~uri:None ~pp_thing:CicNotationPp.pp_term
~domain_of_thing:domain_of_term
- ~interpretate_thing:interpretate_term
+ ~interpretate_thing:(interpretate_term (?create_dummy_ids:None))
~refine_thing:refine_term (text,prefix_len,term)
let disambiguate_obj ?(fresh_instances=false) ~dbd ~aliases ~universe ~uri
if fresh_instances then CicNotationUtil.freshen_obj obj else obj
in
disambiguate_thing ~dbd ~context:[] ~metasenv:[] ~aliases ~universe ~uri
- ~pp_thing:CicNotationPp.pp_obj ~domain_of_thing:domain_of_obj
+ ~pp_thing:(CicNotationPp.pp_obj CicNotationPp.pp_term) ~domain_of_thing:domain_of_obj
~interpretate_thing:interpretate_obj ~refine_thing:refine_obj
(text,prefix_len,obj)
end