summaryrefslogtreecommitdiff
path: root/grammars/prelude
diff options
context:
space:
mode:
authoraarne <unknown>2003-09-22 13:16:55 +0000
committeraarne <unknown>2003-09-22 13:16:55 +0000
commitb1402e8bd6a68a891b00a214d6cf184d66defe19 (patch)
tree90372ac4e53dce91cf949dbf8e93be06f1d9e8bd /grammars/prelude
Founding the newly structured GF2.0 cvs archive.
Diffstat (limited to 'grammars/prelude')
-rw-r--r--grammars/prelude/Coordination.gf105
-rw-r--r--grammars/prelude/Predef.gf25
-rw-r--r--grammars/prelude/Prelude.gf83
3 files changed, 213 insertions, 0 deletions
diff --git a/grammars/prelude/Coordination.gf b/grammars/prelude/Coordination.gf
new file mode 100644
index 000000000..d8265e3c2
--- /dev/null
+++ b/grammars/prelude/Coordination.gf
@@ -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} =
+ \_,_,or,xs ->
+ {s =
+ table {p => table {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 = "," ;
+
+} ;
diff --git a/grammars/prelude/Predef.gf b/grammars/prelude/Predef.gf
new file mode 100644
index 000000000..a91681af6
--- /dev/null
+++ b/grammars/prelude/Predef.gf
@@ -0,0 +1,25 @@
+-- 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 definitions in AppPredefined.hs
+ oper Int : Type = variants {} ; ----
+
+ oper length : Tok -> Int = variants {} ;
+ oper drop : Int -> Tok -> Tok = variants {} ;
+ oper take : Int -> Tok -> Tok = variants {} ;
+ oper tk : Int -> Tok -> Tok = variants {} ;
+ oper dp : Int -> Tok -> Tok = variants {} ;
+ oper eqInt : Int -> Int -> PBool = variants {} ;
+ oper plus : Int -> Int -> Int = variants {} ;
+
+ oper eqStr : Tok -> Tok -> PBool = variants {} ;
+ oper eqTok : (P : Type) -> P -> P -> PBool = variants {} ;
+ oper show : (P : Type) -> P -> Tok = variants {} ;
+ oper read : (P : Type) -> Tok -> P = variants {} ;
+
+ } ;
+
diff --git a/grammars/prelude/Prelude.gf b/grammars/prelude/Prelude.gf
new file mode 100644
index 000000000..f5903d7ec
--- /dev/null
+++ b/grammars/prelude/Prelude.gf
@@ -0,0 +1,83 @@
+-- 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) ;
+
+-- 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_else 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 ;
+
+
+-- 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
+ oper isNil : Tok -> Bool = \b -> pbool2bool (Predef.eqStr [] b) ;
+
+ oper 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
+ oper pbool2bool : Predef.PBool -> Bool = \b -> case b of {
+ Predef.PFalse => False ; Predef.PTrue => True
+ } ;
+
+} ;