1
0
forked from GitHub/gf-core

newly organized resource with tense in lib

This commit is contained in:
aarne
2004-11-16 16:28:22 +00:00
parent 7a16b4501f
commit 2bef5f7a1c
21 changed files with 3941 additions and 0 deletions

105
lib/prelude/Coordination.gf Normal file
View File

@@ -0,0 +1,105 @@
resource Coordination = {
param
ListSize = TwoElem | ManyElem ;
oper
SS = {s : Str} ; ----
ListX = {s1,s2 : Str} ;
twoStr : (x,y : Str) -> ListX = \x,y ->
{s1 = x ; s2 = y} ;
consStr : Str -> ListX -> Str -> ListX = \comma,xs,x ->
{s1 = xs.s1 ++ comma ++ xs.s2 ; s2 = x } ;
twoSS : (_,_ : SS) -> ListX = \x,y ->
twoStr x.s y.s ;
consSS : Str -> ListX -> SS -> ListX = \comma,xs,x ->
consStr comma xs x.s ;
Conjunction : Type = SS ;
ConjunctionDistr : Type = {s1 : Str ; s2 : Str} ;
conjunctX : Conjunction -> ListX -> Str = \or,xs ->
xs.s1 ++ or.s ++ xs.s2 ;
conjunctDistrX : ConjunctionDistr -> ListX -> Str = \or,xs ->
or.s1 ++ xs.s1 ++ or.s2 ++ xs.s2 ;
-- all this lifted to tables
ListTable : Type -> Type = \P -> {s1,s2 : P => Str} ;
twoTable : (P : Type) -> (_,_ : {s : P => Str}) -> ListTable P = \_,x,y ->
{s1 = x.s ; s2 = y.s} ;
consTable : (P : Type) -> Str -> ListTable P -> {s : P => Str} -> ListTable P =
\P,c,xs,x ->
{s1 = table P {o => xs.s1 ! o ++ c ++ xs.s2 ! o} ; s2 = x.s} ;
conjunctTable : (P : Type) -> Conjunction -> ListTable P -> {s : P => Str} =
\P,or,xs ->
{s = table P {p => xs.s1 ! p ++ or.s ++ xs.s2 ! p}} ;
conjunctDistrTable :
(P : Type) -> ConjunctionDistr -> ListTable P -> {s : P => Str} = \P,or,xs ->
{s = table P {p => or.s1++ xs.s1 ! p ++ or.s2 ++ xs.s2 ! p}} ;
-- ... and to two- and three-argument tables: how clumsy! ---
ListTable2 : Type -> Type -> Type = \P,Q ->
{s1,s2 : P => Q => Str} ;
twoTable2 : (P,Q : Type) -> (_,_ : {s : P => Q => Str}) -> ListTable2 P Q =
\_,_,x,y ->
{s1 = x.s ; s2 = y.s} ;
consTable2 :
(P,Q : Type) -> Str -> ListTable2 P Q -> {s : P => Q => Str} -> ListTable2 P Q =
\P,Q,c,xs,x ->
{s1 = table P {p => table Q {q => xs.s1 ! p ! q ++ c ++ xs.s2 ! p! q}} ;
s2 = x.s
} ;
conjunctTable2 :
(P,Q : Type) -> Conjunction -> ListTable2 P Q -> {s : P => Q => Str} =
\P,Q,or,xs ->
{s = table P {p => table Q {q => xs.s1 ! p ! q ++ or.s ++ xs.s2 ! p ! q}}} ;
conjunctDistrTable2 :
(P,Q : Type) -> ConjunctionDistr -> ListTable2 P Q -> {s : P => Q => Str} =
\P,Q,or,xs ->
{s =
table P {p => table Q {q => or.s1++ xs.s1 ! p ! q ++ or.s2 ++ xs.s2 ! p ! q}}} ;
ListTable3 : Type -> Type -> Type -> Type = \P,Q,R ->
{s1,s2 : P => Q => R => Str} ;
twoTable3 : (P,Q,R : Type) -> (_,_ : {s : P => Q => R => Str}) ->
ListTable3 P Q R =
\_,_,_,x,y ->
{s1 = x.s ; s2 = y.s} ;
consTable3 :
(P,Q,R : Type) -> Str -> ListTable3 P Q R -> {s : P => Q => R => Str} ->
ListTable3 P Q R =
\P,Q,R,c,xs,x ->
{s1 = \\p,q,r => xs.s1 ! p ! q ! r ++ c ++ xs.s2 ! p ! q ! r ;
s2 = x.s
} ;
conjunctTable3 :
(P,Q,R : Type) -> Conjunction -> ListTable3 P Q R -> {s : P => Q => R => Str} =
\P,Q,R,or,xs ->
{s = \\p,q,r => xs.s1 ! p ! q ! r ++ or.s ++ xs.s2 ! p ! q ! r} ;
conjunctDistrTable3 :
(P,Q,R : Type) -> ConjunctionDistr -> ListTable3 P Q R ->
{s : P => Q => R => Str} =
\P,Q,R,or,xs ->
{s = \\p,q,r => or.s1++ xs.s1 ! p ! q ! r ++ or.s2 ++ xs.s2 ! p ! q ! r} ;
comma = "," ;
} ;

8
lib/prelude/HTML.gf Normal file
View File

@@ -0,0 +1,8 @@
resource HTML = open Prelude in {
oper
tag : Str -> Str = \t -> "<" + t + ">" ;
endtag : Str -> Str = \t -> tag ("/" + t) ;
intag : Str -> Str -> Str = \t,s -> tag t ++ s ++ endtag t ;
intagAttr : Str -> Str -> Str -> Str =
\t,a,s -> ("<" + t) ++ (a + ">") ++ s ++ endtag t ;
}

12
lib/prelude/Latex.gf Normal file
View File

@@ -0,0 +1,12 @@
resource Latex = open Prelude in {
oper
command : Str -> Str = \c -> "\\" + c ;
fun1 : Str -> Str -> Str = \f,x -> "\\" + f + "{" ++ x ++ "}" ;
fun2 : Str -> Str -> Str -> Str =
\f,x,y -> "\\" + f + "{" ++ x ++ "}{" ++ y ++ "}" ;
begin : Str -> Str = \e -> "\\begin{" + e + "}" ;
end : Str -> Str = \e -> "\\end{" + e + "}" ;
inEnv : Str -> Str -> Str = \e,s -> begin e ++ s ++ end e ;
}

117
lib/prelude/Precedence.gf Normal file
View File

@@ -0,0 +1,117 @@
-- operations for precedence-dependent strings.
-- five levels:
-- p4 (constants), p3 (applications), p2 (products), p1 (sums), p0 (arrows)
resource Precedence = open Prelude in {
param
Prec = p4 | p3 | p2 | p1 | p0 ;
lintype
PrecTerm = Prec => Str ;
oper
pss : PrecTerm -> {s : PrecTerm} = \s -> {s = s} ;
-- change this if you want some other type of parentheses
mkParenth : Str -> Str = \str -> "(" ++ str ++ ")" ;
-- define ordering of precedences
nextPrec : Prec => Prec =
table {p0 => p1 ; p1 => p2 ; p2 => p3 ; _ => p4} ;
prevPrec : Prec => Prec =
table {p4 => p3 ; p3 => p2 ; p2 => p1 ; _ => p0} ;
mkPrec : Str -> Prec => Prec => Str = \str ->
table {
p4 => table { -- use the term of precedence p4...
_ => str} ; -- ...always without parentheses
p3 => table { -- use the term of precedence p3...
p4 => mkParenth str ; -- ...in parentheses if p4 is required...
_ => str} ; -- ...otherwise without parentheses
p2 => table {
p4 => mkParenth str ;
p3 => mkParenth str ;
_ => str} ;
p1 => table {
p1 => str ;
p0 => str ;
_ => mkParenth str} ;
p0 => table {
p0 => str ;
_ => mkParenth str}
} ;
-- make a string into a constant, of precedence p4
mkConst : Str -> PrecTerm =
\f ->
mkPrec f ! p4 ;
-- make a string into a 1/2/3 -place prefix operator, of precedence p3
mkFun1 : Str -> PrecTerm -> PrecTerm =
\f -> \x ->
table {k => mkPrec (f ++ x ! p4) ! p3 ! k} ;
mkFun2 : Str -> PrecTerm -> PrecTerm -> PrecTerm =
\f -> \x -> \y ->
table {k => mkPrec (f ++ x ! p4 ++ y ! p4) ! p3 ! k} ;
mkFun3 : Str -> PrecTerm -> PrecTerm -> PrecTerm -> PrecTerm =
\f -> \x -> \y -> \z ->
table {k => mkPrec (f ++ x ! p4 ++ y ! p4 ++ z ! p4) ! p3 ! k} ;
-- make a string into a non/left/right -associative infix operator, of precedence p
mkInfix : Str -> Prec -> PrecTerm -> PrecTerm -> PrecTerm =
\f -> \p -> \x -> \y ->
table {k => mkPrec (x ! (nextPrec ! p) ++ f ++ y ! (nextPrec ! p)) ! p ! k} ;
mkInfixL : Str -> Prec -> PrecTerm -> PrecTerm -> PrecTerm =
\f -> \p -> \x -> \y ->
table {k => mkPrec (x ! p ++ f ++ y ! (nextPrec ! p)) ! p ! k} ;
mkInfixR : Str -> Prec -> PrecTerm -> PrecTerm -> PrecTerm =
\f -> \p -> \x -> \y ->
table {k => mkPrec (x ! (nextPrec ! p) ++ f ++ y ! p) ! p ! k} ;
-----------------------------------------------------------
-- alternative:
-- precedence as inherent feature
lintype TermWithPrec = {s : Str ; p : Prec} ;
oper
mkpPrec : Str -> Prec -> TermWithPrec =
\f -> \p ->
{s = f ; p = p} ;
usePrec : TermWithPrec -> Prec -> Str =
\x -> \p ->
mkPrec x.s ! x.p ! p ;
-- make a string into a constant, of precedence p4
mkpConst : Str -> TermWithPrec =
\f ->
mkpPrec f p4 ;
-- make a string into a 1/2/3 -place prefix operator, of precedence p3
mkpFun1 : Str -> TermWithPrec -> TermWithPrec =
\f -> \x ->
mkpPrec (f ++ usePrec x p4) p3 ;
mkpFun2 : Str -> TermWithPrec -> TermWithPrec -> TermWithPrec =
\f -> \x -> \y ->
mkpPrec (f ++ usePrec x p4 ++ usePrec y p4) p3 ;
mkpFun3 : Str -> TermWithPrec -> TermWithPrec -> TermWithPrec -> TermWithPrec =
\f -> \x -> \y -> \z ->
mkpPrec (f ++ usePrec x p4 ++ usePrec y p4 ++ usePrec z p4) p3 ;
-- make a string a into non/left/right -associative infix operator, of precedence p
mkpInfix : Str -> Prec -> TermWithPrec -> TermWithPrec -> TermWithPrec =
\f -> \p -> \x -> \y ->
mkpPrec (usePrec x (nextPrec ! p) ++ f ++ usePrec y (nextPrec ! p)) p ;
mkpInfixL : Str -> Prec -> TermWithPrec -> TermWithPrec -> TermWithPrec =
\f -> \p -> \x -> \y ->
mkpPrec (usePrec x p ++ f ++ usePrec y (nextPrec ! p)) p ;
mkpInfixR : Str -> Prec -> TermWithPrec -> TermWithPrec -> TermWithPrec =
\f -> \p -> \x -> \y ->
mkpPrec (usePrec x (nextPrec ! p) ++ f ++ usePrec y p) p ;
} ;

27
lib/prelude/Predef.gf Normal file
View File

@@ -0,0 +1,27 @@
-- predefined functions for concrete syntax, defined in AppPredefined.hs
resource Predef = {
-- this type is for internal use only
param PBool = PTrue | PFalse ;
-- these operations have their proper definitions in AppPredefined.hs
oper Int : Type = variants {} ; -- the type of integers
oper Ints : Int -> Type = variants {} ; -- the type of integers from 0 to n
oper length : Tok -> Int = variants {} ; -- length of string
oper drop : Int -> Tok -> Tok = variants {} ; -- drop prefix of length
oper take : Int -> Tok -> Tok = variants {} ; -- take prefix of length
oper tk : Int -> Tok -> Tok = variants {} ; -- drop suffix of length
oper dp : Int -> Tok -> Tok = variants {} ; -- take suffix of length
oper eqInt : Int -> Int -> PBool = variants {} ; -- test if equal integers
oper lessInt: Int -> Int -> PBool = variants {} ; -- test order of integers
oper plus : Int -> Int -> Int = variants {} ; -- add integers
oper eqStr : Tok -> Tok -> PBool = variants {} ; -- test if equal strings
oper occur : Tok -> Tok -> PBool = variants {} ; -- test if occurs as substring
oper show : (P : Type) -> P -> Tok = variants {} ; -- convert param to string
oper read : (P : Type) -> Tok -> P = variants {} ; -- convert string to param
} ;

4
lib/prelude/PredefAbs.gf Normal file
View File

@@ -0,0 +1,4 @@
abstract PredefAbs = {
cat Int ; String ;
} ;

100
lib/prelude/Prelude.gf Normal file
View File

@@ -0,0 +1,100 @@
-- language-independent prelude facilities
resource Prelude = open (Predef=Predef) in {
oper
-- to construct records and tables
SS : Type = {s : Str} ;
ss : Str -> SS = \s -> {s = s} ;
ss2 : (_,_ : Str) -> SS = \x,y -> ss (x ++ y) ;
ss3 : (_,_ ,_: Str) -> SS = \x,y,z -> ss (x ++ y ++ z) ;
cc2 : (_,_ : SS) -> SS = \x,y -> ss (x.s ++ y.s) ;
SS1 : Type -> Type = \P -> {s : P => Str} ;
ss1 : (A : Type) -> Str -> SS1 A = \A,s -> {s = table {_ => s}} ;
SP1 : Type -> Type = \P -> {s : Str ; p : P} ;
sp1 : (A : Type) -> Str -> A -> SP1 A = \_,s,a -> {s = s ; p = a} ;
nonExist : Str = variants {} ;
optStr : Str -> Str = \s -> variants {s ; []} ;
constTable : (A,B : Type) -> B -> A => B = \_,_,b -> \\_ => b ;
constStr : (A : Type) -> Str -> A => Str = \A -> constTable A Str ;
infixSS : Str -> SS -> SS -> SS = \f,x,y -> ss (x.s ++ f ++ y.s) ;
prefixSS : Str -> SS -> SS = \f,x -> ss (f ++ x.s) ;
postfixSS : Str -> SS -> SS = \f,x -> ss (x.s ++ f) ;
embedSS : Str -> Str -> SS -> SS = \f,g,x -> ss (f ++ x.s ++ g) ;
id : (A : Type) -> A -> A ;
-- discontinuous
SD2 = {s1,s2 : Str} ;
sd2 : (_,_ : Str) -> SD2 = \x,y -> {s1 = x ; s2 = y} ;
-- parentheses
paren : Str -> Str = \s -> "(" ++ s ++ ")" ;
parenss : SS -> SS = \s -> ss (paren s.s) ;
-- free order between two strings
bothWays : Str -> Str -> Str = \x,y -> variants {x ++ y ; y ++ x} ;
-- parametric order between two strings
preOrPost : Bool -> Str -> Str -> Str = \pr,x,y ->
if_then_Str pr (x ++ y) (y ++ x) ;
-- Booleans
param Bool = True | False ;
oper
if_then_else : (A : Type) -> Bool -> A -> A -> A = \_,c,d,e ->
case c of {
True => d ; ---- should not need to qualify
False => e
} ;
andB : (_,_ : Bool) -> Bool = \a,b -> if_then_else Bool a b False ;
orB : (_,_ : Bool) -> Bool = \a,b -> if_then_else Bool a True b ;
notB : Bool -> Bool = \a -> if_then_else Bool a False True ;
if_then_Str : Bool -> Str -> Str -> Str = if_then_else Str ;
-- zero, one, two, or more (elements in a list etc)
param
ENumber = E0 | E1 | E2 | Emore ;
oper
eNext : ENumber -> ENumber = \e -> case e of {
E0 => E1 ; E1 => E2 ; _ => Emore} ;
-- these were defined in Predef before
isNil : Tok -> Bool = \b -> pbool2bool (Predef.eqStr [] b) ;
ifTok : (A : Type) -> Tok -> Tok -> A -> A -> A = \A,t,u,a,b ->
case Predef.eqStr t u of {Predef.PTrue => a ; Predef.PFalse => b} ;
-- so we need an interface
pbool2bool : Predef.PBool -> Bool = \b -> case b of {
Predef.PFalse => False ; Predef.PTrue => True
} ;
init : Tok -> Tok = Predef.tk 1 ;
last : Tok -> Tok = Predef.dp 1 ;
-- bind together two tokens in the lexer, either obligatorily or optionally
oper
glue : Str -> Str -> Str = \x,y -> x ++ BIND ++ y ;
glueOpt : Str -> Str -> Str = \x,y -> variants {glue x y ; x ++ y} ;
noglueOpt : Str -> Str -> Str = \x,y -> variants {x ++ y ; glue x y} ;
-- these should be hidden, and never changed since it's hardcoded in (un)lexers
BIND : Str = "&+" ;
PARA : Str = "&-" ;
} ;