src/Pure/Syntax/syn_trans.ML
changeset 42043 2055515c9d3f
parent 40235 87998864284e
child 42045 fda09013c496
equal deleted inserted replaced
42042:264f8d0e899f 42043:2055515c9d3f
    53   include SYN_TRANS1
    53   include SYN_TRANS1
    54   val abs_tr': Proof.context -> term -> term
    54   val abs_tr': Proof.context -> term -> term
    55   val prop_tr': term -> term
    55   val prop_tr': term -> term
    56   val appl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
    56   val appl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
    57   val applC_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
    57   val applC_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
    58   val pts_to_asts: Proof.context ->
    58   val parsetree_to_ast: Proof.context ->
    59     (string -> (Proof.context -> Ast.ast list -> Ast.ast) option) ->
    59     (string -> (Proof.context -> Ast.ast list -> Ast.ast) option) -> Parser.parsetree -> Ast.ast
    60     Parser.parsetree list -> Ast.ast list
    60   val ast_to_term: Proof.context ->
    61   val asts_to_terms: Proof.context ->
    61     (string -> (Proof.context -> term list -> term) option) -> Ast.ast -> term
    62     (string -> (Proof.context -> term list -> term) option) -> Ast.ast list -> term list
       
    63 end;
    62 end;
    64 
    63 
    65 structure Syn_Trans: SYN_TRANS =
    64 structure Syn_Trans: SYN_TRANS =
    66 struct
    65 struct
    67 
    66 
   532 fun struct_trfuns structs =
   531 fun struct_trfuns structs =
   533   ([], [("_struct", struct_tr structs)], [], [("_struct", struct_ast_tr' structs)]);
   532   ([], [("_struct", struct_tr structs)], [], [("_struct", struct_ast_tr' structs)]);
   534 
   533 
   535 
   534 
   536 
   535 
   537 (** pts_to_asts **)
   536 (** parsetree_to_ast **)
   538 
   537 
   539 fun pts_to_asts ctxt trf pts =
   538 fun parsetree_to_ast ctxt trf =
   540   let
   539   let
   541     fun trans a args =
   540     fun trans a args =
   542       (case trf a of
   541       (case trf a of
   543         NONE => Ast.mk_appl (Ast.Constant a) args
   542         NONE => Ast.mk_appl (Ast.Constant a) args
   544       | SOME f => f ctxt args);
   543       | SOME f => f ctxt args);
   545 
   544 
   546     (*translate pt bottom-up*)
       
   547     fun ast_of (Parser.Node (a, pts)) = trans a (map ast_of pts)
   545     fun ast_of (Parser.Node (a, pts)) = trans a (map ast_of pts)
   548       | ast_of (Parser.Tip tok) = Ast.Variable (Lexicon.str_of_token tok);
   546       | ast_of (Parser.Tip tok) = Ast.Variable (Lexicon.str_of_token tok);
   549 
   547   in ast_of end;
   550     val exn_results = map (Exn.interruptible_capture ast_of) pts;
   548 
   551     val exns = map_filter Exn.get_exn exn_results;
   549 
   552     val results = map_filter Exn.get_result exn_results
   550 
   553   in (case (results, exns) of ([], exn :: _) => reraise exn | _ => results) end;
   551 (** ast_to_term **)
   554 
   552 
   555 
   553 fun ast_to_term ctxt trf =
   556 
       
   557 (** asts_to_terms **)
       
   558 
       
   559 fun asts_to_terms ctxt trf asts =
       
   560   let
   554   let
   561     fun trans a args =
   555     fun trans a args =
   562       (case trf a of
   556       (case trf a of
   563         NONE => Term.list_comb (Lexicon.const a, args)
   557         NONE => Term.list_comb (Lexicon.const a, args)
   564       | SOME f => f ctxt args);
   558       | SOME f => f ctxt args);
   568       | term_of (Ast.Appl (Ast.Constant a :: (asts as _ :: _))) =
   562       | term_of (Ast.Appl (Ast.Constant a :: (asts as _ :: _))) =
   569           trans a (map term_of asts)
   563           trans a (map term_of asts)
   570       | term_of (Ast.Appl (ast :: (asts as _ :: _))) =
   564       | term_of (Ast.Appl (ast :: (asts as _ :: _))) =
   571           Term.list_comb (term_of ast, map term_of asts)
   565           Term.list_comb (term_of ast, map term_of asts)
   572       | term_of (ast as Ast.Appl _) = raise Ast.AST ("ast_to_term: malformed ast", [ast]);
   566       | term_of (ast as Ast.Appl _) = raise Ast.AST ("ast_to_term: malformed ast", [ast]);
   573 
   567   in term_of end;
   574     val exn_results = map (Exn.interruptible_capture term_of) asts;
       
   575     val exns = map_filter Exn.get_exn exn_results;
       
   576     val results = map_filter Exn.get_result exn_results
       
   577   in (case (results, exns) of ([], exn :: _) => reraise exn | _ => results) end;
       
   578 
   568 
   579 end;
   569 end;