diff -r f9bf5c08b446 -r d929a900584c src/HOL/Library/Pure_term.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/HOL/Library/Pure_term.thy Mon Mar 26 14:54:45 2007 +0200 @@ -0,0 +1,116 @@ +(* Title: HOL/Library/Pure_term.thy + ID: $Id$ + Author: Florian Haftmann, TU Muenchen +*) + +header {* Embedding (a subset of) the Pure term algebra in HOL *} + +theory Pure_term +imports MLString +begin + +subsection {* Definitions *} + +types vname = ml_string; +types "class" = ml_string; +types sort = "class list" + +datatype "typ" = + Type ml_string "typ list" (infix "{\}" 120) + | TFix vname sort (infix "\\" 117) + +abbreviation + Fun :: "typ \ typ \ typ" (infixr "\" 115) where + "ty1 \ ty2 \ Type (STR ''fun'') [ty1, ty2]" +abbreviation + Funs :: "typ list \ typ \ typ" (infixr "{\}" 115) where + "tys {\} ty \ foldr (op \) tys ty" + +datatype "term" = + Const ml_string "typ" (infix "\\" 112) + | Fix vname "typ" (infix ":\" 112) + | App "term" "term" (infixl "\" 110) + | Abs "vname \ typ" "term" (infixr "\" 111) + | Bnd nat + +abbreviation + Apps :: "term \ term list \ term" (infixl "{\}" 110) where + "t {\} ts \ foldl (op \) t ts" +abbreviation + Abss :: "(vname \ typ) list \ term \ term" (infixr "{\}" 111) where + "vs {\} t \ foldr (op \) vs t" + + +subsection {* ML interface *} + +ML {* +structure Pure_term = +struct + +val mk_sort = HOLogic.mk_list @{typ class} o map MLString.mk; + +fun mk_typ f (Type (tyco, tys)) = + @{term Type} $ MLString.mk tyco + $ HOLogic.mk_list @{typ typ} (map (mk_typ f) tys) + | mk_typ f (TFree v) = + f v; + +fun mk_term f g (Const (c, ty)) = + @{term Const} $ MLString.mk c $ g ty + | mk_term f g (t1 $ t2) = + @{term App} $ mk_term f g t1 $ mk_term f g t2 + | mk_term f g (Free v) = f v; + +end; +*} + + +subsection {* Code generator setup *} + +definition + Bound :: "int \ term" +where + "Bound k = Bnd (nat k)" + +lemma Bnd_Bound [code inline, code func]: + "Bnd n = Bound (int n)" + unfolding Bound_def by auto + +definition + Absp :: "vname \ typ \ term \ term" +where + "Absp v ty t = (v, ty) \ t" + +lemma Abs_Absp [code inline, code func]: + "(op \) (v, ty) = Absp v ty" + by rule (auto simp add: Absp_def) + +definition + "term_case' f g h k l = term_case f g h (\(v, ty). k v ty) (\n. l (int n))" + +lemma term_case' [code inline, code func]: + "term_case = (\f g h k l. term_case' f g h (\v ty. k (v, ty)) (\v. l (nat v)))" + unfolding term_case'_def by auto + +code_datatype Const App Fix Absp Bound +lemmas [code func] = Bnd_Bound Abs_Absp + +code_type "typ" and "term" + (SML "Term.typ" and "Term.term") + +code_const Type and TFix + (SML "Term.Type/ (_, _)" and "Term.TFree/ (_, _)") + +code_const Const and App and Fix + and Absp and Bound + (SML "Term.Const/ (_, _)" and "Term.$/ (_, _)" and "Term.Free/ (_, _)" + and "Term.Abs/ (_, _, _)" and "Term.Bound/ (IntInf.toInt/ _)") + +code_const term_rec and term_case and "size \ term \ nat" + (SML "!(_; _; _; _; _; raise Fail \"term'_rec\")" + and "!(_; _; _; _; _; raise Fail \"term'_case\")" + and "!(_; raise Fail \"size'_term\")") + +code_reserved SML Term + +end \ No newline at end of file