src/HOL/Statespace/state_space.ML
changeset 55972 51b342baecda
parent 51737 718866dda2fa
child 57181 2d13bf9ea77b
equal deleted inserted replaced
55971:7644d63e8c3f 55972:51b342baecda
    75 (* Library *)
    75 (* Library *)
    76 
    76 
    77 fun fold1 f xs = fold f (tl xs) (hd xs)
    77 fun fold1 f xs = fold f (tl xs) (hd xs)
    78 fun fold1' f [] x = x
    78 fun fold1' f [] x = x
    79   | fold1' f xs _ = fold1 f xs
    79   | fold1' f xs _ = fold1 f xs
    80 
       
    81 fun sublist_idx eq xs ys =
       
    82   let
       
    83     fun sublist n xs ys =
       
    84          if is_prefix eq xs ys then SOME n
       
    85          else (case ys of [] => NONE
       
    86                | (y::ys') => sublist (n+1) xs ys')
       
    87   in sublist 0 xs ys end;
       
    88 
       
    89 fun is_sublist eq xs ys = is_some (sublist_idx eq xs ys);
       
    90 
    80 
    91 fun sorted_subset eq [] ys = true
    81 fun sorted_subset eq [] ys = true
    92   | sorted_subset eq (x::xs) [] = false
    82   | sorted_subset eq (x::xs) [] = false
    93   | sorted_subset eq (x::xs) (y::ys) = if eq (x,y) then sorted_subset eq xs ys
    83   | sorted_subset eq (x::xs) (y::ys) = if eq (x,y) then sorted_subset eq xs ys
    94                                        else sorted_subset eq (x::xs) ys;
    84                                        else sorted_subset eq (x::xs) ys;
   116 
   106 
   117 fun make_namespace_data declinfo distinctthm silent =
   107 fun make_namespace_data declinfo distinctthm silent =
   118      {declinfo=declinfo,distinctthm=distinctthm,silent=silent};
   108      {declinfo=declinfo,distinctthm=distinctthm,silent=silent};
   119 
   109 
   120 
   110 
   121 fun delete_declinfo n ctxt =
       
   122   let val {declinfo,distinctthm,silent} = NameSpaceData.get ctxt;
       
   123   in NameSpaceData.put
       
   124        (make_namespace_data (Termtab.delete_safe n declinfo) distinctthm silent) ctxt
       
   125   end;
       
   126 
       
   127 
       
   128 fun update_declinfo (n,v) ctxt =
   111 fun update_declinfo (n,v) ctxt =
   129   let val {declinfo,distinctthm,silent} = NameSpaceData.get ctxt;
   112   let val {declinfo,distinctthm,silent} = NameSpaceData.get ctxt;
   130   in NameSpaceData.put
   113   in NameSpaceData.put
   131       (make_namespace_data (Termtab.update (n,v) declinfo) distinctthm silent) ctxt
   114       (make_namespace_data (Termtab.update (n,v) declinfo) distinctthm silent) ctxt
   132   end;
   115   end;
   250 
   233 
   251 fun namespace_definition name nameT parent_expr parent_comps new_comps thy =
   234 fun namespace_definition name nameT parent_expr parent_comps new_comps thy =
   252   let
   235   let
   253     val all_comps = parent_comps @ new_comps;
   236     val all_comps = parent_comps @ new_comps;
   254     val vars = (map (fn n => (Binding.name n, NONE, NoSyn)) all_comps);
   237     val vars = (map (fn n => (Binding.name n, NONE, NoSyn)) all_comps);
   255     val full_name = Sign.full_bname thy name;
       
   256     val dist_thm_name = distinct_compsN;
   238     val dist_thm_name = distinct_compsN;
   257 
   239 
   258     val dist_thm_full_name = dist_thm_name;
   240     val dist_thm_full_name = dist_thm_name;
   259     fun comps_of_thm thm = prop_of thm
   241     fun comps_of_thm thm = prop_of thm
   260              |> (fn (_$(_$t)) => DistinctTreeProver.dest_tree t) |> map (fst o dest_Free);
   242              |> (fn (_$(_$t)) => DistinctTreeProver.dest_tree t) |> map (fst o dest_Free);
   294     |> add_locale name ([], vars) [Element.Assumes [assume]]
   276     |> add_locale name ([], vars) [Element.Assumes [assume]]
   295     |> Proof_Context.theory_of
   277     |> Proof_Context.theory_of
   296     |> interprete_parent name dist_thm_full_name parent_expr
   278     |> interprete_parent name dist_thm_full_name parent_expr
   297   end;
   279   end;
   298 
   280 
   299 fun encode_dot x = if x= #"." then #"_" else x;
   281 fun encode_dot x = if x = #"." then #"_" else x;
   300 
   282 
   301 fun encode_type (TFree (s, _)) = s
   283 fun encode_type (TFree (s, _)) = s
   302   | encode_type (TVar ((s,i),_)) = "?" ^ s ^ string_of_int i
   284   | encode_type (TVar ((s,i),_)) = "?" ^ s ^ string_of_int i
   303   | encode_type (Type (n,Ts)) =
   285   | encode_type (Type (n,Ts)) =
   304       let
   286       let
   306         val n' = String.map encode_dot n;
   288         val n' = String.map encode_dot n;
   307       in if Ts'="" then n' else Ts' ^ "_" ^ n' end;
   289       in if Ts'="" then n' else Ts' ^ "_" ^ n' end;
   308 
   290 
   309 fun project_name T = projectN ^"_"^encode_type T;
   291 fun project_name T = projectN ^"_"^encode_type T;
   310 fun inject_name T = injectN ^"_"^encode_type T;
   292 fun inject_name T = injectN ^"_"^encode_type T;
   311 
       
   312 fun project_free T pT V = Free (project_name T, V --> pT);
       
   313 fun inject_free T pT V = Free (inject_name T, pT --> V);
       
   314 
       
   315 fun get_name n = getN ^ "_" ^ n;
       
   316 fun put_name n = putN ^ "_" ^ n;
       
   317 fun get_const n T nT V = Free (get_name n, (nT --> V) --> T);
       
   318 fun put_const n T nT V = Free (put_name n, T --> (nT --> V) --> (nT --> V));
       
   319 
       
   320 fun lookup_const T nT V =
       
   321   Const (@{const_name StateFun.lookup}, (V --> T) --> nT --> (nT --> V) --> T);
       
   322 
       
   323 fun update_const T nT V =
       
   324   Const (@{const_name StateFun.update},
       
   325     (V --> T) --> (T --> V) --> nT --> (T --> T) --> (nT --> V) --> (nT --> V));
       
   326 
       
   327 fun K_const T = Const (@{const_name K_statefun}, T --> T --> T);
       
   328 
   293 
   329 
   294 
   330 fun add_declaration name decl thy =
   295 fun add_declaration name decl thy =
   331   thy
   296   thy
   332   |> Named_Target.init I name
   297   |> Named_Target.init I name
   345     val parent_comps =
   310     val parent_comps =
   346       maps (fn (Ts',n,rs) => parent_components thy (map subst Ts', n, rs)) parents;
   311       maps (fn (Ts',n,rs) => parent_components thy (map subst Ts', n, rs)) parents;
   347     val all_comps = rename renaming (parent_comps @ map (apsnd subst) components);
   312     val all_comps = rename renaming (parent_comps @ map (apsnd subst) components);
   348   in all_comps end;
   313   in all_comps end;
   349 
   314 
   350 fun take_upto i xs = List.take(xs,i) handle General.Subscript => xs;
       
   351 
       
   352 fun statespace_definition state_type args name parents parent_comps components thy =
   315 fun statespace_definition state_type args name parents parent_comps components thy =
   353   let
   316   let
   354     val full_name = Sign.full_bname thy name;
   317     val full_name = Sign.full_bname thy name;
   355     val all_comps = parent_comps @ components;
   318     val all_comps = parent_comps @ components;
   356 
   319 
   357     val components' = map (fn (n,T) => (n,(T,full_name))) components;
   320     val components' = map (fn (n,T) => (n,(T,full_name))) components;
   358     val all_comps' = map (fn (n,T) => (n,(T,full_name))) all_comps;
       
   359 
   321 
   360     fun parent_expr (prefix, (_, n, rs)) =
   322     fun parent_expr (prefix, (_, n, rs)) =
   361       (suffix namespaceN n, (prefix, Expression.Positional rs));
   323       (suffix namespaceN n, (prefix, Expression.Positional rs));
   362     val parents_expr = map parent_expr parents;
   324     val parents_expr = map parent_expr parents;
   363     fun distinct_types Ts =
   325     fun distinct_types Ts =
   450   end;
   412   end;
   451 
   413 
   452 
   414 
   453 (* prepare arguments *)
   415 (* prepare arguments *)
   454 
   416 
   455 fun read_raw_parent ctxt raw_T =
       
   456   (case Proof_Context.read_typ_abbrev ctxt raw_T of
       
   457     Type (name, Ts) => (Ts, name)
       
   458   | T => error ("Bad parent statespace specification: " ^ Syntax.string_of_typ ctxt T));
       
   459 
       
   460 fun read_typ ctxt raw_T env =
   417 fun read_typ ctxt raw_T env =
   461   let
   418   let
   462     val ctxt' = fold (Variable.declare_typ o TFree) env ctxt;
   419     val ctxt' = fold (Variable.declare_typ o TFree) env ctxt;
   463     val T = Syntax.read_typ ctxt' raw_T;
   420     val T = Syntax.read_typ ctxt' raw_T;
   464     val env' = Term.add_tfreesT T env;
   421     val env' = Term.add_tfreesT T env;
   585 
   542 
   586 
   543 
   587 (*** parse/print - translations ***)
   544 (*** parse/print - translations ***)
   588 
   545 
   589 local
   546 local
       
   547 
   590 fun map_get_comp f ctxt (Free (name,_)) =
   548 fun map_get_comp f ctxt (Free (name,_)) =
   591       (case (get_comp ctxt name) of
   549       (case (get_comp ctxt name) of
   592         SOME (T,_) => f T T dummyT
   550         SOME (T,_) => f T T dummyT
   593       | NONE => (Syntax.free "arbitrary"(*; error "context not ready"*)))
   551       | NONE => (Syntax.free "arbitrary"(*; error "context not ready"*)))
   594   | map_get_comp _ _ _ = Syntax.free "arbitrary";
   552   | map_get_comp _ _ _ = Syntax.free "arbitrary";
   595 
   553 
   596 val get_comp_projection = map_get_comp project_free;
       
   597 val get_comp_injection  = map_get_comp inject_free;
       
   598 
       
   599 fun name_of (Free (n,_)) = n;
   554 fun name_of (Free (n,_)) = n;
       
   555 
   600 in
   556 in
   601 
   557 
   602 fun gen_lookup_tr ctxt s n =
   558 fun gen_lookup_tr ctxt s n =
   603   (case get_comp (Context.Proof ctxt) n of
   559   (case get_comp (Context.Proof ctxt) n of
   604     SOME (T, _) =>
   560     SOME (T, _) =>
   622         SOME (T, _) =>
   578         SOME (T, _) =>
   623           if prj = project_name T
   579           if prj = project_name T
   624           then Syntax.const "_statespace_lookup" $ s $ n
   580           then Syntax.const "_statespace_lookup" $ s $ n
   625           else raise Match
   581           else raise Match
   626       | NONE => raise Match)
   582       | NONE => raise Match)
   627   | lookup_tr' _ ts = raise Match;
   583   | lookup_tr' _ _ = raise Match;
   628 
   584 
   629 fun gen_update_tr id ctxt n v s =
   585 fun gen_update_tr id ctxt n v s =
   630   let
   586   let
   631     fun pname T = if id then @{const_name Fun.id} else project_name T;
   587     fun pname T = if id then @{const_name Fun.id} else project_name T;
   632     fun iname T = if id then @{const_name Fun.id} else inject_name T;
   588     fun iname T = if id then @{const_name Fun.id} else inject_name T;