src/Pure/Syntax/syntax_phases.ML
changeset 52177 15fcad6eb956
parent 52163 72e83c82c1d4
child 52185 1b481b490454
equal deleted inserted replaced
52176:d3ee6315ca22 52177:15fcad6eb956
   513 fun typ_to_ast ctxt trf T = ast_of_termT ctxt trf (term_of_typ ctxt T);
   513 fun typ_to_ast ctxt trf T = ast_of_termT ctxt trf (term_of_typ ctxt T);
   514 
   514 
   515 
   515 
   516 (* term_to_ast *)
   516 (* term_to_ast *)
   517 
   517 
       
   518 local
       
   519 
       
   520 fun mark_aprop tm =
       
   521   let
       
   522     fun aprop t = Syntax.const "_aprop" $ t;
       
   523 
       
   524     fun is_prop Ts t =
       
   525       fastype_of1 (Ts, t) = propT handle TERM _ => false;
       
   526 
       
   527     fun is_term (Const ("Pure.term", _) $ _) = true
       
   528       | is_term _ = false;
       
   529 
       
   530     fun mark _ (t as Const _) = t
       
   531       | mark Ts (t as Const ("_bound", _) $ u) = if is_prop Ts u then aprop t else t
       
   532       | mark _ (t as Free (x, T)) = if T = propT then aprop (Syntax.free x) else t
       
   533       | mark _ (t as Var (xi, T)) = if T = propT then aprop (Syntax.var xi) else t
       
   534       | mark Ts (t as Bound _) = if is_prop Ts t then aprop t else t
       
   535       | mark Ts (Abs (x, T, t)) = Abs (x, T, mark (T :: Ts) t)
       
   536       | mark Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) =
       
   537           if is_prop Ts t andalso not (is_term t) then Const ("_type_prop", T) $ mark Ts t1
       
   538           else mark Ts t1 $ mark Ts t2
       
   539       | mark Ts (t as t1 $ t2) =
       
   540           (if is_Const (Term.head_of t) orelse not (is_prop Ts t) then I else aprop)
       
   541             (mark Ts t1 $ mark Ts t2);
       
   542   in mark [] tm end;
       
   543 
       
   544 in
       
   545 
   518 fun term_to_ast idents is_syntax_const ctxt trf tm =
   546 fun term_to_ast idents is_syntax_const ctxt trf tm =
   519   let
   547   let
   520     val show_types =
   548     val show_types =
   521       Config.get ctxt show_types orelse Config.get ctxt show_sorts orelse
   549       Config.get ctxt show_types orelse Config.get ctxt show_sorts orelse
   522       Config.get ctxt show_all_types;
   550       Config.get ctxt show_all_types;
   524     val show_free_types = Config.get ctxt show_free_types;
   552     val show_free_types = Config.get ctxt show_free_types;
   525     val show_all_types = Config.get ctxt show_all_types;
   553     val show_all_types = Config.get ctxt show_all_types;
   526     val show_markup = Config.get ctxt show_markup;
   554     val show_markup = Config.get ctxt show_markup;
   527 
   555 
   528     val {structs, fixes} = idents;
   556     val {structs, fixes} = idents;
       
   557 
       
   558     fun prune_typs (t_seen as (Const _, _)) = t_seen
       
   559       | prune_typs (t as Free (x, ty), seen) =
       
   560           if ty = dummyT then (t, seen)
       
   561           else if not show_free_types orelse member (op aconv) seen t then (Syntax.free x, seen)
       
   562           else (t, t :: seen)
       
   563       | prune_typs (t as Var (xi, ty), seen) =
       
   564           if ty = dummyT then (t, seen)
       
   565           else if not show_free_types orelse member (op aconv) seen t then (Syntax.var xi, seen)
       
   566           else (t, t :: seen)
       
   567       | prune_typs (t_seen as (Bound _, _)) = t_seen
       
   568       | prune_typs (Abs (x, ty, t), seen) =
       
   569           let val (t', seen') = prune_typs (t, seen);
       
   570           in (Abs (x, ty, t'), seen') end
       
   571       | prune_typs (t1 $ t2, seen) =
       
   572           let
       
   573             val (t1', seen') = prune_typs (t1, seen);
       
   574             val (t2', seen'') = prune_typs (t2, seen');
       
   575           in (t1' $ t2', seen'') end;
   529 
   576 
   530     fun mark_atoms ((t as Const (c, _)) $ u) =
   577     fun mark_atoms ((t as Const (c, _)) $ u) =
   531           if member (op =) Pure_Thy.token_markers c
   578           if member (op =) Pure_Thy.token_markers c
   532           then t $ u else mark_atoms t $ mark_atoms u
   579           then t $ u else mark_atoms t $ mark_atoms u
   533       | mark_atoms (t $ u) = mark_atoms t $ mark_atoms u
   580       | mark_atoms (t $ u) = mark_atoms t $ mark_atoms u
   546       | mark_atoms (t as Var (xi, T)) =
   593       | mark_atoms (t as Var (xi, T)) =
   547           if xi = Syntax_Ext.dddot_indexname then Const ("_DDDOT", T)
   594           if xi = Syntax_Ext.dddot_indexname then Const ("_DDDOT", T)
   548           else Syntax.const "_var" $ t
   595           else Syntax.const "_var" $ t
   549       | mark_atoms a = a;
   596       | mark_atoms a = a;
   550 
   597 
   551     fun prune_typs (t_seen as (Const _, _)) = t_seen
       
   552       | prune_typs (t as Free (x, ty), seen) =
       
   553           if ty = dummyT then (t, seen)
       
   554           else if not show_free_types orelse member (op aconv) seen t then (Syntax.free x, seen)
       
   555           else (t, t :: seen)
       
   556       | prune_typs (t as Var (xi, ty), seen) =
       
   557           if ty = dummyT then (t, seen)
       
   558           else if not show_free_types orelse member (op aconv) seen t then (Syntax.var xi, seen)
       
   559           else (t, t :: seen)
       
   560       | prune_typs (t_seen as (Bound _, _)) = t_seen
       
   561       | prune_typs (Abs (x, ty, t), seen) =
       
   562           let val (t', seen') = prune_typs (t, seen);
       
   563           in (Abs (x, ty, t'), seen') end
       
   564       | prune_typs (t1 $ t2, seen) =
       
   565           let
       
   566             val (t1', seen') = prune_typs (t1, seen);
       
   567             val (t2', seen'') = prune_typs (t2, seen');
       
   568           in (t1' $ t2', seen'') end;
       
   569 
       
   570     fun ast_of tm =
   598     fun ast_of tm =
   571       (case strip_comb tm of
   599       (case strip_comb tm of
   572         (t as Abs _, ts) => Ast.mk_appl (ast_of (Syntax_Trans.abs_tr' ctxt t)) (map ast_of ts)
   600         (t as Abs _, ts) => Ast.mk_appl (ast_of (Syntax_Trans.abs_tr' ctxt t)) (map ast_of ts)
   573       | ((c as Const ("_free", _)), Free (x, T) :: ts) =>
   601       | ((c as Const ("_free", _)), Free (x, T) :: ts) =>
   574           Ast.mk_appl (constrain (c $ Syntax.free x) T) (map ast_of ts)
   602           Ast.mk_appl (constrain (c $ Syntax.free x) T) (map ast_of ts)
   596         Ast.Appl [Ast.Constant "_constrain", simple_ast_of ctxt t,
   624         Ast.Appl [Ast.Constant "_constrain", simple_ast_of ctxt t,
   597           ast_of_termT ctxt trf (term_of_typ ctxt T)]
   625           ast_of_termT ctxt trf (term_of_typ ctxt T)]
   598       else simple_ast_of ctxt t;
   626       else simple_ast_of ctxt t;
   599   in
   627   in
   600     tm
   628     tm
   601     |> Syntax_Trans.prop_tr'
   629     |> mark_aprop
   602     |> show_types ? (#1 o prune_typs o rpair [])
   630     |> show_types ? (#1 o prune_typs o rpair [])
   603     |> mark_atoms
   631     |> mark_atoms
   604     |> ast_of
   632     |> ast_of
   605   end;
   633   end;
       
   634 
       
   635 end;
   606 
   636 
   607 
   637 
   608 
   638 
   609 (** unparse **)
   639 (** unparse **)
   610 
   640