Attic/Quot/Examples/FSet3.thy
branchNominal2-Isabelle2013
changeset 3208 da575186d492
parent 3206 fb201e383f1b
child 3209 2fb0bc0dcbf1
equal deleted inserted replaced
3206:fb201e383f1b 3208:da575186d492
     1 theory FSet3
       
     2 imports "../../../Nominal/FSet"
       
     3 begin
       
     4 
       
     5 (* TBD *)
       
     6 
       
     7 text {* syntax for fset comprehensions (adapted from lists) *}
       
     8 
       
     9 nonterminals fsc_qual fsc_quals
       
    10 
       
    11 syntax
       
    12 "_fsetcompr" :: "'a \<Rightarrow> fsc_qual \<Rightarrow> fsc_quals \<Rightarrow> 'a fset"  ("{|_ . __")
       
    13 "_fsc_gen" :: "'a \<Rightarrow> 'a fset \<Rightarrow> fsc_qual" ("_ <- _")
       
    14 "_fsc_test" :: "bool \<Rightarrow> fsc_qual" ("_")
       
    15 "_fsc_end" :: "fsc_quals" ("|}")
       
    16 "_fsc_quals" :: "fsc_qual \<Rightarrow> fsc_quals \<Rightarrow> fsc_quals" (", __")
       
    17 "_fsc_abs" :: "'a => 'b fset => 'b fset"
       
    18 
       
    19 syntax (xsymbols)
       
    20 "_fsc_gen" :: "'a \<Rightarrow> 'a fset \<Rightarrow> fsc_qual" ("_ \<leftarrow> _")
       
    21 syntax (HTML output)
       
    22 "_fsc_gen" :: "'a \<Rightarrow> 'a fset \<Rightarrow> fsc_qual" ("_ \<leftarrow> _")
       
    23 
       
    24 parse_translation (advanced) {*
       
    25 let
       
    26   val femptyC = Syntax.const @{const_name fempty};
       
    27   val finsertC = Syntax.const @{const_name finsert};
       
    28   val fmapC = Syntax.const @{const_name fmap};
       
    29   val fconcatC = Syntax.const @{const_name fconcat};
       
    30   val IfC = Syntax.const @{const_name If};
       
    31   fun fsingl x = finsertC $ x $ femptyC;
       
    32 
       
    33   fun pat_tr ctxt p e opti = (* %x. case x of p => e | _ => [] *)
       
    34     let
       
    35       val x = Free (Name.variant (fold Term.add_free_names [p, e] []) "x", dummyT);
       
    36       val e = if opti then fsingl e else e;
       
    37       val case1 = Syntax.const "_case1" $ p $ e;
       
    38       val case2 = Syntax.const "_case1" $ Syntax.const Term.dummy_patternN
       
    39                                         $ femptyC;
       
    40       val cs = Syntax.const "_case2" $ case1 $ case2
       
    41       val ft = Datatype_Case.case_tr false Datatype.info_of_constr
       
    42                  ctxt [x, cs]
       
    43     in lambda x ft end;
       
    44 
       
    45   fun abs_tr ctxt (p as Free(s,T)) e opti =
       
    46         let val thy = ProofContext.theory_of ctxt;
       
    47             val s' = Sign.intern_const thy s
       
    48         in if Sign.declared_const thy s'
       
    49            then (pat_tr ctxt p e opti, false)
       
    50            else (lambda p e, true)
       
    51         end
       
    52     | abs_tr ctxt p e opti = (pat_tr ctxt p e opti, false);
       
    53 
       
    54   fun fsc_tr ctxt [e, Const("_fsc_test",_) $ b, qs] =
       
    55         let 
       
    56           val res = case qs of 
       
    57                       Const("_fsc_end",_) => fsingl e
       
    58                     | Const("_fsc_quals",_)$ q $ qs => fsc_tr ctxt [e, q, qs];
       
    59         in 
       
    60           IfC $ b $ res $ femptyC 
       
    61         end
       
    62 
       
    63     | fsc_tr ctxt [e, Const("_fsc_gen",_) $ p $ es, Const("_fsc_end",_)] =
       
    64          (case abs_tr ctxt p e true of
       
    65             (f,true) => fmapC $ f $ es
       
    66           | (f, false) => fconcatC $ (fmapC $ f $ es))
       
    67        
       
    68     | fsc_tr ctxt [e, Const("_fsc_gen",_) $ p $ es, Const("_fsc_quals",_) $ q $ qs] =
       
    69         let
       
    70           val e' = fsc_tr ctxt [e, q, qs];
       
    71         in 
       
    72           fconcatC $ (fmapC $ (fst (abs_tr ctxt p e' false)) $ es) 
       
    73         end
       
    74 
       
    75 in [("_fsetcompr", fsc_tr)] end
       
    76 *}
       
    77 
       
    78 
       
    79 (* NEEDS FIXING *)
       
    80 (* examles *)
       
    81 (*
       
    82 term "{|(x,y,z). b|}"
       
    83 term "{|x. x \<leftarrow> xs|}"
       
    84 term "{|(x,y,z). x\<leftarrow>xs|}"
       
    85 term "{|e x y. x\<leftarrow>xs, y\<leftarrow>ys|}"
       
    86 term "{|(x,y,z). x<a, x>b|}"
       
    87 term "{|(x,y,z). x\<leftarrow>xs, x>b|}"
       
    88 term "{|(x,y,z). x<a, x\<leftarrow>xs|}"
       
    89 term "{|(x,y). Cons True x \<leftarrow> xs|}"
       
    90 term "{|(x,y,z). Cons x [] \<leftarrow> xs|}"
       
    91 term "{|(x,y,z). x<a, x>b, x=d|}"
       
    92 term "{|(x,y,z). x<a, x>b, y\<leftarrow>ys|}"
       
    93 term "{|(x,y,z). x<a, x\<leftarrow>xs,y>b|}"
       
    94 term "{|(x,y,z). x<a, x\<leftarrow>xs, y\<leftarrow>ys|}"
       
    95 term "{|(x,y,z). x\<leftarrow>xs, x>b, y<a|}"
       
    96 term "{|(x,y,z). x\<leftarrow>xs, x>b, y\<leftarrow>ys|}"
       
    97 term "{|(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,y>x|}"
       
    98 term "{|(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,z\<leftarrow>zs|}"
       
    99 *)
       
   100 
       
   101 (* BELOW CONSTRUCTION SITE *)
       
   102 
       
   103 
       
   104 end