Added list patterns. Added som simple prelude functions.

This commit is contained in:
bringert
2005-12-01 17:27:06 +00:00
parent 8de44f741e
commit 396cc63cfc
12 changed files with 890 additions and 705 deletions

View File

@@ -16,6 +16,8 @@ data ConsDecl_
type ConsDecl = Tree ConsDecl_
data Pattern_
type Pattern = Tree Pattern_
data PListElem_
type PListElem = Tree PListElem_
data FieldPattern_
type FieldPattern = Tree FieldPattern_
data Exp_
@@ -44,14 +46,17 @@ data Tree :: * -> * where
DeriveDecl :: Ident -> Ident -> Tree Decl_
ConsDecl :: Ident -> Exp -> Tree ConsDecl_
POr :: Pattern -> Pattern -> Tree Pattern_
PListCons :: Pattern -> Pattern -> Tree Pattern_
PConsTop :: Ident -> Pattern -> [Pattern] -> Tree Pattern_
PCons :: Ident -> [Pattern] -> Tree Pattern_
PRec :: [FieldPattern] -> Tree Pattern_
PList :: [PListElem] -> Tree Pattern_
PType :: Tree Pattern_
PStr :: String -> Tree Pattern_
PInt :: Integer -> Tree Pattern_
PVar :: Ident -> Tree Pattern_
PWild :: Tree Pattern_
PListElem :: Pattern -> Tree PListElem_
FieldPattern :: Ident -> Pattern -> Tree FieldPattern_
ELet :: [LetDef] -> Exp -> Tree Exp_
ECase :: Exp -> [Case] -> Tree Exp_
@@ -120,10 +125,13 @@ composOpM f t = case t of
DeriveDecl i0 i1 -> return DeriveDecl `ap` f i0 `ap` f i1
ConsDecl i exp -> return ConsDecl `ap` f i `ap` f exp
POr pattern0 pattern1 -> return POr `ap` f pattern0 `ap` f pattern1
PListCons pattern0 pattern1 -> return PListCons `ap` f pattern0 `ap` f pattern1
PConsTop i pattern patterns -> return PConsTop `ap` f i `ap` f pattern `ap` mapM f patterns
PCons i patterns -> return PCons `ap` f i `ap` mapM f patterns
PRec fieldpatterns -> return PRec `ap` mapM f fieldpatterns
PList plistelems -> return PList `ap` mapM f plistelems
PVar i -> return PVar `ap` f i
PListElem pattern -> return PListElem `ap` f pattern
FieldPattern i pattern -> return FieldPattern `ap` f i `ap` f pattern
ELet letdefs exp -> return ELet `ap` mapM f letdefs `ap` f exp
ECase exp cases -> return ECase `ap` f exp `ap` mapM f cases
@@ -174,10 +182,13 @@ composOpFold zero combine f t = case t of
DeriveDecl i0 i1 -> f i0 `combine` f i1
ConsDecl i exp -> f i `combine` f exp
POr pattern0 pattern1 -> f pattern0 `combine` f pattern1
PListCons pattern0 pattern1 -> f pattern0 `combine` f pattern1
PConsTop i pattern patterns -> f i `combine` f pattern `combine` foldr combine zero (map f patterns)
PCons i patterns -> f i `combine` foldr combine zero (map f patterns)
PRec fieldpatterns -> foldr combine zero (map f fieldpatterns)
PList plistelems -> foldr combine zero (map f plistelems)
PVar i -> f i
PListElem pattern -> f pattern
FieldPattern i pattern -> f i `combine` f pattern
ELet letdefs exp -> foldr combine zero (map f letdefs) `combine` f exp
ECase exp cases -> f exp `combine` foldr combine zero (map f cases)
@@ -228,14 +239,17 @@ instance Show (Tree c) where
DeriveDecl i0 i1 -> opar n . showString "DeriveDecl" . showChar ' ' . showsPrec 1 i0 . showChar ' ' . showsPrec 1 i1 . cpar n
ConsDecl i exp -> opar n . showString "ConsDecl" . showChar ' ' . showsPrec 1 i . showChar ' ' . showsPrec 1 exp . cpar n
POr pattern0 pattern1 -> opar n . showString "POr" . showChar ' ' . showsPrec 1 pattern0 . showChar ' ' . showsPrec 1 pattern1 . cpar n
PListCons pattern0 pattern1 -> opar n . showString "PListCons" . showChar ' ' . showsPrec 1 pattern0 . showChar ' ' . showsPrec 1 pattern1 . cpar n
PConsTop i pattern patterns -> opar n . showString "PConsTop" . showChar ' ' . showsPrec 1 i . showChar ' ' . showsPrec 1 pattern . showChar ' ' . showsPrec 1 patterns . cpar n
PCons i patterns -> opar n . showString "PCons" . showChar ' ' . showsPrec 1 i . showChar ' ' . showsPrec 1 patterns . cpar n
PRec fieldpatterns -> opar n . showString "PRec" . showChar ' ' . showsPrec 1 fieldpatterns . cpar n
PList plistelems -> opar n . showString "PList" . showChar ' ' . showsPrec 1 plistelems . cpar n
PType -> showString "PType"
PStr str -> opar n . showString "PStr" . showChar ' ' . showsPrec 1 str . cpar n
PInt n -> opar n . showString "PInt" . showChar ' ' . showsPrec 1 n . cpar n
PVar i -> opar n . showString "PVar" . showChar ' ' . showsPrec 1 i . cpar n
PWild -> showString "PWild"
PListElem pattern -> opar n . showString "PListElem" . showChar ' ' . showsPrec 1 pattern . cpar n
FieldPattern i pattern -> opar n . showString "FieldPattern" . showChar ' ' . showsPrec 1 i . showChar ' ' . showsPrec 1 pattern . cpar n
ELet letdefs exp -> opar n . showString "ELet" . showChar ' ' . showsPrec 1 letdefs . showChar ' ' . showsPrec 1 exp . cpar n
ECase exp cases -> opar n . showString "ECase" . showChar ' ' . showsPrec 1 exp . showChar ' ' . showsPrec 1 cases . cpar n
@@ -295,14 +309,17 @@ johnMajorEq (ValueDecl i patterns exp) (ValueDecl i_ patterns_ exp_) = i == i_ &
johnMajorEq (DeriveDecl i0 i1) (DeriveDecl i0_ i1_) = i0 == i0_ && i1 == i1_
johnMajorEq (ConsDecl i exp) (ConsDecl i_ exp_) = i == i_ && exp == exp_
johnMajorEq (POr pattern0 pattern1) (POr pattern0_ pattern1_) = pattern0 == pattern0_ && pattern1 == pattern1_
johnMajorEq (PListCons pattern0 pattern1) (PListCons pattern0_ pattern1_) = pattern0 == pattern0_ && pattern1 == pattern1_
johnMajorEq (PConsTop i pattern patterns) (PConsTop i_ pattern_ patterns_) = i == i_ && pattern == pattern_ && patterns == patterns_
johnMajorEq (PCons i patterns) (PCons i_ patterns_) = i == i_ && patterns == patterns_
johnMajorEq (PRec fieldpatterns) (PRec fieldpatterns_) = fieldpatterns == fieldpatterns_
johnMajorEq (PList plistelems) (PList plistelems_) = plistelems == plistelems_
johnMajorEq PType PType = True
johnMajorEq (PStr str) (PStr str_) = str == str_
johnMajorEq (PInt n) (PInt n_) = n == n_
johnMajorEq (PVar i) (PVar i_) = i == i_
johnMajorEq PWild PWild = True
johnMajorEq (PListElem pattern) (PListElem pattern_) = pattern == pattern_
johnMajorEq (FieldPattern i pattern) (FieldPattern i_ pattern_) = i == i_ && pattern == pattern_
johnMajorEq (ELet letdefs exp) (ELet letdefs_ exp_) = letdefs == letdefs_ && exp == exp_
johnMajorEq (ECase exp cases) (ECase exp_ cases_) = exp == exp_ && cases == cases_
@@ -361,59 +378,62 @@ instance Ord (Tree c) where
index (DeriveDecl _ _) = 5
index (ConsDecl _ _) = 6
index (POr _ _) = 7
index (PConsTop _ _ _) = 8
index (PCons _ _) = 9
index (PRec _) = 10
index (PType ) = 11
index (PStr _) = 12
index (PInt _) = 13
index (PVar _) = 14
index (PWild ) = 15
index (FieldPattern _ _) = 16
index (ELet _ _) = 17
index (ECase _ _) = 18
index (EIf _ _ _) = 19
index (EDo _ _) = 20
index (EAbs _ _) = 21
index (EPi _ _ _) = 22
index (EPiNoVar _ _) = 23
index (EBind _ _) = 24
index (EBindC _ _) = 25
index (EOr _ _) = 26
index (EAnd _ _) = 27
index (EEq _ _) = 28
index (ENe _ _) = 29
index (ELt _ _) = 30
index (ELe _ _) = 31
index (EGt _ _) = 32
index (EGe _ _) = 33
index (EListCons _ _) = 34
index (EAdd _ _) = 35
index (ESub _ _) = 36
index (EMul _ _) = 37
index (EDiv _ _) = 38
index (EMod _ _) = 39
index (ENeg _) = 40
index (EApp _ _) = 41
index (EProj _ _) = 42
index (ERecType _) = 43
index (ERec _) = 44
index (EList _) = 45
index (EVar _) = 46
index (EType ) = 47
index (EStr _) = 48
index (EInteger _) = 49
index (EDouble _) = 50
index (EMeta ) = 51
index (LetDef _ _ _) = 52
index (Case _ _) = 53
index (BindVar _ _) = 54
index (BindNoVar _) = 55
index (VVar _) = 56
index (VWild ) = 57
index (FieldType _ _) = 58
index (FieldValue _ _) = 59
index (Ident _) = 60
index (PListCons _ _) = 8
index (PConsTop _ _ _) = 9
index (PCons _ _) = 10
index (PRec _) = 11
index (PList _) = 12
index (PType ) = 13
index (PStr _) = 14
index (PInt _) = 15
index (PVar _) = 16
index (PWild ) = 17
index (PListElem _) = 18
index (FieldPattern _ _) = 19
index (ELet _ _) = 20
index (ECase _ _) = 21
index (EIf _ _ _) = 22
index (EDo _ _) = 23
index (EAbs _ _) = 24
index (EPi _ _ _) = 25
index (EPiNoVar _ _) = 26
index (EBind _ _) = 27
index (EBindC _ _) = 28
index (EOr _ _) = 29
index (EAnd _ _) = 30
index (EEq _ _) = 31
index (ENe _ _) = 32
index (ELt _ _) = 33
index (ELe _ _) = 34
index (EGt _ _) = 35
index (EGe _ _) = 36
index (EListCons _ _) = 37
index (EAdd _ _) = 38
index (ESub _ _) = 39
index (EMul _ _) = 40
index (EDiv _ _) = 41
index (EMod _ _) = 42
index (ENeg _) = 43
index (EApp _ _) = 44
index (EProj _ _) = 45
index (ERecType _) = 46
index (ERec _) = 47
index (EList _) = 48
index (EVar _) = 49
index (EType ) = 50
index (EStr _) = 51
index (EInteger _) = 52
index (EDouble _) = 53
index (EMeta ) = 54
index (LetDef _ _ _) = 55
index (Case _ _) = 56
index (BindVar _ _) = 57
index (BindNoVar _) = 58
index (VVar _) = 59
index (VWild ) = 60
index (FieldType _ _) = 61
index (FieldValue _ _) = 62
index (Ident _) = 63
compareSame (Module imports decls) (Module imports_ decls_) = mappend (compare imports imports_) (compare decls decls_)
compareSame (Import i) (Import i_) = compare i i_
compareSame (DataDecl i exp consdecls) (DataDecl i_ exp_ consdecls_) = mappend (compare i i_) (mappend (compare exp exp_) (compare consdecls consdecls_))
@@ -422,14 +442,17 @@ instance Ord (Tree c) where
compareSame (DeriveDecl i0 i1) (DeriveDecl i0_ i1_) = mappend (compare i0 i0_) (compare i1 i1_)
compareSame (ConsDecl i exp) (ConsDecl i_ exp_) = mappend (compare i i_) (compare exp exp_)
compareSame (POr pattern0 pattern1) (POr pattern0_ pattern1_) = mappend (compare pattern0 pattern0_) (compare pattern1 pattern1_)
compareSame (PListCons pattern0 pattern1) (PListCons pattern0_ pattern1_) = mappend (compare pattern0 pattern0_) (compare pattern1 pattern1_)
compareSame (PConsTop i pattern patterns) (PConsTop i_ pattern_ patterns_) = mappend (compare i i_) (mappend (compare pattern pattern_) (compare patterns patterns_))
compareSame (PCons i patterns) (PCons i_ patterns_) = mappend (compare i i_) (compare patterns patterns_)
compareSame (PRec fieldpatterns) (PRec fieldpatterns_) = compare fieldpatterns fieldpatterns_
compareSame (PList plistelems) (PList plistelems_) = compare plistelems plistelems_
compareSame PType PType = EQ
compareSame (PStr str) (PStr str_) = compare str str_
compareSame (PInt n) (PInt n_) = compare n n_
compareSame (PVar i) (PVar i_) = compare i i_
compareSame PWild PWild = EQ
compareSame (PListElem pattern) (PListElem pattern_) = compare pattern pattern_
compareSame (FieldPattern i pattern) (FieldPattern i_ pattern_) = mappend (compare i i_) (compare pattern pattern_)
compareSame (ELet letdefs exp) (ELet letdefs_ exp_) = mappend (compare letdefs letdefs_) (compare exp exp_)
compareSame (ECase exp cases) (ECase exp_ cases_) = mappend (compare exp exp_) (compare cases cases_)

View File

@@ -63,15 +63,15 @@ The symbols used in Syntax are the following: \\
\begin{tabular}{lll}
{\symb{;}} &{\symb{:}} &{\symb{\{}} \\
{\symb{\}}} &{\symb{{$=$}}} &{\symb{{$|$}{$|$}}} \\
{\symb{(}} &{\symb{)}} &{\symb{\_}} \\
{\symb{{$-$}{$>$}}} &{\symb{{$<$}{$-$}}} &{\symb{$\backslash$}} \\
{\symb{{$>$}{$>$}{$=$}}} &{\symb{{$>$}{$>$}}} &{\symb{\&\&}} \\
{\symb{{$=$}{$=$}}} &{\symb{/{$=$}}} &{\symb{{$<$}}} \\
{\symb{{$<$}{$=$}}} &{\symb{{$>$}}} &{\symb{{$>$}{$=$}}} \\
{\symb{::}} &{\symb{{$+$}}} &{\symb{{$-$}}} \\
{\symb{::}} &{\symb{(}} &{\symb{)}} \\
{\symb{[}} &{\symb{]}} &{\symb{,}} \\
{\symb{\_}} &{\symb{{$-$}{$>$}}} &{\symb{{$<$}{$-$}}} \\
{\symb{$\backslash$}} &{\symb{{$>$}{$>$}{$=$}}} &{\symb{{$>$}{$>$}}} \\
{\symb{\&\&}} &{\symb{{$=$}{$=$}}} &{\symb{/{$=$}}} \\
{\symb{{$<$}}} &{\symb{{$<$}{$=$}}} &{\symb{{$>$}}} \\
{\symb{{$>$}{$=$}}} &{\symb{{$+$}}} &{\symb{{$-$}}} \\
{\symb{*}} &{\symb{/}} &{\symb{\%}} \\
{\symb{.}} &{\symb{[}} &{\symb{]}} \\
{\symb{?}} &{\symb{,}} & \\
{\symb{.}} &{\symb{?}} & \\
\end{tabular}\\
\subsection*{Comments}
@@ -126,12 +126,18 @@ All other symbols are terminals.\\
\end{tabular}\\
\begin{tabular}{lll}
{\nonterminal{Pattern1}} & {\arrow} &{\nonterminal{Ident}} {\nonterminal{Pattern2}} {\nonterminal{ListPattern}} \\
{\nonterminal{Pattern1}} & {\arrow} &{\nonterminal{Pattern2}} {\terminal{::}} {\nonterminal{Pattern1}} \\
& {\delimit} &{\nonterminal{Pattern2}} \\
\end{tabular}\\
\begin{tabular}{lll}
{\nonterminal{Pattern2}} & {\arrow} &{\terminal{rec}} {\terminal{\{}} {\nonterminal{ListFieldPattern}} {\terminal{\}}} \\
{\nonterminal{Pattern2}} & {\arrow} &{\nonterminal{Ident}} {\nonterminal{Pattern3}} {\nonterminal{ListPattern}} \\
& {\delimit} &{\nonterminal{Pattern3}} \\
\end{tabular}\\
\begin{tabular}{lll}
{\nonterminal{Pattern3}} & {\arrow} &{\terminal{rec}} {\terminal{\{}} {\nonterminal{ListFieldPattern}} {\terminal{\}}} \\
& {\delimit} &{\terminal{[}} {\nonterminal{ListPListElem}} {\terminal{]}} \\
& {\delimit} &{\terminal{Type}} \\
& {\delimit} &{\nonterminal{String}} \\
& {\delimit} &{\nonterminal{Integer}} \\
@@ -140,9 +146,19 @@ All other symbols are terminals.\\
& {\delimit} &{\terminal{(}} {\nonterminal{Pattern}} {\terminal{)}} \\
\end{tabular}\\
\begin{tabular}{lll}
{\nonterminal{PListElem}} & {\arrow} &{\nonterminal{Pattern}} \\
\end{tabular}\\
\begin{tabular}{lll}
{\nonterminal{ListPListElem}} & {\arrow} &{\emptyP} \\
& {\delimit} &{\nonterminal{PListElem}} \\
& {\delimit} &{\nonterminal{PListElem}} {\terminal{,}} {\nonterminal{ListPListElem}} \\
\end{tabular}\\
\begin{tabular}{lll}
{\nonterminal{ListPattern}} & {\arrow} &{\emptyP} \\
& {\delimit} &{\nonterminal{Pattern2}} {\nonterminal{ListPattern}} \\
& {\delimit} &{\nonterminal{Pattern3}} {\nonterminal{ListPattern}} \\
\end{tabular}\\
\begin{tabular}{lll}

File diff suppressed because one or more lines are too long

View File

@@ -16,7 +16,7 @@ $i = [$l $d _ '] -- identifier character
$u = [\0-\255] -- universal: any character
@rsyms = -- reserved words consisting of special symbols
\; | \: | \{ | \} | \= | \| \| | \( | \) | \_ | \- \> | \< \- | \\ | \> \> \= | \> \> | \& \& | \= \= | \/ \= | \< | \< \= | \> | \> \= | \: \: | \+ | \- | \* | \/ | \% | \. | \[ | \] | \? | \,
\; | \: | \{ | \} | \= | \| \| | \: \: | \( | \) | \[ | \] | \, | \_ | \- \> | \< \- | \\ | \> \> \= | \> \> | \& \& | \= \= | \/ \= | \< | \< \= | \> | \> \= | \+ | \- | \* | \/ | \% | \. | \?
:-
"--" [.]* ; -- Toss single line comments

File diff suppressed because one or more lines are too long

View File

@@ -20,8 +20,12 @@ import Transfer.ErrM
'}' { PT _ (TS "}") }
'=' { PT _ (TS "=") }
'||' { PT _ (TS "||") }
'::' { PT _ (TS "::") }
'(' { PT _ (TS "(") }
')' { PT _ (TS ")") }
'[' { PT _ (TS "[") }
']' { PT _ (TS "]") }
',' { PT _ (TS ",") }
'_' { PT _ (TS "_") }
'->' { PT _ (TS "->") }
'<-' { PT _ (TS "<-") }
@@ -35,17 +39,13 @@ import Transfer.ErrM
'<=' { PT _ (TS "<=") }
'>' { PT _ (TS ">") }
'>=' { PT _ (TS ">=") }
'::' { PT _ (TS "::") }
'+' { PT _ (TS "+") }
'-' { PT _ (TS "-") }
'*' { PT _ (TS "*") }
'/' { PT _ (TS "/") }
'%' { PT _ (TS "%") }
'.' { PT _ (TS ".") }
'[' { PT _ (TS "[") }
']' { PT _ (TS "]") }
'?' { PT _ (TS "?") }
',' { PT _ (TS ",") }
'Type' { PT _ (TS "Type") }
'case' { PT _ (TS "case") }
'data' { PT _ (TS "data") }
@@ -119,12 +119,18 @@ Pattern : Pattern1 '||' Pattern { POr $1 $3 }
Pattern1 :: { Pattern }
Pattern1 : Ident Pattern2 ListPattern { PConsTop $1 $2 (reverse $3) }
Pattern1 : Pattern2 '::' Pattern1 { PListCons $1 $3 }
| Pattern2 { $1 }
Pattern2 :: { Pattern }
Pattern2 : 'rec' '{' ListFieldPattern '}' { PRec $3 }
Pattern2 : Ident Pattern3 ListPattern { PConsTop $1 $2 (reverse $3) }
| Pattern3 { $1 }
Pattern3 :: { Pattern }
Pattern3 : 'rec' '{' ListFieldPattern '}' { PRec $3 }
| '[' ListPListElem ']' { PList $2 }
| 'Type' { PType }
| String { PStr $1 }
| Integer { PInt $1 }
@@ -133,9 +139,19 @@ Pattern2 : 'rec' '{' ListFieldPattern '}' { PRec $3 }
| '(' Pattern ')' { $2 }
PListElem :: { PListElem }
PListElem : Pattern { PListElem $1 }
ListPListElem :: { [PListElem] }
ListPListElem : {- empty -} { [] }
| PListElem { (:[]) $1 }
| PListElem ',' ListPListElem { (:) $1 $3 }
ListPattern :: { [Pattern] }
ListPattern : {- empty -} { [] }
| ListPattern Pattern2 { flip (:) $1 $2 }
| ListPattern Pattern3 { flip (:) $1 $2 }
FieldPattern :: { FieldPattern }

View File

@@ -88,14 +88,17 @@ instance Print (Tree c) where
DeriveDecl i0 i1 -> prPrec _i 0 (concatD [doc (showString "derive") , prt 0 i0 , prt 0 i1])
ConsDecl i exp -> prPrec _i 0 (concatD [prt 0 i , doc (showString ":") , prt 0 exp])
POr pattern0 pattern1 -> prPrec _i 0 (concatD [prt 1 pattern0 , doc (showString "||") , prt 0 pattern1])
PConsTop i pattern patterns -> prPrec _i 1 (concatD [prt 0 i , prt 2 pattern , prt 0 patterns])
PCons i patterns -> prPrec _i 2 (concatD [doc (showString "(") , prt 0 i , prt 0 patterns , doc (showString ")")])
PRec fieldpatterns -> prPrec _i 2 (concatD [doc (showString "rec") , doc (showString "{") , prt 0 fieldpatterns , doc (showString "}")])
PType -> prPrec _i 2 (concatD [doc (showString "Type")])
PStr str -> prPrec _i 2 (concatD [prt 0 str])
PInt n -> prPrec _i 2 (concatD [prt 0 n])
PVar i -> prPrec _i 2 (concatD [prt 0 i])
PWild -> prPrec _i 2 (concatD [doc (showString "_")])
PListCons pattern0 pattern1 -> prPrec _i 1 (concatD [prt 2 pattern0 , doc (showString "::") , prt 1 pattern1])
PConsTop i pattern patterns -> prPrec _i 2 (concatD [prt 0 i , prt 3 pattern , prt 0 patterns])
PCons i patterns -> prPrec _i 3 (concatD [doc (showString "(") , prt 0 i , prt 0 patterns , doc (showString ")")])
PRec fieldpatterns -> prPrec _i 3 (concatD [doc (showString "rec") , doc (showString "{") , prt 0 fieldpatterns , doc (showString "}")])
PList plistelems -> prPrec _i 3 (concatD [doc (showString "[") , prt 0 plistelems , doc (showString "]")])
PType -> prPrec _i 3 (concatD [doc (showString "Type")])
PStr str -> prPrec _i 3 (concatD [prt 0 str])
PInt n -> prPrec _i 3 (concatD [prt 0 n])
PVar i -> prPrec _i 3 (concatD [prt 0 i])
PWild -> prPrec _i 3 (concatD [doc (showString "_")])
PListElem pattern -> prPrec _i 0 (concatD [prt 0 pattern])
FieldPattern i pattern -> prPrec _i 0 (concatD [prt 0 i , doc (showString "=") , prt 0 pattern])
ELet letdefs exp -> prPrec _i 0 (concatD [doc (showString "let") , doc (showString "{") , prt 0 letdefs , doc (showString "}") , doc (showString "in") , prt 0 exp])
ECase exp cases -> prPrec _i 0 (concatD [doc (showString "case") , prt 0 exp , doc (showString "of") , doc (showString "{") , prt 0 cases , doc (showString "}")])
@@ -157,10 +160,15 @@ instance Print [ConsDecl] where
[] -> (concatD [])
[x] -> (concatD [prt 0 x])
x:xs -> (concatD [prt 0 x , doc (showString ";") , prt 0 xs])
instance Print [PListElem] where
prt _ es = case es of
[] -> (concatD [])
[x] -> (concatD [prt 0 x])
x:xs -> (concatD [prt 0 x , doc (showString ",") , prt 0 xs])
instance Print [Pattern] where
prt _ es = case es of
[] -> (concatD [])
x:xs -> (concatD [prt 2 x , prt 0 xs])
x:xs -> (concatD [prt 3 x , prt 0 xs])
instance Print [FieldPattern] where
prt _ es = case es of
[] -> (concatD [])

View File

@@ -19,14 +19,17 @@ transTree t = case t of
DeriveDecl i0 i1 -> failure t
ConsDecl i exp -> failure t
POr pattern0 pattern1 -> failure t
PListCons pattern0 pattern1 -> failure t
PConsTop i pattern patterns -> failure t
PCons i patterns -> failure t
PRec fieldpatterns -> failure t
PList plistelems -> failure t
PType -> failure t
PStr str -> failure t
PInt n -> failure t
PVar i -> failure t
PWild -> failure t
PListElem pattern -> failure t
FieldPattern i pattern -> failure t
ELet letdefs exp -> failure t
ECase exp cases -> failure t
@@ -95,15 +98,21 @@ transConsDecl t = case t of
transPattern :: Pattern -> Result
transPattern t = case t of
POr pattern0 pattern1 -> failure t
PListCons pattern0 pattern1 -> failure t
PConsTop i pattern patterns -> failure t
PCons i patterns -> failure t
PRec fieldpatterns -> failure t
PList plistelems -> failure t
PType -> failure t
PStr str -> failure t
PInt n -> failure t
PVar i -> failure t
PWild -> failure t
transPListElem :: PListElem -> Result
transPListElem t = case t of
PListElem pattern -> failure t
transFieldPattern :: FieldPattern -> Result
transFieldPattern t = case t of
FieldPattern i pattern -> failure t

View File

@@ -24,30 +24,39 @@ separator ConsDecl ";" ;
-- Disjunctive patterns.
POr. Pattern ::= Pattern1 "||" Pattern ;
-- List constructor patterns
PListCons. Pattern1 ::= Pattern2 "::" Pattern1 ;
-- Hack: constructor applied to at least one pattern
-- this is to separate it from variable patterns
PConsTop. Pattern1 ::= Ident Pattern2 [Pattern] ;
PConsTop. Pattern2 ::= Ident Pattern3 [Pattern] ;
-- Real constructor pattern
internal PCons. Pattern2 ::= "(" Ident [Pattern] ")" ;
internal PCons. Pattern3 ::= "(" Ident [Pattern] ")" ;
-- Record patterns
PRec. Pattern2 ::= "rec" "{" [FieldPattern] "}";
-- The pattern matching the Type constant
PType. Pattern2 ::= "Type" ;
-- String literal patterns
PStr. Pattern2 ::= String ;
-- Integer literal patterns
PInt. Pattern2 ::= Integer ;
-- Variable patterns
PVar. Pattern2 ::= Ident ;
-- Wild card patterns
PWild. Pattern2 ::= "_" ;
PRec. Pattern3 ::= "rec" "{" [FieldPattern] "}";
coercions Pattern 2 ;
-- List patterns
PList. Pattern3 ::= "[" [PListElem] "]" ;
PListElem. PListElem ::= Pattern ;
separator PListElem "," ;
-- The pattern matching the Type constant
PType. Pattern3 ::= "Type" ;
-- String literal patterns
PStr. Pattern3 ::= String ;
-- Integer literal patterns
PInt. Pattern3 ::= Integer ;
-- Variable patterns
PVar. Pattern3 ::= Ident ;
-- Wild card patterns
PWild. Pattern3 ::= "_" ;
coercions Pattern 3 ;
[]. [Pattern] ::= ;
(:). [Pattern] ::= Pattern2 [Pattern] ;
(:). [Pattern] ::= Pattern3 [Pattern] ;
FieldPattern. FieldPattern ::= Ident "=" Pattern ;
separator FieldPattern ";" ;

View File

@@ -381,6 +381,8 @@ desugar = return . map f
where
f :: Tree a -> Tree a
f x = case x of
PListCons p1 p2 -> pListCons <| p1 <| p2
PList xs -> pList (map f [p | PListElem p <- xs])
EIf exp0 exp1 exp2 -> ifBool <| exp0 <| exp1 <| exp2
EDo bs e -> mkDo (map f bs) (f e)
BindNoVar exp0 -> BindVar VWild <| exp0
@@ -406,6 +408,16 @@ desugar = return . map f
_ -> composOp f x
where g <| x = g (f x)
--
-- * List patterns
--
pListCons :: Pattern -> Pattern -> Pattern
pListCons p1 p2 = PCons (Ident "Cons") [PWild,p1,p2]
pList :: [Pattern] -> Pattern
pList = foldr pListCons (PCons (Ident "Nil") [PWild])
--
-- * Use an overloaded function.
--

View File

@@ -0,0 +1,6 @@
import prelude
l1 = append ? [1,2,3,5,6] [3]
l2 = 2 :: l1
main = rec { x = length ? l2; y = l2}

View File

@@ -13,6 +13,11 @@ const _ _ x _ = x
id : (A:Type) -> A -> A
id _ x = x
flip : (A:Type) -> (B:Type) -> (C:Type) -> (A -> B -> C) -> B -> A -> C
flip _ _ _ f x y = f y x
compose : (A:Type) -> (B:Type) -> (C:Type) -> (B -> C) -> (A -> B) -> A -> C
compose _ _ _ f g x = f (g x)
--
-- The Integer type
@@ -117,13 +122,16 @@ data List : (_:Type) -> Type where
Nil : (A:Type) -> List A
Cons : (A:Type) -> A -> List A -> List A
size : (A:Type) -> List A -> Nat
size _ (Nil _) = Zero
size A (Cons _ x xs) = Succ (size A xs)
foldr : (A : Type) -> (B : Type) -> (A -> B -> B) -> B -> List A -> B
foldr _ _ _ x [] = x
foldr A B f x (y::ys) = f y (foldr A B f x ys)
length : (A:Type) -> List A -> Integer
length A = foldr A Integer (\_ -> \y -> y+1) 0
map : (A:Type) -> (B:Type) -> (A -> B) -> List A -> List B
map _ B _ (Nil _) = Nil B
map A B f (Cons _ x xs) = Cons B (f x) (map A B f xs)
map _ _ _ [] = []
map A B f (x::xs) = f x :: map A B f xs
append : (A:Type) -> List A -> List A -> List A
append A xs ys = foldr A (List A) (Cons A) ys xs
@@ -131,9 +139,7 @@ append A xs ys = foldr A (List A) (Cons A) ys xs
concat : (A : Type) -> List (List A) -> List A
concat A = foldr (List A) (List A) (append A) (Nil A)
foldr : (A : Type) -> (B : Type) -> (A -> B -> B) -> B -> List A -> B
foldr _ _ _ x (Nil _) = x
foldr A B f x (Cons _ y ys) = f y (foldr A B f x ys)
-- Instances: