# HG changeset patch # User wenzelm # Date 1627917616 -7200 # Node ID d3d6e01a6b002929ae2bf6e7771d8ee2f32bc84c # Parent fa92c5f8af865919c9dcd000360e53eaaec5e74b more operations on types and terms; abstract syntax operations for Pure and HOL; diff -r fa92c5f8af86 -r d3d6e01a6b00 src/Tools/Haskell/Haskell.thy --- a/src/Tools/Haskell/Haskell.thy Mon Aug 02 14:08:42 2021 +0200 +++ b/src/Tools/Haskell/Haskell.thy Mon Aug 02 17:20:16 2021 +0200 @@ -5,7 +5,7 @@ *) theory Haskell - imports Pure + imports Main begin generate_file "Isabelle/Bytes.hs" = \ @@ -1773,48 +1773,120 @@ {-# LANGUAGE OverloadedStrings #-} module Isabelle.Term ( - Indexname, - - Sort, dummyS, - - Typ(..), dummyT, is_dummyT, Term(..), - aconv, list_comb, strip_comb, head_of, lambda) + Name, Indexname, Sort, Typ(..), Term(..), Free, + type_op0, type_op1, op0, op1, op2, typed_op2, binder, + dummyS, dummyT, is_dummyT, propT, is_propT, (-->), dest_funT, (--->), + aconv, list_comb, strip_comb, head_of, lambda +) where import Isabelle.Bytes (Bytes) - -type Indexname = (Bytes, Int) - - -type Sort = [Bytes] +infixr 5 --> +infixr ---> + + +{- types and terms -} + +type Name = Bytes + +type Indexname = (Name, Int) + +type Sort = [Name] + +data Typ = + Type (Name, [Typ]) + | TFree (Name, Sort) + | TVar (Indexname, Sort) + deriving (Show, Eq, Ord) + +data Term = + Const (Name, [Typ]) + | Free (Name, Typ) + | Var (Indexname, Typ) + | Bound Int + | Abs (Name, Typ, Term) + | App (Term, Term) + deriving (Show, Eq, Ord) + +type Free = (Name, Typ) + + +{- type and term operators -} + +type_op0 :: Name -> (Typ, Typ -> Bool) +type_op0 name = (mk, is) + where + mk = Type (name, []) + is (Type (name, _)) = True + is _ = False + +type_op1 :: Name -> (Typ -> Typ, Typ -> Maybe Typ) +type_op1 name = (mk, dest) + where + mk ty = Type (name, [ty]) + dest (Type (name, [ty])) = Just ty + dest _ = Nothing + +type_op2 :: Name -> (Typ -> Typ -> Typ, Typ -> Maybe (Typ, Typ)) +type_op2 name = (mk, dest) + where + mk ty1 ty2 = Type (name, [ty1, ty2]) + dest (Type (name, [ty1, ty2])) = Just (ty1, ty2) + dest _ = Nothing + +op0 :: Name -> (Term, Term -> Bool) +op0 name = (mk, is) + where + mk = Const (name, []) + is (Const (c, _)) = c == name + is _ = False + +op1 :: Name -> (Term -> Term, Term -> Maybe Term) +op1 name = (mk, dest) + where + mk t = App (Const (name, []), t) + dest (App (Const (c, _), t)) | c == name = Just t + dest _ = Nothing + +op2 :: Name -> (Term -> Term -> Term, Term -> Maybe (Term, Term)) +op2 name = (mk, dest) + where + mk t u = App (App (Const (name, []), t), u) + dest (App (App (Const (c, _), t), u)) | c == name = Just (t, u) + dest _ = Nothing + +typed_op2 :: Name -> (Typ -> Term -> Term -> Term, Term -> Maybe (Typ, Term, Term)) +typed_op2 name = (mk, dest) + where + mk ty t u = App (App (Const (name, [ty]), t), u) + dest (App (App (Const (c, [ty]), t), u)) | c == name = Just (ty, t, u) + dest _ = Nothing + +binder :: Name -> Free -> Term -> Term +binder c (a, ty) b = App (Const (c, [ty]), lambda (a, ty) b) + + +{- type operations -} dummyS :: Sort dummyS = [""] - -data Typ = - Type (Bytes, [Typ]) - | TFree (Bytes, Sort) - | TVar (Indexname, Sort) - deriving (Show, Eq, Ord) - -dummyT :: Typ -dummyT = Type (\\<^type_name>\dummy\\, []) - -is_dummyT :: Typ -> Bool -is_dummyT (Type (\\<^type_name>\dummy\\, [])) = True -is_dummyT _ = False - - -data Term = - Const (Bytes, [Typ]) - | Free (Bytes, Typ) - | Var (Indexname, Typ) - | Bound Int - | Abs (Bytes, Typ, Term) - | App (Term, Term) - deriving (Show, Eq, Ord) +dummyT :: Typ; is_dummyT :: Typ -> Bool +(dummyT, is_dummyT) = type_op0 \\<^type_name>\dummy\\ + +propT :: Typ; is_propT :: Typ -> Bool +(propT, is_propT) = type_op0 \\<^type_name>\prop\\ + +(-->) :: Typ -> Typ -> Typ; dest_funT :: Typ -> Maybe (Typ, Typ) +((-->), dest_funT) = type_op2 \\<^type_name>\fun\\ + +(--->) :: [Typ] -> Typ -> Typ +[] ---> b = b +(a : as) ---> b = a --> (as ---> b) + + +{- term operations -} aconv :: Term -> Term -> Bool aconv (App (t1, u1)) (App (t2, u2)) = aconv t1 t2 && aconv u1 u2 @@ -1835,7 +1907,7 @@ head_of (App (f, _)) = head_of f head_of u = u -lambda :: (Bytes, Typ) -> Term -> Term +lambda :: Free -> Term -> Term lambda (name, typ) body = Abs (name, typ, abstract 0 body) where abstract lev (Free (x, ty)) | name == x && typ == ty = Bound lev @@ -1844,6 +1916,109 @@ abstract _ t = t \ +generate_file "Isabelle/Pure.hs" = \ +{- Title: Isabelle/Term.hs + Author: Makarius + LICENSE: BSD 3-clause (Isabelle) + +Support for Isabelle/Pure logic. + +See also \<^file>\$ISABELLE_HOME/src/Pure/logic.ML\. +-} + +{-# LANGUAGE OverloadedStrings #-} + +module Isabelle.Pure ( + mk_forall, mk_equals, dest_equals, mk_implies, dest_implies +) +where + +import Isabelle.Term + +mk_forall :: Free -> Term -> Term +mk_forall = binder \\<^const_name>\Pure.all\\ + +mk_equals :: Typ -> Term -> Term -> Term; dest_equals :: Term -> Maybe (Typ, Term, Term) +(mk_equals, dest_equals) = typed_op2 \\<^const_name>\Pure.eq\\ + +mk_implies :: Term -> Term -> Term; dest_implies :: Term -> Maybe (Term, Term) +(mk_implies, dest_implies) = op2 \\<^const_name>\Pure.imp\\ +\ + +generate_file "Isabelle/HOL.hs" = \ +{- Title: Isabelle/Term.hs + Author: Makarius + LICENSE: BSD 3-clause (Isabelle) + +Support for Isabelle/HOL logic. + +See also \<^file>\$ISABELLE_HOME/src/HOL/Tools/hologic.ML\. +-} + +{-# LANGUAGE OverloadedStrings #-} + +module Isabelle.HOL ( + boolT, is_boolT, mk_trueprop, dest_trueprop, + mk_setT, dest_setT, mk_mem, dest_mem, + mk_eq, dest_eq, true, is_true, false, is_false, + mk_not, dest_not, mk_conj, dest_conj, mk_disj, dest_disj, + mk_imp, dest_imp, mk_iff, dest_iff, + mk_all, mk_ex +) +where + +import Isabelle.Term + + +boolT :: Typ; is_boolT :: Typ -> Bool +(boolT, is_boolT) = type_op0 \\<^type_name>\bool\\ + +mk_trueprop :: Term -> Term; dest_trueprop :: Term -> Maybe Term +(mk_trueprop, dest_trueprop) = op1 \\<^const_name>\Trueprop\\ + +mk_setT :: Typ -> Typ; dest_setT :: Typ -> Maybe Typ +(mk_setT, dest_setT) = type_op1 \\<^type_name>\set\\ + +mk_mem :: Typ -> Term -> Term -> Term; dest_mem :: Term -> Maybe (Typ, Term, Term) +(mk_mem, dest_mem) = typed_op2 \\<^const_name>\Set.member\\ + +mk_eq :: Typ -> Term -> Term -> Term; dest_eq :: Term -> Maybe (Typ, Term, Term) +(mk_eq, dest_eq) = typed_op2 \\<^const_name>\HOL.eq\\ + +true :: Term; is_true :: Term -> Bool +(true, is_true) = op0 \\<^const_name>\True\\ + +false :: Term; is_false :: Term -> Bool +(false, is_false) = op0 \\<^const_name>\False\\ + +mk_not :: Term -> Term; dest_not :: Term -> Maybe Term +(mk_not, dest_not) = op1 \\<^const_name>\Not\\ + +mk_conj :: Term -> Term -> Term; dest_conj :: Term -> Maybe (Term, Term) +(mk_conj, dest_conj) = op2 \\<^const_name>\conj\\ + +mk_disj :: Term -> Term -> Term; dest_disj :: Term -> Maybe (Term, Term) +(mk_disj, dest_disj) = op2 \\<^const_name>\disj\\ + +mk_imp :: Term -> Term -> Term; dest_imp :: Term -> Maybe (Term, Term) +(mk_imp, dest_imp) = op2 \\<^const_name>\implies\\ + +mk_iff :: Term -> Term -> Term +mk_iff = mk_eq boolT + +dest_iff :: Term -> Maybe (Term, Term) +dest_iff tm = + case dest_eq tm of + Just (ty, t, u) | ty == boolT -> Just (t, u) + _ -> Nothing + +mk_all :: Free -> Term -> Term +mk_all = binder \\<^const_name>\All\\ + +mk_ex :: Free -> Term -> Term +mk_ex = binder \\<^const_name>\Ex\\ +\ + generate_file "Isabelle/Term_XML/Encode.hs" = \ {- Title: Isabelle/Term_XML/Encode.hs Author: Makarius diff -r fa92c5f8af86 -r d3d6e01a6b00 src/Tools/ROOT --- a/src/Tools/ROOT Mon Aug 02 14:08:42 2021 +0200 +++ b/src/Tools/ROOT Mon Aug 02 17:20:16 2021 +0200 @@ -8,7 +8,7 @@ theories Examples -session Haskell in Haskell = Pure + +session Haskell in Haskell = HOL + theories Haskell theories [condition = ISABELLE_GHC_STACK]