440 end |
440 end |
441 |
441 |
442 (* define syntax for case combinator *) |
442 (* define syntax for case combinator *) |
443 (* TODO: re-implement case syntax using a parse translation *) |
443 (* TODO: re-implement case syntax using a parse translation *) |
444 local |
444 local |
445 open Syntax |
|
446 fun syntax c = Syntax.mark_const (fst (dest_Const c)) |
445 fun syntax c = Syntax.mark_const (fst (dest_Const c)) |
447 fun xconst c = Long_Name.base_name (fst (dest_Const c)) |
446 fun xconst c = Long_Name.base_name (fst (dest_Const c)) |
448 fun c_ast authentic con = |
447 fun c_ast authentic con = Ast.Constant (if authentic then syntax con else xconst con) |
449 Constant (if authentic then syntax con else xconst con) |
|
450 fun showint n = string_of_int (n+1) |
448 fun showint n = string_of_int (n+1) |
451 fun expvar n = Variable ("e" ^ showint n) |
449 fun expvar n = Ast.Variable ("e" ^ showint n) |
452 fun argvar n (m, _) = Variable ("a" ^ showint n ^ "_" ^ showint m) |
450 fun argvar n (m, _) = Ast.Variable ("a" ^ showint n ^ "_" ^ showint m) |
453 fun argvars n args = map_index (argvar n) args |
451 fun argvars n args = map_index (argvar n) args |
454 fun app s (l, r) = mk_appl (Constant s) [l, r] |
452 fun app s (l, r) = Ast.mk_appl (Ast.Constant s) [l, r] |
455 val cabs = app "_cabs" |
453 val cabs = app "_cabs" |
456 val capp = app @{const_syntax Rep_cfun} |
454 val capp = app @{const_syntax Rep_cfun} |
457 val capps = Library.foldl capp |
455 val capps = Library.foldl capp |
458 fun con1 authentic n (con,args) = |
456 fun con1 authentic n (con,args) = |
459 Library.foldl capp (c_ast authentic con, argvars n args) |
457 Library.foldl capp (c_ast authentic con, argvars n args) |
460 fun case1 authentic (n, c) = |
458 fun case1 authentic (n, c) = |
461 app "_case1" (Syntax.strip_positions_ast (con1 authentic n c), expvar n) |
459 app "_case1" (Syntax.strip_positions_ast (con1 authentic n c), expvar n) |
462 fun arg1 (n, (con,args)) = List.foldr cabs (expvar n) (argvars n args) |
460 fun arg1 (n, (con,args)) = List.foldr cabs (expvar n) (argvars n args) |
463 fun when1 n (m, c) = |
461 fun when1 n (m, c) = if n = m then arg1 (n, c) else Ast.Constant @{const_syntax bottom} |
464 if n = m then arg1 (n, c) else (Constant @{const_syntax bottom}) |
462 val case_constant = Ast.Constant (syntax (case_const dummyT)) |
465 val case_constant = Constant (syntax (case_const dummyT)) |
|
466 fun case_trans authentic = |
463 fun case_trans authentic = |
467 (if authentic then Parse_Print_Rule else Parse_Rule) |
464 (if authentic then Syntax.Parse_Print_Rule else Syntax.Parse_Rule) |
468 (app "_case_syntax" |
465 (app "_case_syntax" |
469 (Variable "x", |
466 (Ast.Variable "x", |
470 foldr1 (app "_case2") (map_index (case1 authentic) spec)), |
467 foldr1 (app "_case2") (map_index (case1 authentic) spec)), |
471 capp (capps (case_constant, map_index arg1 spec), Variable "x")) |
468 capp (capps (case_constant, map_index arg1 spec), Ast.Variable "x")) |
472 fun one_abscon_trans authentic (n, c) = |
469 fun one_abscon_trans authentic (n, c) = |
473 (if authentic then Parse_Print_Rule else Parse_Rule) |
470 (if authentic then Syntax.Parse_Print_Rule else Syntax.Parse_Rule) |
474 (cabs (con1 authentic n c, expvar n), |
471 (cabs (con1 authentic n c, expvar n), |
475 capps (case_constant, map_index (when1 n) spec)) |
472 capps (case_constant, map_index (when1 n) spec)) |
476 fun abscon_trans authentic = |
473 fun abscon_trans authentic = |
477 map_index (one_abscon_trans authentic) spec |
474 map_index (one_abscon_trans authentic) spec |
478 val trans_rules : ast Syntax.trrule list = |
475 val trans_rules : Ast.ast Syntax.trrule list = |
479 case_trans false :: case_trans true :: |
476 case_trans false :: case_trans true :: |
480 abscon_trans false @ abscon_trans true |
477 abscon_trans false @ abscon_trans true |
481 in |
478 in |
482 val thy = Sign.add_trrules trans_rules thy |
479 val thy = Sign.add_trrules trans_rules thy |
483 end |
480 end |