Skip to content

Commit 05014e9

Browse files
committed
Type annotations for option labels + new bubble impl
1 parent 5f7f5d6 commit 05014e9

File tree

7 files changed

+171
-181
lines changed

7 files changed

+171
-181
lines changed

src/compiler/api/GF/Compile/Compute/Concrete2.hs

Lines changed: 131 additions & 161 deletions
Large diffs are not rendered by default.

src/compiler/api/GF/Compile/Repl.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import GF.Compile.Compute.Concrete2
2323
, ChoiceMap
2424
, Globals(Gl)
2525
, OptionInfo(..)
26+
, bubble
2627
, stdPredef
2728
, unit
2829
, eval
@@ -57,7 +58,6 @@ import GF.Infra.Ident (moduleNameS)
5758
import GF.Infra.Option (noOptions)
5859
import GF.Infra.UseIO (justModuleName)
5960
import GF.Text.Pretty (render)
60-
import Debug.Trace
6161

6262
data ReplOpts = ReplOpts
6363
{ lang :: Lang
@@ -282,11 +282,11 @@ runRepl' opts@ReplOpts { lang, evalToFlat } gl@(Gl g _) = do
282282
outputStrLn $ show i ++ (if null opts then ". " else "*. ") ++ render (ppTerm Unqualified 0 r)
283283

284284
outputOptions ois os =
285-
forM_ ois $ \(OptionInfo c n ls) -> do
285+
forM_ ois $ \(OptionInfo c _ n ls) -> do
286286
outputStrLn ""
287287
outputStrLn $ show (unchoice c) ++ ") " ++ render (ppValue Unqualified 0 n)
288288
let sel = fromMaybe 0 (Map.lookup c os) + 1
289-
forM_ (zip [1..] ls) $ \(i, l) ->
289+
forM_ (zip [1..] ls) $ \(i, (_,l)) ->
290290
outputStrLn $ (if i == sel then "->" else " ") ++ show i ++ ". " ++ render (ppValue Unqualified 0 l)
291291

292292
runRepl :: ReplOpts -> IO ()

src/compiler/api/GF/Compile/TypeCheck/ConcreteNew.hs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,17 @@ tcRho scope c (Reset ctl t) mb_ty =
385385
case ty of
386386
VApp c id [] -> return (Reset (Coordination mb_mn conj (snd id)) t, ty)
387387
_ -> evalError (pp "Needs atomic type"<+>ppValue Unqualified 0 ty)
388-
tcRho scope s (Opts n cs) mb_ty = do
388+
tcRho scope s (Opts (nty,n) cs) mb_ty = do
389+
gl <- globals
389390
let (s1,s2,s3) = split3 s
390-
(n,_) <- tcRho scope s1 n Nothing
391-
(ls,_) <- tcUnifying scope s2 (fst <$> cs) Nothing
391+
(n,nty) <- tcRho scope s1 n (nty <&> \ty -> eval gl [] poison ty [])
392+
nty <- value2termM True [] nty
393+
ls <- forCM s2 cs $ \s' ((lty,l),_) -> do
394+
(l,lty) <- tcRho scope s' l (lty <&> \ty -> eval gl [] poison ty [])
395+
lty <- value2termM True [] lty
396+
return (Just lty, l)
392397
(ts,ty) <- tcUnifying scope s3 (snd <$> cs) mb_ty
393-
return (Opts n (zip ls ts), ty)
398+
return (Opts (Just nty, n) (zip ls ts), ty)
394399
tcRho scope s t _ = unimplemented ("tcRho "++show t)
395400

396401
evalCodomain :: Scope -> Ident -> Value -> EvalM Value
@@ -1110,9 +1115,9 @@ quantify scope t tvs ty = do
11101115
check m n xs (VFV c (VarFree vs)) = do
11111116
(xs,vs) <- mapAccumM (check m n) xs vs
11121117
return (xs,VFV c (VarFree vs))
1113-
check m n xs (VFV c (VarOpts name os)) = do
1114-
(xs,os) <- mapAccumM (\acc (l,v) -> second (l,) <$> check m n acc v) xs os
1115-
return (xs,VFV c (VarOpts name os))
1118+
check m n xs (VFV c (VarOpts nty name os)) = do
1119+
(xs,os) <- mapAccumM (\acc (lty,l,v) -> second (lty,l,) <$> check m n acc v) xs os
1120+
return (xs,VFV c (VarOpts nty name os))
11161121
check m n xs (VAlts v vs) = do
11171122
(xs,v) <- check m n xs v
11181123
(xs,vs) <- mapAccumM (\xs (v1,v2) -> do (xs,v1) <- check m n xs v1

src/compiler/api/GF/Data/Utilities.hs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
-- Basic functions not in the standard libraries
1212
-----------------------------------------------------------------------------
1313

14-
14+
{-# LANGUAGE TupleSections #-}
1515
module GF.Data.Utilities(module GF.Data.Utilities) where
1616

1717
import Data.Bifunctor (first)
1818
import Data.Maybe
1919
import Data.List
20-
import Control.Monad (MonadPlus(..),foldM,liftM,when)
20+
import Control.Monad (MonadPlus(..),foldM,liftM,liftM2,when)
2121
import Control.Applicative(liftA2)
2222
import qualified Data.Set as Set
2323

@@ -128,7 +128,7 @@ compareBy f = both f compare
128128
both :: (a -> b) -> (b -> b -> c) -> a -> a -> c
129129
both f g x y = g (f x) (f y)
130130

131-
-- * functions on pairs
131+
-- * functions on tuples
132132

133133
apFst :: (a -> a') -> (a, b) -> (a', b)
134134
apFst f (a, b) = (f a, b)
@@ -174,6 +174,18 @@ allM p = foldM (\b x -> if b then p x else return False) True
174174
anyM :: (Foldable f, Monad m) => (a -> m Bool) -> f a -> m Bool
175175
anyM p = foldM (\b x -> if b then return True else p x) False
176176

177+
-- | Lifts a monadic action to pairs in the first element.
178+
firstM :: Monad m => (a -> m a') -> (a, b) -> m (a', b)
179+
firstM f (a, b) = (,b) <$> f a
180+
181+
-- | Lifts a monadic action to pairs in the second element.
182+
secondM :: Monad m => (b -> m b') -> (a, b) -> m (a, b')
183+
secondM f (a, b) = (a,) <$> f b
184+
185+
-- | Lifts a pair of monadic actions to an action on pairs, sequencing left-to-right.
186+
bimapM :: Monad m => (a -> m a') -> (b -> m b') -> (a, b) -> m (a', b')
187+
bimapM f g (a, b) = liftM2 (,) (f a) (g b)
188+
177189
-- * functions on Maybes
178190

179191
-- | Returns the argument on the right, or a default value on the left.

src/compiler/api/GF/Grammar/Grammar.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,10 @@ data Info =
344344
| AnyInd Bool ModuleName -- ^ (/INDIR/) the 'Bool' says if canonical
345345
deriving Show
346346

347-
type Type = Term
348-
type Cat = QIdent
349-
type Fun = QIdent
347+
type Type = Term
348+
type MTyTerm = (Maybe Term, Term)
349+
type Cat = QIdent
350+
type Fun = QIdent
350351

351352
type QIdent = (ModuleName,Ident)
352353

@@ -374,7 +375,7 @@ data Term =
374375
| P Term Label -- ^ projection: @r.p@
375376
| ExtR Term Term -- ^ extension: @R ** {x : A}@ (both types and terms)
376377

377-
| Opts Term [Option] -- ^ options: @options s in { e => x ; ... }@
378+
| Opts MTyTerm [Option] -- ^ options: @options s in { e => x ; ... }@
378379

379380
| Table Term Term -- ^ table type: @P => A@
380381
| T TInfo [Case] -- ^ table: @table {p => c ; ...}@
@@ -474,7 +475,7 @@ type Equation = ([Patt],Term)
474475

475476
type Labelling = (Label, Type)
476477
type Assign = (Label, (Maybe Type, Term))
477-
type Option = (Term, Term)
478+
type Option = (MTyTerm, Term)
478479
type Case = (Patt, Term)
479480
--type Cases = ([Patt], Term)
480481
type LocalDef = (Ident, (Maybe Type, Term))

src/compiler/api/GF/Grammar/Parser.y

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ Exp4 :: { Term }
453453
Exp4
454454
: Exp4 Exp5 { App $1 $2 }
455455
| Exp4 '{' Exp '}' { App $1 (ImplArg $3) }
456-
| 'option' Exp 'of' '{' ListOpt '}' { Opts $2 $5 }
456+
| 'option' Exp 'of' '{' ListOpt '}' { Opts (Nothing, $2) $5 }
457457
| 'case' Exp 'of' '{' ListCase '}' { let annot = case $2 of
458458
Typed _ t -> TTyped t
459459
_ -> TRaw
@@ -612,7 +612,7 @@ ListPattTupleComp
612612

613613
Opt :: { Option }
614614
Opt
615-
: '(' Exp ')' '=>' Exp { ($2,$5) }
615+
: '(' Exp ')' '=>' Exp { ((Nothing,$2),$5) }
616616

617617
ListOpt :: { [Option] }
618618
ListOpt

src/compiler/api/GF/Grammar/Printer.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ ppTerm q d (S x y) = case x of
219219
ppTerm q d (ExtR x y) = prec d 3 (ppTerm q 3 x <+> "**" <+> ppTerm q 4 y)
220220
ppTerm q d (App x y) = prec d 4 (ppTerm q 4 x <+> ppTerm q 5 y)
221221
ppTerm q d (V e es) = hang "table" 2 (sep [ppTerm q 6 e,brackets (fsep (punctuate ';' (map (ppTerm q 0) es)))])
222+
ppTerm q d (Opts (_,n) cs) = prec d 4 ("option" <+> ppTerm q 0 n <+> "of" <+> braces (fsep (punctuate ';'
223+
(map (\((_,l),t) -> parens (ppTerm q 0 l) <+> "=>" <+> ppTerm q 0 t) cs))))
222224
ppTerm q d (FV es) = prec d 4 ("variants" <+> braces (fsep (punctuate ';' (map (ppTerm q 0) es))))
223225
ppTerm q d (AdHocOverload es) = "overload" <+> braces (fsep (punctuate ';' (map (ppTerm q 0) es)))
224226
ppTerm q d (Alts e xs) = prec d 4 ("pre" <+> braces (ppTerm q 0 e <> ';' <+> fsep (punctuate ';' (map (ppAltern q) xs))))

0 commit comments

Comments
 (0)